2015-10-31 144 views
0
import java.io.PrintStream; 
import java.util.Scanner; 

public class BasicCalculator 
{ 
    public static void main(String[] args) 
    { 
    int ADDITION = 1; 
    int SUBTRACTION = 2; 
    int MULTIPLICATION = 3; 
    int DIVISION = 4; 
    int EXIT = 5; 

    Scanner keyboard = new Scanner(System.in); 
    int choice; 
    do 
    { 
     System.out.println("Choose from the following:"); 
     System.out.println("1. Add 2 integers"); 
     System.out.println("2. Subtract 2 integers"); 
     System.out.println("3. Multiply 2 integers"); 
     System.out.println("4. Divide 2 integers"); 
     System.out.println("5. Exit"); 
     System.out.print("Enter choice: "); 
     choice = keyboard.nextInt(); 
     if ((choice == 1) || (choice == 2) || (choice == 3) || (choice == 4)) 
     { 
     System.out.print("Enter first integer: "); 
     int left = keyboard.nextInt(); 
     System.out.print("Enter second integer: "); 
     int right = keyboard.nextInt(); 
     switch (choice) 
     { 
     double Result; 
     case 1: 
      Result = left + right; 
      System.out.println(left + " + " + right + " = " + Result); 
      break; 
     case 2: 
      Result = left - right; 
      System.out.println(left + " - " + right + " = " + Result); 
      break; 
     case 3: 
      Result = left * right; 
      System.out.println(left + " * " + right + " = " + Result); 
      break; 
     case 4: 
      Result = left/right; 
      System.out.println(left + "/" + right + " = " + Result); 
     } 
     System.out.println(); 
     } 
    } while (choice != 5); 
    } 
} 

錯誤:簡單的語法錯誤

BasicCalculator.java:34: error: case, default, or '}' expected 
     int Result; 
     ^
BasicCalculator.java:34: error: case, default, or '}' expected 
     int Result; 
      ^
BasicCalculator.java:34: error: case, default, or '}' expected 
     int Result; 

上面的代碼是我爲我的介紹對計算機編程類項目,我碰到的是從格式問題而產生了一些錯誤。我可以得到一些基本的幫助什麼導致錯誤。我仍然試圖習慣於閱讀記事本++中的錯誤描述並理解它們的含義。

+1

第34行是什麼行? – Mureinik

+0

您不能在switch-statement中聲明變量。 –

回答

0

(我不知道這是完全正確的,因爲我真的不這樣做的Java ,但在這裏不用) 我認爲錯誤是,你必須double Result; switch語句內 - 的Java只允許switch語句中無論是casedefault。嘗試將double Result行放在switch (...行的上方。另外,我會確保所有下方的case 1/2/3報表線是適當的縮進 - 它可能只是在堆棧上交換的格式,但Result = ...線向前看一個字符(不知道這是否會有所作爲,但它的最好總是有一致的代碼)。

+0

感謝爲我清除。它始終是我拋棄我的小事。但感謝您花時間使用論壇來幫助像我這樣的令人頭疼的用戶。再次感謝! –

+0

@ R.Isaac這很好:),你總是會忘記這樣的小細節,每個人都會這樣做。別客氣! – Joe

+0

就像筆記一樣,下面的@dasblinkenlight也有一個好處 - 它們也可以在'case'部分中,也不在外面 – Joe

2

您不能在case標籤之外的 a switch聲明中聲明變量。如果你想Result所有case選自S共享,包括default,事先聲明它switch,就像這樣:

double Result = 0; 
switch (choice) 
{ 
case 1: 
    Result = left + right; 
    System.out.println(left + " + " + right + " = " + Result); 
    break; 
case 2: 
    Result = left - right; 
    System.out.println(left + " - " + right + " = " + Result); 
    break; 
case 3: 
    Result = left * right; 
    System.out.println(left + " * " + right + " = " + Result); 
    break; 
case 4: 
    Result = left/right; 
    System.out.println(left + "/" + right + " = " + Result); 
}