输出两个整数之间偶数的总和 [英] Output sum of even numbers between two integers
问题描述
我正在我的一门大学课程中研究一个简单的 JAVA 问题.我被这个程序难住了.我将展示我目前所拥有的,并给出我必须回答的问题.我也在 StackOverflow 上查看了一个类似的问题,但这不是同一个问题,所以它没有帮助.我需要写的程序是:
I am working on a simple JAVA question in one of my college courses. I am stumped on this one program. I will display what I have so far and give the question I have to answer. I also looked at a similar question on StackOverflow, BUT it isn't the same problem so it DIDN'T help. The program I need to write is:
编写一个使用while"循环执行以下步骤的程序:
Write a program that uses 'while' loops to perform the following steps:
a.) 提示用户输入两个整数:'firstNum' 和 'secondNum'(firstNum 必须小于 secondNum)
a.) Prompt the user to input two integers: 'firstNum' and 'secondNum' (firstNum must be less than secondNum)
b.) 输出 'firstNum' 和 'secondNum' 之间的所有奇数.
b.) Output all the odd numbers between 'firstNum' and 'secondNum' inclusive.
c.) 输出 'firstNum' 和 'secondNum' 之间所有偶数的总和.
c.) Output the sum of all the even numbers between 'firstNum' and 'secondNum' inclusive.
这就是我目前所拥有的......(我仍然需要计算偶数并将它们相加)
This is what I have so far... (I still need to calculate the even numbers and sum them up)
//import classes
import java.util.*;
public class chapter5no9
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//Part A
int firstNum;
int secondNum;
int sumEven;
System.out.println("Please enter an integer: ");
firstNum = console.nextInt();
System.out.println("Please enter another integer less than the first integer: ");
secondNum = console.nextInt();
//Part B
if (firstNum < secondNum)
{
System.out.print("Your second number is greater than the first. So Please re-enter: ");
secondNum = console.nextInt();
}
else
{
System.out.print("Odd Numbers: ");
firstNum++;
while (firstNum > secondNum)
{
if (secondNum % 2 != 0)
{
System.out.print(" " + secondNum);
}
secondNum++;
}
System.out.println();
System.out.print("Sum of Even Numbers: ");
firstNum++;
while (firstNum > secondNum)
{
if (secondNum % 2 != 0)
{
System.out.print(" " + secondNum);
}
secondNum++;
}
}
}
}
推荐答案
我创建了 loopCounter 变量来处理所需的迭代,而无需更改用户输入的值.对您的代码进行了以下更改.
I created the loopCounter variable to handle the required iterations without changing the values inputted by the user. The following changes have been made to your code.
A 部分:添加了一个 while 循环来验证用户输入.还更改了 if 语句中的逻辑.
Part A: added a while loop to validate user input. Also changed logic in if statement.
B 部分:用一个循环打印奇数和总偶数
Part B: used one loop to print odd numbers and total even numbers
//Part A
int firstNum;
int secondNum;
int sumEven=0;
System.out.println("Please enter an integer: ");
firstNum = input.nextInt();
System.out.println("Please enter another integer less than the first integer: ");
secondNum = input.nextInt();
//Part B
//validate input in a loop
while(true)
{
if (firstNum > secondNum)
{
System.out.print("Your second number is larger than the first. So Please re-enter: ");
secondNum = input.nextInt();
}
else
{
break;
}
}
System.out.print("Odd Numbers: ");
int loopCounter=firstNum;
while(loopCounter<secondNum)
{
if (loopCounter%2!=0)
{
System.out.print(" " + loopCounter);
}//end if
else
{
sumEven+=loopCounter;
}//end else
loopCounter++;
}
System.out.println();
System.out.print("Sum of Even Numbers: ");
System.out.print(sumEven);
}
这篇关于输出两个整数之间偶数的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!