簡體   English   中英

如何使用“for”循環運行此程序

[英]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++)

當然,這是否使代碼更有意義地更清晰或更具表現力是一個意見問題。 為什么要替換目前尚不清楚whilefor ,但在任何情況下循環的結構是一樣的。 for循環條件中只有兩個語句的語法,一個用於啟動循環,另一個用於遞增循環。 終止條件完全相同。


旁注,這個條件是多余的:

// ...
while (finalNumber !=0);
if(finalNumber == 0)
// ...

根據定義,當達到if條件時, finalNumber 必須0 前一行已經測試了該條件。 你不需要兩次測試相同的條件,運行時不會說明finalNumber的值:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM