2016-08-05 102 views
-1

我是一個字符串來做一個簡單的計算器,如果一個人被零除以它不會破壞代碼。我真的沒有什麼理想,我做了什麼我可以做什麼。我需要得到這個不要school.my家庭作品讀取執行加,減,乘和除兩個numbers.handle無效異常和算術異常。卡在循環while while循環使用try

import java.util.*; 
public class Calcultator { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    int w1=0; 
    int w2=0; 
    int w3=0; 
    double i1 = 0; 
    double i2=0; 
    String sign1 = null; 
    double sum=0; 
    do { 
     try { 
      System.out.println("Enter a your first number"); 
      i1=scan.nextDouble(); 
      w1++; 
      System.out.println("il="+1l); 
      System.out.println("w1="+w1); 
     } catch(Exception e) { 
      System.out.println("you must enter a number"); 
      w1=0; 
     } 
    } while(w1==0); 

    do { 
     try { 
      System.out.println("Enter a your first number");     
      i2=scan.nextDouble(); 
      w1++; 
     } catch(Exception e) { 
      System.out.println("you must enter a number"); 
     } 
    } while(w2==0); 

    do { 
     try { 
      System.out.println("1)/ 2)* 3)- 4)+"); 
      int sign=scan.nextInt(); 
      switch(sign) { 
       case 1: 
        if(i1==0 || i2==0){ 
         System.out.println("Zero can not be Devided"); 
         break; 
        } else { 
         sign1="/"; 
         break; 
        } 
       case 2: 
        sign1="*"; 
        break; 
       case 3: 
        sign1="-"; 
        break; 
       case 4: 
        sign1="+"; 
        break; 
       default : 
        break; 
      } 
     } catch(Exception e) { 
      System.out.println("you must enter a number"); 
     } 
    } while(w3==0); 

    if(sign1=="/") { 
     sum=i1/i2; 
     System.out.println(i1 +"/"+i2+"="+sum); 
    } else if(sign1=="*") { 
     sum=i1*i2; 
     System.out.println(i1 +"*"+i2+"="+sum); 
    } else if(sign1=="-") { 
     sum=i1-i2; 
     System.out.println(i1 +"-"+i2+"="+sum); 
    } else { 
     sum=i1+i2; 
     System.out.println(i1 +"+"+i2+"="+sum); 
    } 

    scan.close(); 
} 
} 
+2

可能的重複http://stackoverflow.com/questions/28623651/scanner-nextline-return-null/28623706#28623706 –

+1

請使用一致的縮進來正確格式化您的代碼。目前很難閱讀。我懷疑你使用了空格和製表符的組合,這就是爲什麼它會被破壞。對於SO,如果您只使用空格縮進,則效果最佳,因此「標籤」的寬度沒有多大差別。 –

+0

提示:你的catch語句是**不**有幫助。它只是告訴你「發生了同樣的錯誤」。將其更改爲告訴**發生了哪種錯誤,例如通過打印其消息和/或堆棧跟蹤。而代碼格式**很重要**。我還建議在**命名上花費更多時間,因爲你使用的名字不會告訴他們背後的事情。 – GhostCat

回答

1

首先,你在第二do-while循環做了一個壞的複製/粘貼:

do { 
    try { 
     System.out.println("Enter a your first number"); // should say "Enter a your second number"   
     i2=scan.nextDouble(); // correct 
     w1++; // should be w2 - but I'd use a bool instead 
    } catch(Exception e) { 
     System.out.println("you must enter a number"); 
     //need to set w2 in here - added below 
     w2 = 0; 
    } 
} while(w2==0); 

w2在這個循環中從來沒有改變(你改變w1代替),所以它將始終爲0,並且永遠不會退出循環。 而不是使用int w1,w2w3,然後不重用他們,我會使用一個單一的(有意義的名字命名的)布爾變量作爲一個風格問題:

boolean validInput = false; 
do { 
    try { 
     System.out.println("Enter your first number"); 
     i1 = scan.nextDouble(); 
     validInput = true; 
    } catch(Exception e) { 
     System.out.println("You must enter a number"); 
     validInput = false; 
    } 
} while(!validInput); 
validInput = false; 
// Second loop to follow using validInput instead of w2 

你的switch語句看起來大多不錯,但再,你永遠不會改變w3。我建議再次使用validInput。在你的第一種情況下,你也說明零不能被分割,這是不正確的。 0可以被分割(0 /任何== 0),但是你不能被零除。您還需要處理用戶輸入無效操作的情況(即簽署< 1或簽署> 4)。 我也會在設置符號的地方進行計算,最後不需要多次計算。 我建議什麼:

validInput = false; 
double result = 0; 
String operationStr = null; // I'll use operationStr instead of sign1, so the reader knows what it's for 
do { 
    try { 
     // More understandable output for the user (unless it must be in the format you supplied) 
     System.out.println("Enter an operation: "); 
     System.out.println("Enter 1 for /"); 
     System.out.println("Enter 2 for *"); 
     System.out.println("Enter 3 for -"); 
     System.out.println("Enter 4 for +"); 
     int inputOperation = scan.nextInt(); // inputOperation instead of sign for readability: + and - are signs, * and/aren't. 
     switch(inputOperation) { 
      case 1: 
       if(i2 == 0){ // only need to worry about dividing BY zero 
        System.out.println("Error: cannot divide by zero - undefined"); 
       } else { 
        operationStr = "/"; 
        validInput = true; 
        result = i1/i2; 
       } 
       break; // only really need one break statement, but this is again a trivial matter of style. 
      case 2: 
       operationStr = "*"; 
       validInput = true; 
       result = i1 * i2; 
       break; 
      case 3: 
       operationStr = "-"; 
       validInput = true; 
       result = i1 - i2; 
       break; 
      case 4: 
       operationStr = "+"; 
       result = i1 + i2; 
       break; 
      default: 
       // An invalid int was entered, out of the range of our operators 
       System.out.println("Error: Please enter a valid operation: 1, 2, 3, or 4 ") 
       break; 
     } 
    } catch(Exception e) { 
     System.out.println("you must enter a number"); 
    } 
} while(validInput = false); 

System.out.println(i1 + operationStr + i2 + "=" + result); 
scan.close(); 
// End of program 

最後一點,還有,你可能需要修正了許多拼寫錯誤和嚴重命名變量,在你的代碼,該標記不會與馬虎語法太深刻的印象或拼寫錯誤。嘗試根據他們的做什麼來命名變量,以便您的標記和任何閱讀它的人都可以輕鬆理解您的代碼。例如,改名爲i1 input1。使用boolean validInput而不是int w1, w2, w3,因爲通過查看它們,這些沒有任何意義。在涉及變量的操作中的值之間使用空格以提高可讀性,最後,正確使用縮進,以便讀者理解您的循環。

+0

非常感謝。那麼它爲什麼會陷入第一個循環而catch語句不起作用。如果我無法獲得第一個循環的工作,我不會專注於第二個循環。對於sloopy代碼,我很遺憾。我對它仍然是新的。我瞭解基礎知識。它在學校沒有幫助,我一直在學習android編程使用可能會有點混亂。他們都使用java和如此不同。謝謝ypu我會盡我所能採取您的建議。再次感謝您。 – matti