[英]Why doesn't the scanner class recognize the other numbers?
public class Hello {
public static void main(String [] args){
int number, count = 0, sum = 0;
int Largest= 0, largestEvenNumber = 0;
Scanner console = new Scanner(System.in);
number = console.nextInt(); // read an integer entered by a user
if (number > Largest) { // Condition for computing the largest number
Largest = number;
}
if (number < 0) { // Condition for computing the number of negative integers in the sequence
count = count + 1;
}
if (number % 2 == 0) { // Condition for computing the largest even integer in the sequence
if (largestEvenNumber < number) {
largestEvenNumber = number;
}
}
if (number % 3 == 0) { // Condition for computing the sum of numbers divisible by 3
sum += number;
}
System.out.println("\nThe largest integer is " + Largest);
System.out.println("The number of negative integers in the sequence is " + count);
System.out.println("The largest even integer in the sequence is " + largestEvenNumber);
System.out.printf("The sum of numbers divisible by 3 is %d", sum);
}
}
我想得到下面給出的預期 output。 但是,掃描器 class 只讀取第一個數字。 如何在不創建多個對象的情況下更正此問題?
Output:
2
-1
-5
-3
9
8
0
The largest integer is 2
The number of negative integers in the sequence is 0
The largest even integer in the sequence is 2
The sum of numbers divisible by 3 is 0
Process finished with exit code 0
預計 Output:
最大的integer是9 數列中負整數的個數是3 數列中最大的偶數integer是8 能被3整除的數之和是6
謝謝!
您只調用console.nextInt()
一次,因此只讀取一個數字。 如果你想打電話,你需要循環調用console.hasNext()
。 由於您使用的是System.in
。 例如:
while (console.hasNextInt()) {
number = console.nextInt();
// calculations
}
您只閱讀一次輸入。 我在你的代碼中沒有看到循環,所以number = console.nextInt();
只運行一次。 你應該做的是將它放在一個循環中,當你擁有所有數字時退出循環(你如何檢查可以通過多種方式完成),並且當你在循環中時將你收到的任何輸入放入數組或另一種數據結構。 收集完輸入后,檢查數據結構中的所有數字。
private static final int DATA_SIZE = 5; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> data = new ArrayList<>(); // put data in array while (data.size() == DATA_SIZE){ data.add(scanner.nextInt()); } // calculate data from array... }
當然,我還有其他的建議,例如,您可以使用main
方法中可用的數組(當然有知識)
要么
先問用戶你有多少數據量,然后拿去再計算
要么....
如果你想一次輸入所有號碼,你應該設置一個終端號碼。 當您輸入所有號碼時,您應該添加終端號碼以表示輸入結束。
例如:
public static void main(String [] args){
int number, count = 0, sum = 0;
int Largest= 0, largestEvenNumber = 0;
Scanner console = new Scanner(System.in);
int endNumber = -1; //set the terminal number
do {
number = console.nextInt(); // read an integer entered by a user
if (number > Largest) { // Condition for computing the largest number
Largest = number;
}
if (number < 0) { // Condition for computing the number of negative integers in the sequence
count = count + 1;
}
if (number % 2 == 0) { // Condition for computing the largest even integer in the sequence
if (largestEvenNumber < number) {
largestEvenNumber = number;
}
}
if (number % 3 == 0) { // Condition for computing the sum of numbers divisible by 3
sum += number;
}
}while (number!=endNumber);
System.out.println("\nThe largest integer is " + Largest);
System.out.println("The number of negative integers in the sequence is " + count);
System.out.println("The largest even integer in the sequence is " + largestEvenNumber);
System.out.printf("The sum of numbers divisible by 3 is %d", sum);
}
如果代碼只被執行一次,那一行。 因此,掃描器只接受第一個輸入。 使用 while 循環接受多個輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.