在 Java 中生成随机数学运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14407034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:10:43  来源:igfitidea点击:

Generate random math operator in Java

java

提问by Perception

I was wondering how I should initialize int c from the switch statement. I can't figure out how to "extract" the random switch-argument into the final answer. I am a beginner so maybe I am missing something obvious here. Here is the code that I have so far:

我想知道我应该如何从 switch 语句初始化 int c 。我不知道如何将随机切换参数“提取”到最终答案中。我是初学者,所以也许我在这里遗漏了一些明显的东西。这是我到目前为止的代码:

import java.util.*;
public class whileTest{
public static void main (String args[]){
    Scanner sc = new Scanner(System.in);

    String a;

        do{

        String operatorSwitch;
        int b;
        int c;

        Random number = new Random();
        int d = number.nextInt(100) +1;
        int e = number.nextInt(100) +1;

        Random operatorChoice = new Random();
        int operator = operatorChoice.nextInt(4);

        switch (operator){

            case 0: operatorSwitch= "+";
                c = d+e;
                break;
            case 1: operatorSwitch= "-";
                c = d-e;
                break;
            case 2: operatorSwitch= "*";
                c = d*e;
                break;
            case 3: operatorSwitch= "/";
                c = d/e;
                break;
            default: operatorSwitch= "";
        }
        System.out.println("What is: "+d+operatorSwitch+e+"?");
        b = sc.nextInt();


        if(b!=c)
            System.out.println("Wrong answer! Right answer is: "+c);
        else{if(b==c)
            System.out.println("Right answer!"+c);
        }
            System.out.println("Continue? y/n");
            a = sc.next();

        }while(a.equals("y"));
    }
}

回答by Perception

As a practice, unless you have a good reason not too, you should always explicitly initialize your variables. In this case you want to change this:

作为一种实践,除非您有充分的理由不这样做,否则您应该始终显式初始化您的变量。在这种情况下,您要更改此设置:

String operatorSwitch;
int b;
int c;

To this:

对此:

String operatorSwitch = null;
int b = 0;
int c = 0;

The problem with your current code is a compilation one, due to the variable 'c' possiblynot being initialized.

您当前代码的问题是编译问题,因为变量 'c'可能未初始化。

回答by flup

Not sure how pretty you wish to make this but you could write an Operator interface with anonymous subclasses and pick a random index from those.

不确定您希望这样做有多漂亮,但您可以编写一个带有匿名子类的 Operator 接口,并从中选择一个随机索引。

interface Operator{
    public int operateOn( int a, int b );
    public char getDisplayChar();
}

Operator[] operators = {new Operator(){
        public int operateOn(int a, int b){
            return a + b;
        }

        public char getDisplayChar(){
            return '+';
        }
    }, new Operator(){
        public int operateOn(int a, int b){
            return a - b;
        }

        public char getDisplayChar(){
            return '-';
        }
    } // etc.
    };