Java如何使用指定的表达式计算变量的值?

时间:2017-05-26 01:55:13

标签: java variables

我正在学习" 通过游戏学习Java "由Lubomir Stanchev提出,我无法解决这个问题。

我们将随机要求玩家添加,乘以或减去一位数字(我们跳过除法,因为除以两个数字的结果不是整数)。游戏应该询问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&amp; y是整数,而变量符号是char。我来自JS,所以我发现这很奇怪。在JS中,无论类型如何,表达式都会自动计算或连接。我在第3章中,所以我还没有研究过parseInt这件事,我认为这件事不适用于char。

感谢您的帮助。

现在是我更新的答案:

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");
    }
}

确实编译了!!!

2 个答案:

答案 0 :(得分:5)

您需要在sign上执行if / else if / else并相应地调整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;
}

答案 1 :(得分:1)

你去吧

 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");
}