我制作了一个程序,提示用户输入两个数字。如果这些数字是偶数倍,我将在两个数字之间显示所有奇数。如果这些数字不是偶数倍,我会要求用户重新输入这两个数字。我使用while循环成功完成了这个程序。但我想知道如何使用"来制作这样的节目。而是循环。这是我的原始代码,这是我在while循环中使用的地方。所有提示和建议都会有所帮助。
/**
* Description: This program takes two integers entered by the user and
* displays the odd numbers between the integers if the integers entered
* are even multiples. If the integers entered are not even multiples,
* the program will prompt the user to re enter the integers.
*/
/**
* This is the main loops class. This class
* includes the main method as well
* as the program method.
*/
public class loops {
/**
* This is the main method. It provides an
* entry into the program.
*/
public static void main (String []args) {
loops lps = new loops();//Instantiating loops class.
lps.program();//Invoking the program method on the loops class.
}
/**
* This is the program method. This
* method will carry out the program
* functions.
*/
public void program() {
/**
* Declaring variables that
* will be used in the program.
*/
int firstNumber;
int secondNumber;
int finalNumber;
java.util.Scanner input=new java.util.Scanner(System.in);//Using the Java utility scanner.
try {//Try statement. Used to handle exceptions.
do {//Using a do-while loop.
/**
* Prompting the user to enter two
* integers. Then taking the remainder
* of the integers.
*/
System.out.println("Enter your first number.");
firstNumber = input.nextInt();
input.nextLine();
System.out.println("Enter your second number.");
secondNumber = input.nextInt();
finalNumber = firstNumber % secondNumber;
if(finalNumber !=0) {//If the remainder is not zero, these statements should be printed.
System.out.println("Please enter even multiples.");
System.out.println("(Try switching the order in which you input the numbers)");
}
}
/**
* This is a while loop that will keep prompting the
* user to enter two integers until the remainder
* of the two integers is 0 and the two numbers
* entered are even multiples.
*/
while (finalNumber !=0);
if(finalNumber == 0) {//The following code will be executed only if the remainder is 0.
System.out.println("Displaying all odd numbers between " + firstNumber + " and " + secondNumber);
int number = secondNumber;//Declaring number variable, giving it value of secondNumber.
/**
* This is a while loop that will display all the odd
* numbers between the two integers entered.
*/
while(number <= firstNumber){
if (number % 2 != 0) {
System.out.println(number);
}
number++;
}
}
}
catch (java.util.InputMismatchException e) { //Catching multiple exceptions.
System.out.println("Please enter whole numbers.");
}
catch ( java.lang.ArithmeticException e) { //Catching multiple exceptions.
System.out.println("Please enter whole numbers.");
}
}
}
答案 0 :(得分:0)
你这样做的地方:
while (finalNumber !=0)
你可以这样做:
for (;finalNumber != 0;)
(将条件置于循环的开头,当然,因为for
没有do... while
等价物,并且你在哪里这样做:
while(number <= firstNumber)
你可以这样做:
for (;number <= firstNumber;)
或者您可以组合一些语句并将两行代码移动到循环条件中:
for (int number = secondNumber; number <= firstNumber; number++)
当然,这是否使代码更有意义地更清晰或更具表现力是一个意见问题。目前尚不清楚为什么要将while
替换为for
,但无论如何,循环结构都是相同的。 for
在循环条件中只有两个语句的语法,一个用于启动循环,另一个用于递增循环。终止条件完全相同。
旁注,这个if条件是多余的:
// ...
while (finalNumber !=0);
if(finalNumber == 0)
// ...
根据定义,当达到if
条件时,finalNumber
必须为0
。前一行已经测试了该条件。您不需要两次测试相同的条件,运行时不会考虑finalNumber
的值:)