do-while 循环内的扫描输入在下一次迭代时未正确重置
Scanned input inside a do-while loop doesn't reset properly on the next iteration
我的程序旨在将 2 和 20 到 60 之间的输入数字之间的所有偶数相加。逻辑是正确的并且可以工作,但它应该能够 运行 如果用户愿意,当它再次 运行s 时,只有当你输入一个比上一次迭代更高的新整数时,输入才会改变。如果输入小一,它只使用与以前相同的整数输入。这是代码:
import java.util.Scanner;
public class Practice_7_1
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
int input;
int i = 2;
int sum = 0;
int restart;
do
{
System.out.print ("\nEnter a value between 20 and 60: ");
input = scan.nextInt();
if (input >= 20 && input <= 60) // checks validity of input
{
while (i <= input)
{
sum = sum + i;
i = i + 2;
}
System.out.println ("\nSum of even numbers between 2 and " +
input + " is: " + sum);
}
else
{
System.out.println ("\nInput is not between 20 and 60. ");
}
System.out.print ("\nEnter a new value? (1 for yes, any other number
for no): ");
restart = scan.nextInt();
} while (restart == 1);
}
}
例如,如果我输入 20,程序输出:
Sum of even numbers between 2 and 20 is: 110
Enter a new value? (1 for yes, any other number for no):
然后我输入 30(与程序相同 运行):
Sum of even numbers between 2 and 30 is: 240
Enter a new value? (1 for yes, any other number for no):
然后我再次尝试输入 20:
Sum of even numbers between 2 and 20 is: 240
Enter a new value? (1 for yes, any other number for no):
(显然应该是 110,而不是 240)
我最初的想法是它实际上并没有在第二次迭代中扫描新输入,但是因为如果我继续给它提供更大价值的输入它就会工作我知道那不是真的。
在 main() 中使用此代码
Scanner scan = new Scanner (System.in);
int input;
int i = 2;
int sum = 0;
int restart;
do
{
System.out.print ("\nEnter a value between 20 and 60: ");
input = scan.nextInt();
if (input >= 20 && input <= 60) // checks validity of input
{
i = 2;
sum = 0;
while (i <= input)
{
sum = sum + i;
i = i + 2;
}
System.out.println ("\nSum of even numbers between 2 and " +
input + " is: " + sum);
}
else
{
System.out.println ("\nInput is not between 20 and 60. ");
}
System.out.print ("\nEnter a new value? (1 for yes, any other number for no): ");
restart = scan.nextInt();
} while (restart == 1);
我的程序旨在将 2 和 20 到 60 之间的输入数字之间的所有偶数相加。逻辑是正确的并且可以工作,但它应该能够 运行 如果用户愿意,当它再次 运行s 时,只有当你输入一个比上一次迭代更高的新整数时,输入才会改变。如果输入小一,它只使用与以前相同的整数输入。这是代码:
import java.util.Scanner;
public class Practice_7_1
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
int input;
int i = 2;
int sum = 0;
int restart;
do
{
System.out.print ("\nEnter a value between 20 and 60: ");
input = scan.nextInt();
if (input >= 20 && input <= 60) // checks validity of input
{
while (i <= input)
{
sum = sum + i;
i = i + 2;
}
System.out.println ("\nSum of even numbers between 2 and " +
input + " is: " + sum);
}
else
{
System.out.println ("\nInput is not between 20 and 60. ");
}
System.out.print ("\nEnter a new value? (1 for yes, any other number
for no): ");
restart = scan.nextInt();
} while (restart == 1);
}
}
例如,如果我输入 20,程序输出:
Sum of even numbers between 2 and 20 is: 110
Enter a new value? (1 for yes, any other number for no):
然后我输入 30(与程序相同 运行):
Sum of even numbers between 2 and 30 is: 240
Enter a new value? (1 for yes, any other number for no):
然后我再次尝试输入 20:
Sum of even numbers between 2 and 20 is: 240
Enter a new value? (1 for yes, any other number for no):
(显然应该是 110,而不是 240)
我最初的想法是它实际上并没有在第二次迭代中扫描新输入,但是因为如果我继续给它提供更大价值的输入它就会工作我知道那不是真的。
在 main() 中使用此代码
Scanner scan = new Scanner (System.in);
int input;
int i = 2;
int sum = 0;
int restart;
do
{
System.out.print ("\nEnter a value between 20 and 60: ");
input = scan.nextInt();
if (input >= 20 && input <= 60) // checks validity of input
{
i = 2;
sum = 0;
while (i <= input)
{
sum = sum + i;
i = i + 2;
}
System.out.println ("\nSum of even numbers between 2 and " +
input + " is: " + sum);
}
else
{
System.out.println ("\nInput is not between 20 and 60. ");
}
System.out.print ("\nEnter a new value? (1 for yes, any other number for no): ");
restart = scan.nextInt();
} while (restart == 1);