久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 97日韩精品| 亚洲精品大全 | 亚洲精品视频在线观看免费 | 在线中文字幕国产 | 99精品久久久国产一区二区三 | 久草在线视频中文 | 少妇黄色| 农夫在线精品视频免费观看 | 精品视频在线观看 | 国产专区视频 | 亚洲一区二区三区视频 | 亚洲成人免费电影 | 亚洲精品国产成人 | 日本久久黄色 | 情侣av | 午夜不卡福利视频 | www.日本国产| 91看片在线观看 | 成人在线一区二区 | 中文字幕在线精品 | 97精品超碰一区二区三区 | 狠狠操电影 | 久久综合成人精品亚洲另类欧美 | 91久久精品一区二区二区 | 成人不卡 | 亚洲情侣视频 | 羞羞视频在线观看网站 | 91精品久久久久 | 精品久久久久久亚洲综合网站 | 91免费在线视频 | 精品动漫一区 | 亚洲综合一区二区三区 | 国产精品96久久久久久 | 国产一级电影在线 | 蜜桃视频在线观看免费视频网站www | 精品亚洲一区二区三区 | 国产一区二区三区四 | 农村真人裸体丰满少妇毛片 | www.天天操 | 欧美成人高清视频 | 日本精品一区二区在线观看 |