2013-03-14 32 views
1

我正在爲一個類的項目工作,並且可以使用一些幫助來弄清楚爲什麼我的代碼的行爲方式如此。該任務是提示用戶輸入兩個整數並選擇一個算術運算來執行它們,同時避免「除以零」的情況。下面是我的代碼段:開關直到第二次嘗試才​​接受輸入

import java.util.Scanner; 

public class Program3 
{ 

public static void main(String[] args) 
{ 

    double operandOne; 
    double operandTwo; 
    char newLine = '\n'; 
    Scanner input = new Scanner (System.in); 

    //"input" is an object which calls for input from the keyboard. 

     System.out.print("This program will request values for two operands "); 
     System.out.println("and perform an arithmetic operation of your choice on them."); 
     System.out.println(newLine + "Please enter a value for the first operand."); 
     operandOne = input.nextDouble(); 

     System.out.println("Thank you. Please enter a value for the second operand."); 
     operandTwo = input.nextDouble(); 

     // This section explains what's going on to the user and requests input for the operands. 

     if (operandTwo == 0) 
     { 

      System.out.print("You will not be able to perform division if the "); 
      System.out.println("second operand is zero!"); 
      System.out.println(newLine + "Please choose an option:"); 
      System.out.println("Type 1 to select a new value for the second operand."); 
      System.out.println("Type 2 to continue with a value of zero."); 

      // You can't divide by zero! Are you SURE you want to use that number? 

      int reallyWantZero = input.nextInt(); 
      do 
      { 
       System.out.println(newLine + "You must type either 1 or 2 and press enter."); 
       reallyWantZero = input.nextInt(); 
      } 

      while ((reallyWantZero < 1) || (reallyWantZero > 2)); 
      { 
       switch (reallyWantZero) 
      { 
       case 1: 
        System.out.println(newLine + "Please enter a new value for the second operand."); 
        operandTwo = input.nextDouble(); 
       break; 
       case 2: 
        System.out.println(newLine + "Okay, we will proceed."); 
        break; 
      } 
      } 
     } 

當我執行的代碼,我得到如下:

「請爲第一個操作數輸入一個值

謝謝。請輸入第二個操作數的值

如果第二個操作數爲零,則不能執行除法操作!

請選擇一個選項:

類型1,選擇第二個操作數的新值。 類型2繼續值爲零。

您必須輸入1或2並按Enter鍵。

好吧,我們將繼續進行。」

我不明白,爲什麼在進入2繼續不被接受的第一次,但它接受了第二次。任何人都可以幫助解釋什麼怎麼回事,我該如何改正

在此先感謝

編輯:?!

我修改了代碼:

好吧,我明白你的意思,但它似乎沒有工作。我修改我的代碼爲:

while ((reallyWantZero < 1) || (reallyWantZero > 2)); 
       { 
        System.out.println(newLine + "You must type either  1 or 2 and press enter."); 
        reallyWantZero = input.nextInt(); 
       }    
       switch (reallyWantZero) 
       { 
        case 1: 

但它仍然表現完全相同的方式。另外,如果我輸入了一個無效的數字,該程序就會掛起,根本不會返回任何內容。

但它仍然表現完全相同的方式。另外,如果我輸入了一個無效的數字,該程序就會掛起,根本不會返回任何內容。

回答

5

由於do...while循環總是至少執行一次

do 
{ 
    System.out.println(newLine + "You must type either 1 or 2 and press enter."); 
    reallyWantZero = input.nextInt(); 
} 
while ((reallyWantZero < 1) || (reallyWantZero > 2)); 

它檢查執行該循環體一次之後的條件。所以它總是進入這部分代碼,這意味着用戶將被要求至少輸入兩次。您需要使用while循環來檢查循環的條件,然後再輸入它。

E.g.

while ((reallyWantZero < 1) || (reallyWantZero > 2)) { 
    System.out.println(newLine + "You must type either 1 or 2 and press enter."); 
    reallyWantZero = input.nextInt(); 
} 
+0

哇,你是反應神速! :) 我會將修改添加到問題。謝謝。 – Brian 2013-03-14 17:20:21

+0

在while語句後去掉分號看上面我的例子 – cowls 2013-03-14 17:22:06

+0

哈,就是這樣!它現在按預期工作。非常感謝您的幫助。 – Brian 2013-03-14 17:27:25

1

爲什麼不

while (reallyWantZero != 1 && reallyWantZero != 2) { 
    System.out.println("enter 1 or 2"); 
    reallyWantZero = input.nextInt(); 
} 

//do stuff here with that info 
+0

我是一個完整的初學者,所以我一直在想這一切。 :) – Brian 2013-03-14 17:28:22

相關問題