Java 如何根据用户输入显示偶数和奇数?- 爪哇
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33859079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I display even and odd numbers based on user input? - java
提问by
Please help... I need to write a program that displays even and odd numbers based on user input, but it loops forever during my last print statement.
请帮助... 我需要编写一个程序,根据用户输入显示偶数和奇数,但它在我上次打印语句中永远循环。
Here is what I have so far, what's wrong?
这是我到目前为止所拥有的,有什么问题吗?
import java.util.Scanner;
public class Integer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = 0;
int odd = 0;
int even = 0;
System.out.println("How many numbers will be entered?");
n = input.nextInt();
while (n < 0 || n > 100) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
n = input.nextInt();
}
while(n >= 0) {
System.out.println("Now enter " + n + " integers: ");
int num = input.nextInt();
while(num > 0) {
for(int i = 0; i <= n; i++){
if (i % 2 == 0) {
even++;
}
else {
odd++;
}
System.out.println("You entered " + odd + " odd numbers and " + even + " even numbers.");
}
}
}
}
}
采纳答案by Redroy
You have a couple of issues with too many while loops. You don't decrement n
anywhere but you also loop on num
while it's greater than 0. So it will loop forever if they stick something other than 0 in there.
您有几个 while 循环过多的问题。你不会n
在任何地方减少,但你也会在num
它大于 0 时循环。因此,如果它们在那里粘贴 0 以外的东西,它将永远循环。
System.out.println("Now enter " + n + " integers: ");
while(n >= 0) {
int num = input.nextInt();
if(num > 0) {
if (num % 2 == 0)
even++;
else
odd++;
}
n--;
}
System.out.println("You entered " + odd + " odd numbers and " + even + " even numbers.");
回答by ehehhh
Scanner input = new Scanner(System.in);
int n = 0;
int odd = 0;
int even = 0;
System.out.println("How many numbers will be entered?");
n = input.nextInt();
while (n < 0 || n > 100) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
n = input.nextInt();
}
while (n > 0) {
System.out.println("Now enter integer " + n + ": ");
int num = input.nextInt();
if (num % 2 == 0) {
even++;
} else {
odd++;
}
n--;
}
System.out.println("You entered " + odd + " odd numbers and " + even + " even numbers.");
Explanation:
You handle n
inputs from user and just check if the input is odd or even with an if-statement. You were over-complicating it.
说明:您处理n
来自用户的输入,只需使用 if 语句检查输入是奇数还是偶数。你把它复杂化了。
回答by Raf
The infinite/forever loops is happening because of input.nextInt()
and this is due to the behavior of next()
method of Scanner class.
无限/永远循环的发生是因为input.nextInt()
并且这是由于next()
Scanner 类的方法的行为。
next(), nextInt()
and its other variations do not move the cursor to the next line (end of current line - instead reads the first complete token with default delimiter as whitespace). nextLine()
method on the other hand reads the whole input and moves the cursor to the next line.
next(), nextInt()
并且它的其他变体不会将光标移动到下一行(当前行的末尾 - 而是读取第一个完整标记,默认分隔符为空格)。nextLine()
另一方面,方法读取整个输入并将光标移动到下一行。
Your code can be fixed by adding input.nextLine()
to move the cursor to the beginning of the line before taking the next input. See below modified code.
您的代码可以通过添加input.nextLine()
将光标移动到行的开头来修复,然后再进行下一个输入。请参阅下面的修改代码。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = 0;
int odd = 0;
int even = 0;
System.out.println("How many numbers will be entered?");
n = input.nextInt();
while (n < 0 || n > 100) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
n = input.nextInt();
}
for(int i = 0 ; i < n; i++) {
System.out.println("Now enter integer " + (i+1) + " of " + n + " integers: ");
int num = input.nextInt();
while(num < 0) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
num = input.nextInt();
}
//this line is needed to move the input cursor to next line (due to use of nextInt() above)
input.nextLine();
if(i%2==0) {
even++;
} else {
odd++;
}
}
System.out.println("You entered " + odd + " odd numbers and " + even + " even numbers.");
}