久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 天天色官网 | 天天爽夜夜爽精品视频婷婷 | 亚洲视频免费在线播放 | 欧美日韩视频在线 | 日韩成人av在线 | 亚洲va欧美va人人爽午夜 | 欧美日韩一区二区视频在线观看 | 九九99精品| 亚洲欧美日本在线 | 九九免费观看视频 | 国产一区二区三区久久久久久久久 | 超碰97av | 在线一区 | 国产乱码精品一区二区三区五月婷 | 午夜影院视频在线观看 | 日韩高清中文字幕 | 欧美视频xxx | 天天躁日日躁狠狠躁2018小说 | 亚洲天天 | 涩涩导航| 一级毛片色一级 | 成人精品免费视频 | 久久专区 | 免费人成激情视频在线观看冫 | 国产精品成人一区 | 在线看日韩 | 欧美国产精品一区二区三区 | 99综合网 | 成人久久久 | 97国产精品视频 | 欧美一级特黄aaa大片在线观看 | 欧美一区二区久久 | 亚洲精品毛片av | 人妖videosex高潮另类 | 日韩久久精品 | 黄色一级特级片 | 日本 欧美 国产 | 婷婷丁香在线视频 | 久久国产精品99久久久久久丝袜 | 欧美日韩国产精品一区二区 | 日本精品一区二区三区在线观看视频 |