2017-05-26 59 views
-2

我正在學習「通過遊戲學習Java」by Lubomir Stanchev,有我無法解決的這個問題。Java如何使用指定的表達式計算變量的值?

我們將隨機要求玩家添加,乘或減一位數字(我們跳過除法,因爲除以兩個數字的結果不是整數)。遊戲應該問10個數學問題並記錄答案。最後,遊戲應該告訴玩家他們做得如何,也就是說他們回答了多少問題。

這是我有:

import java.util.Scanner; 

public class Arithmetic { 
    public static void main(String args[]){ 
     Scanner keyboard = new Scanner(System.in); 
     int i, x, y, z, answer, a, counter = 0; 
     char sign; 
     for(i = 0; i < 10; i++) { 
      x = (int)(Math.random() * 10); 
      y = (int)(Math.random() * 10); 
      a = (int)(Math.random() * 3);//random sign 

      if(a==0){ 
       sign = '+'; 
      } else if(a == 1) { 
       sign = '-'; 
      } else { 
       sign = '*'; 
      } 

      System.out.print(x + " " + sign + " " + y + " = "); 
      z = keyboard.nextInt();   
      answer = x + sign + y; 

      System.out.println(answer); 

      if(z == answer){ 
       System.out.println("Correct"); 
       counter++;     
      } else { 
       System.out.println("Wrong");  
      } 
     } 
     System.out.println("You answered "+counter+" questions correctly"); 
    } 
} 

有可變答案的價值問題。它不會計算表達式,因爲x是整數,而變量符號是char。我來自JS,所以我覺得這很奇怪。在JS中,不論其類型如何,表達式都會自動計算或連接。我在第3章中,所以我沒有研究parseInt這個東西,我相信它不適用於字符。

感謝您的幫助。

這是我現在的更新答案:

public class Arithmetic { 
    public static void main(String args[]){ 
     Scanner keyboard = new Scanner(System.in); 
     int i, x, y, z, answer, a, counter=0; 
     char sign; 
     for(i=0;i<10;i++){ 
      x = (int)(Math.random() * 10); 
      y = (int)(Math.random() * 10); 
      a = (int)(Math.random() * 3);//random sign 

      if(a==0){ 
       sign = '+'; 
       answer = x + y; 
      } else if(a==1) { 
       sign = '-'; 
       answer = x - y; 
      } else { 
       sign = '*'; 
       answer = x * y; 
      } 

      System.out.print(x+" "+sign+" "+y+" = "); 
      z = keyboard.nextInt();   

      if(z==answer){ 
       System.out.println("Correct"); 
       counter++;     
      } else { 
       System.out.println("Wrong");  
      } 
     } 
     System.out.println("You answered "+counter+" questions correctly"); 
    } 
} 

它沒有編譯!

+0

「在JS中,表達式是自動計算或concatenat不管其類型如何「 - 你能詳細說明一下嗎? – Nayuki

+0

@Nayuki https://www.w3schools.com/js/js_type_conversion.asp – BEngleman

回答

5

你需要做的的if/else if/else語句在sign並相應地調整answer

if (sign == '+') { 
    answer = x + y; 
} else if (sign == '-') { 
    answer = x - y; 
} else if (sign == '*') { 
    answer = x * y; 
} else { 
    System.err.println("Unknown operator: " + sign); 
} 

或者,你可以使用一個switch聲明:

switch(sign) { 
case('+'): 
    answer = x + y; 
    break; 
case('-'): 
    answer = x - y; 
    break; 
case('*'): 
    answer = x * y; 
    break; 
default: 
    System.err.println("Unknown operator: " + sign); 
    break; 
} 
+0

真棒,它的工作。謝謝。 – BEngleman

2

在這裏你去

public static void main(String args[]) { 
    Scanner keyboard = new Scanner(System.in); 
    int i, x, y, z, answer, a, counter = 0; 
    char sign; 
    for (i = 0; i < 10; i++) { 
     x = (int) (Math.random() * 10); 
     y = (int) (Math.random() * 10); 
     a = (int) (Math.random() * 3);//random sign 

     if (a == 0) { 
      sign = '+'; 
     } else if (a == 1) { 
      sign = '-'; 
     } else { 
      sign = '*'; 
     } 

     System.out.print(x + " " + sign + " " + y + " = "); 
     z = keyboard.nextInt(); 


     //Using Ternary operator 
     answer = sign == '+' ? (x +y) : (sign == '-' ? (x-y) : x * y); 

     System.out.println(answer); 

     if (z == answer) { 
      System.out.println("Correct"); 
      counter++; 
     } else { 
      System.out.println("Wrong"); 
     } 
    } 
    System.out.println("You answered " + counter + " questions correctly"); 
} 
+0

謝謝,我確定這也可以 – BEngleman