久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 尤物在线 | 国内久久 | www.色综合| 夜夜爽99久久国产综合精品女不卡 | 日韩欧美在线视频一区 | 久久久久久久久久影视 | av网站在线看 | 日韩美香港a一级毛片免费 国产综合av | 99国内精品久久久久久久 | a黄视频 | 日韩欧美精品一区 | 久久99精品久久久久久国产越南 | 毛片免费看 | 色综合久久久 | 国产黄色在线观看 | 日韩一区二区三区精品 | 国产 日韩 欧美 在线 | 久久久av | 五月婷六月丁香 | 久久久久国产一区二区三区 | 国产精品久久亚洲 | 欧美激情国产精品 | 天天插天天操 | 精品视频一区二区三区在线观看 | 成年人精品视频 | 高清国产午夜精品久久久久久 | 日本在线视 | 在线观看国产视频 | 色婷婷av99xx| 91久久综合 | 精品一二区 | 日本字幕在线观看 | 国产伦精品一区二区三区照片91 | 国产精品国产三级国产aⅴ中文 | 亚洲一区二区三区四区五区中文 | 99精品视频网 | 国产影音先锋 | 国产精品mv在线观看 | 久久成人综合 | 国产一区在线免费观看 | 精品一区二区免费视频 |