我试图从用户输入的数字中找到偶数和甚至最大值。例如,如果他们回答“有多少整数?”用4输入整数:2,9,18,4应输出:
how many integers? 4
next integer? 2
next integer? 9
next integer? 18
next integer? 4
even sum = 24
even max = 18
这是我的代码:
public static void evenSum(){
//prompt the user to enter the amount of integers
Scanner console = new Scanner(System.in);
System.out.print("how many integers? ");
int numbers = console.nextInt();
//prompt user to enter the first integer
System.out.print("next integer? ");
int firstNum = console.nextInt();
//set the even max to the firstNum
int evenMax = firstNum;
//set the evenSum to zero
int evenSum = 0;
//for loop for the number of times to ask user to input numbers
for (int i = 2; i <= numbers; i++) {
System.out.print("next integer? ");
int num = console.nextInt();
//check to see if the first number is even
if (firstNum % 2 == 0){
//if it is even then add it to the evenSum
evenSum += firstNum;
}
//check to see if the numbers entered are even
if (num % 2 == 0) {
//if they are even add them to the evenSum
evenSum += num;
}
//check to see if the number entered is bigger than the first number
if (num > firstNum) {
if (num % 2 == 0 ) {
evenMax = num;
}
}
}
System.out.println("even sum = " +evenSum);
System.out.println("even max = " +evenMax);
}
但这是我的输出是:
how many integers? 4
next integer? 2
next integer? 9
next integer? 18
next integer? 4
even sum = 28
even max = 4
有人可以帮我弄清问题是什么吗?
答案 0 :(得分:0)
将for循环中的以下代码移到for循环之前 -
if (firstNum % 2 == 0){
//if it is even then add it to the evenSum
evenSum += firstNum;
}
这样可以防止在evenSum中重复添加第一个数字
答案 1 :(得分:0)
你也想要
if (num > evenMax) {
if (num % 2 == 0 ) {
evenMax = num;
}
}
或者
if (num > evenMax && num % 2 == 0) {
evenMax = num;
}
在您的方案中,firstNum
为2,因此其后的每个数字在技术上都较大,因此您(理论上)不会获得在第一个数字后输入的最大偶数。
答案 2 :(得分:0)
向上移动for循环中的第一个if条件(在for循环之外) 或者在处理数据结构之前将所有用户输入存储在数据结构中。 将它们存储在Array中可以更容易地操作数据。
答案 3 :(得分:0)
你做了一些非常奇怪的事情,第一次输入数字时,它被视为特殊。这导致输入的第一个偶数(在这种情况下为2)被多次添加到总数中。
将所有输入放在同一个循环中,这样您就可以平等对待所有内容:
public static void evenSum(){
//prompt the user to enter the amount of integers
Scanner console = new Scanner(System.in);
System.out.print("how many integers? ");
int numbers = console.nextInt();
int evenSum = 0;
int evenMax = 0;
//for loop for the number of times to ask user to input numbers
for (int i = 0; i < numbers; i++) {
//input new number
System.out.print("next integer? ");
int num = console.nextInt();
//check to see if the number is even. if it is not even,
//we don't care about it at all and just go to the next one
if (num % 2 == 0){
//add it to the sum
evenSum += num;
//if it's larger than the maximum, set the new maximum
if (num > evenMax) {
evenMax = num;
}
}
}
System.out.println("even sum = " +evenSum);
System.out.println("even max = " +evenMax);
}
如您所见,此代码也只检查一个数字是否只有一次。每次使用时都不需要连续检查是否num
:它的值在循环的单次运行期间不会改变。
答案 4 :(得分:0)
工作代码: -
Scanner console = new Scanner(System.in);
int numbers =0, firstNum =0, num =0 ;
System.out.print("how many integers? ");
numbers = console.nextInt();
System.out.print("next integer? ");
firstNum = console.nextInt();
int evenMax = 0;
int evenSum = 0;
if(firstNum%2==0)
{
evenSum = firstNum;
evenMax = firstNum;
}
for (int i = 1; i < numbers; i++) {
System.out.print("next integer? ");
num = console.nextInt();
if (num % 2 == 0) {
//don't add firstNum multiple times to the evenSum, earlier it was added every time you entered an even number
evenSum += num;
//check if the number you entered, i.e. num greater than the already existing greatest number i.e. evenMax and if so update it
evenMax = num > evenMax: num?evenMax;
}
}
System.out.println("even sum = " +evenSum);
System.out.println("even max = " +evenMax);
}
希望这会有所帮助。您的代码中存在三个主要问题: -
每次输入偶数时,firstNum(如果它是偶数)都会被添加到总和中。即,如果第一个数字是4并且循环运行10次并且遇到6个偶数,那么连同偶数4也会被加6次。如果你想把它作为特殊数字使用并单独得到它的价值,你就必须在循环之前将它加到总和上。
您应该将每个新的偶数与先前的最大偶数进行比较,从而设置evenMax的值。您将它们与firstNum进行比较,因此如果第一个数字是2且最后一个偶数是大于2的任何数字,则将其设置为evenMax的值。将每个偶数与当前最大偶数进行比较,即evenMax的当前值。
您不检查第一个数字是否为偶数,并将其指定为最大数。因此,如果它是999999,它仍然被分配,但它不是。
如果您觉得有用,请将其检查为正确答案并投票。