久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

讀取輸入,直到輸入特定數字

Read input until a certain number is typed(讀取輸入,直到輸入特定數字)
本文介紹了讀取輸入,直到輸入特定數字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當輸入零并立即開始求和時,我需要停止詢問整數輸入.當我輸入零時,我的程序不會停止.我需要它停止并開始總結它收集的所有輸入.

I need to stop asking for integer inputs when zero is typed as an input and start summation immediately. My program doesn't stop when I type zero. I need it to stop and start summing up all the inputs it has gathered.

這是我所擁有的:

public class Inttosum {

    public static void main(String[] args) {
        System.out.println("Enter an integer");
        Scanner kb = new Scanner(System.in);
        int askool = kb.nextInt();
        int sum = 0;
        int score = 0;


        while(askool != 0){
          score = kb.nextInt();
          sum += score;
        }
    }
}

///////////////最終代碼有效..謝謝!公共類 Inttosum {

/////////////////The final code which worked..Thank you! public class Inttosum {

    public static void main(String[] args) {
        System.out.println("Enter an integer");

    Scanner kb = new Scanner(System.in);
    int sum = 0;
    int score = 0;
    do {
        score = kb.nextInt();
        sum += score;

    }while(score != 0);
    System.out.print(sum); 
 }
}

推薦答案

do-while

您正在使用稱為 askool 的東西作為循環條件,但在循環中更新變量 score.您可以使用 do-while 循環.改變

do-while

You are using something called askool as a loop condition, but updating the variable score in your loop. You could use a do-while loop. Change

while(askool != 0){
    score = kb.nextInt();
    sum += score;
}

類似

do {
    score = kb.nextInt();
    sum += score;
}while(score != 0);

使用 break

我還建議調用 Scanner.hasNextInt() 在調用 nextInt 之前.而且,由于你不使用 score(只是 sum)你可以這樣寫,

Using break

I also suggest calling Scanner.hasNextInt() before calling nextInt. And, since you don't use the score (just the sum) you could write it like,

int sum = 0;
while (kb.hasNextInt()) {
    int score = kb.nextInt();
    if (score == 0) {
        break;
    }
    sum += score;
}
System.out.print(sum);

如果用戶輸入文本,它也會停止(并且仍然 sum 所有 ints).

Which will also stop (and still sum all ints) if the user enters text.

這篇關于讀取輸入,直到輸入特定數字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 日日夜夜天天 | 免费一级a毛片 | 国产精品久久一区二区三区 | 国产肉体xxxx裸体784大胆 | 亚洲视频免费在线观看 | 精品一区在线 | 国产成人影视 | 黄色大片网站 | 国产精品成人一区 | 亚洲欧美国产高清va在线播放 | 日韩毛片免费看 | 国产精品成人国产乱一区 | 国产精品久久久久久亚洲影视 | 一区二区三区免费 | 欧美日韩大片 | 色综合久久久久 | 白浆在线 | 黄av在线 | 国产成人免费在线观看 | 久久av网 | 亚洲iv一区二区三区 | 在线观看av的网站 | 黄色大片在线免费观看 | 色午夜 | 在线一区二区三区 | 黄色av毛片| av成人在线播放 | 国产一级片免费看 | 免费网站观看www在线观看 | 中文字幕www | 日韩av在线一区二区 | 黄色小视频免费看 | 亚洲网站在线 | 中文字幕在线观看一区二区三区 | 91久久久久久久久久 | 日韩精品国产精品 | 欧美在线中文字幕 | 最新日韩在线 | 黄色免费观看视频 | 精品一区视频 | 攵女(高h)欢欲 |