2013-12-09 47 views
0

所以我只是玩弄一些我學到的東西,做出一些毫無意義的計算器。之前有人說,我知道這可以做得更簡單!「第一種類型的int,第二種類型的布爾值」java

我對我的發言時,雖然得到這個錯誤(錯誤的操作類型的二元運算符「||」第一種類型:INT,第二種類型:布爾「)

它也拋出的問題與

int AA = A + B; 
int AB = A * B; 
int AC = A/B; 

說找不到符號

class cases{ //1-4 sums with cases, delete after// 
    public static void main(String args[]){ 

    System.out.println("Welcome to a pointless calculator."); 

     int A = Integer.parseInt(args[0]); 
     int B = Integer.parseInt(args[1]); 
     switch (A){ 
     case 1: 
      System.out.print("You entered 1"); 
      break; 
     case 2: 
      System.out.print("You entered 2"); 
      break; 
     case 3: 
      System.out.print("You entered 3"); 
     break; 
     case 4: 
      System.out.print("You entered 4"); 
     } 
     while (A || B > 4){ 
     System.out.print("please enter numbers 1-4"); 
     break; 


     } 
     switch (B) { 
     case 1: 
      System.out.println(" and 1"); 
      break; 
     case 2: 
      System.out.println(" and 2"); 
      break; 
     case 3: 
      System.out.println(" and 3"); 
     break; 
     case 4: 
      System.out.println(" and 4"); 
     } 

     } 
     { 

     int AA = A + B; 
     int AB = A * B; 
     int AC = A/B; 


     System.out.print("the answers added = "); 
     System.out.println(AA); 
     System.out.print("the answers multipled = "); 
     System.out.println(AB); 
     System.out.print("the answers divided = "); 
     System.out.println(AC); 



     } 
     } 

回答

3

的問題在這裏:

while (A || B > 4){ 

那表情打破了這樣的:

while (
    A 
    || 
    B > 4 
){ 

Aint型的,但你像一個boolean對待它。你不能用Java來做到這一點。你可能意味着:

while (A > 4 || B > 4){ 

有可能是代碼進一步的問題。例如,你有一個塊與你的代碼最後沒有任何關聯。我在認爲在Java中,最終成爲一個實例初始化塊,但坦率地說,我認爲你需要退後一步,通過一些教程。

+0

謝謝 - 現在我所面臨的額外問題是,它不會在有效的arg條目中打印out.prints。 – user2992500

2

while內部的條件表達式需要解析爲boolean||運算符的兩個操作數需要求值爲boolean值。見here

while (A || B > 4){ 

不是語法正確。

同樣在碼

... 
    } 
    { 

    int AA = A + B; 
    int AB = A * B; 
    int AC = A/B; 


    System.out.print("the answers added = "); 
    System.out.println(AA); 
    System.out.print("the answers multipled = "); 
    System.out.println(AB); 
    System.out.print("the answers divided = "); 
    System.out.println(AC); 



    } 
    } 

你關閉你的main方法塊和啓動實例初始化了這一點。此時AB不在範圍內。

如果您正確縮進括號,您會看到塊的結束位置。

相關問題