第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使無效數(shù)字不添加到我的最終計(jì)算和計(jì)數(shù)器總數(shù)中?

如何使無效數(shù)字不添加到我的最終計(jì)算和計(jì)數(shù)器總數(shù)中?

蕪湖不蕪 2021-11-03 11:02:58
當(dāng)我輸入一個(gè)有非法分?jǐn)?shù)提示的數(shù)字時(shí),我的問題出現(xiàn)了,它仍然向我的計(jì)數(shù)器添加一個(gè)數(shù)字,并將該數(shù)字添加到總數(shù)中,因此平均值被丟棄。我一直在絞盡腦汁,嘗試了幾乎所有方法來弄清楚。代碼有點(diǎn)草率,因?yàn)槲疫€沒有清理它,我只是想先整理一下移動(dòng)部分。public class StudentSentinel {  public static void main(String[] args) {    double score;    double total = 0.0;    double average;    int scoreCount = 0;    // create the Scanner object. Name it stdin    Scanner stdin = new Scanner(System.in);    // title at the top of the output    System.out.println(" student score report");;    //   read the first score    System.out.printf("Enter a score  (1-100, -1 to quit)" +      ": ", scoreCount);    score = stdin.nextDouble();    scoreCount++;    while (score != -1.0) {      total += score;      System.out.printf("Enter a score  (1-100, -1 to quit)" +        ": ", scoreCount);      scoreCount++;      score = stdin.nextDouble();      if (score < -1)        System.out.println("Illegal score.  Try again");      else if (score > 100) {        System.out.println("Illegal score.  Try again");      }      // increment the loop counter    }    // end of for loop    average = total / scoreCount;    System.out.printf("\nThe average score for %d students is %8.2f\n",      scoreCount, average);  } // end of main    } // end of class definition
查看完整描述

2 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

首先檢查分?jǐn)?shù)是否合法,然后增加計(jì)數(shù)器并將其添加到您的total. 您還可以分配score和檢查不是-1通過單個(gè)操作。并且始終使用大括號(hào)(即使它們是可選的)。喜歡,


// read the first score

System.out.printf("Enter a score  (1-100, -1 to quit)" + ": ", scoreCount);

while ((score = stdin.nextDouble()) != -1.0) {

    if (score < -1 || score > 100) {

        System.out.println("Illegal score.  Try again");

    } else {

        scoreCount++;

        total += score;

    }

    System.out.printf("Enter a score  (1-100, -1 to quit)" + ": ", scoreCount);

}


查看完整回答
反對(duì) 回復(fù) 2021-11-03
?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊

試試這個(gè)代碼。這個(gè)對(duì)我有用:


public class StudentSentinel  {


 public static void main(String[] args) {

    double score;

    double total = 0.0;

    double average;

    int scoreCount = 0;


    // create the Scanner object. Name it stdin

    Scanner stdin = new Scanner(System.in);


    // title at the top of the output

    System.out.println(" student score report");;


    //   read the first score

    System.out.printf("Enter a score  (1-100, -1 to quit)" +

            ": ", scoreCount);


    score = stdin.nextDouble();

    scoreCount++;

    total += score;

    while (score != -1.0) {



        System.out.printf("Enter a score  (1-100, -1 to quit)" +

                ": ", scoreCount);

        scoreCount++;

        score = stdin.nextDouble();

        if (score < -1) {

            System.out.println("Illegal score.  Try again");

            continue;

        }else if (score > 100) {

            System.out.println("Illegal score.  Try again");

            continue;

        }

        total += score;

        // increment the loop counter

    }

    // end of for loop

    average = total / scoreCount;

    System.out.printf("\nThe average score for %d students is %8.2f\n",

            scoreCount, average);

} // end of main

}


查看完整回答
反對(duì) 回復(fù) 2021-11-03
  • 2 回答
  • 0 關(guān)注
  • 166 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)