[英]How Can I run this program by using “for” loops
我制作了一个程序,提示用户输入两个数字。 如果这些数字是偶数倍,我将在两个数字之间显示所有奇数。 如果这些数字不是偶数倍,我会要求用户重新输入这两个数字。 我使用while循环成功完成了这个程序。 但我想知道如何使用“for”循环来制作这样的程序。 这是我的原始代码,这是我在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.");
}
}
}
你这样做的地方:
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
循环条件中只有两个语句的语法,一个用于启动循环,另一个用于递增循环。 终止条件完全相同。
旁注,这个条件是多余的:
// ...
while (finalNumber !=0);
if(finalNumber == 0)
// ...
根据定义,当达到if
条件时, finalNumber
必须为0
。 前一行已经测试了该条件。 你不需要两次测试相同的条件,运行时不会说明finalNumber
的值:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.