简体   繁体   English

Java:如何从string和int计算答案

[英]Java: How to calculate answer from string and int

So I have recently started to code a calculator which will provide two random numbers from 1-50 and either a *, + or - symbol. 所以我最近开始编写一个计算器,它将提供1-50的两个随机数和一个*, +-符号。 However I'm not so sure on how to actually check if the answer a user has inputted is correct as I can't actually calculate an answer. 但是,我不太确定如何实际检查用户输入的答案是否正确,因为我实际上无法计算答案。 Any help would be great thanks. 任何帮助都会非常感谢。 Code below (also sorry for the lack of annotations) 下面的代码(也很抱歉没有注释)

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Hello, what is your name?"); //Asks the childs name
    Scanner demo = new Scanner(System.in); //creates the scanner
    String a = demo.nextLine();
    System.out.println("Hi " + a + " I hope your ready for the quiz");
    System.out.println("Lets begin");


    String [] arr = {"*", "+", "-"};
    Random random = new Random();
    Random no = new Random();
    for(int counter1= 1; counter1 <=1;counter1++){
    int select = random.nextInt(arr.length);
    int firstnumber;
    for(int counter= 1; counter <=1;counter++){
        firstnumber = no.nextInt(50);
        int firstnumber2;
        firstnumber2 = no.nextInt(50);
        System.out.println(firstnumber + " " + arr[select] + " " + firstnumber2);

        int b = demo.nextInt();

You must use if (or switch ) in order to find out on how to calculate the input. 您必须使用if (或switch )以了解如何计算输入。

int result = 0;
if (arr[select].equals("*")
    result = firstnumber * secondnumber; // do not call it firstnumber2
else if (arr[select].equals("+")
    result = firstnumber + secondnumber;
else // if (arr[select].equals("-")) - else if not needed if only three elements are used
    result = firstnumber - secondnumber;

switch : switch

switch(arr[select]) {
    case "*": result = firstnumber * secondnumber;
              break;
    case "+": result = firstnumber + secondnumber;
              break;
    case "-": result = firstnumber - secondnumber;
              break;
    default: break;
}

There are several approaches to this. 有几种方法可以解决这个问题。 A good style would be the following: 一个好的风格将是以下:

@FunctionalInterface
interface CalcFunction{
    int calc(int a , int b);
}

HashMap<String , CalcFunction> operations = new HashMap<>();
operations.put("*" , (a , b) -> a * b);
operations.put("/" , (a , b) -> a / b);
operations.put("+" , (a , b) -> a + b);
operations.put("-" , (a , b) -> a - b);

//select a random operation
String op = generateOperation();

//generate operands
int a = randomNumber();
int b = randomNumber();

//the correct result
int expected = operations.get(op).calc(a , b);

The advantage of this approach is that it's easily extensible by additional operations. 这种方法的优点是它可以通过额外的操作轻松扩展。

EDIT: 编辑:
The basic idea is to map each of the expressions "*", "/", "+", "-" to an instance of CalcFunction that implements exactly that operation in calc . 基本思想是将每个表达式“*”,“/”,“+”,“ - ” CalcFunctionCalcFunction的实例,该实例在calc完全实现该操作。 Since CalcFunction is a Functional Interface , it can be expressed by a lambda. 由于CalcFunction是一个Functional Interface ,它可以用lambda表示。

(a , b) -> a * b

For example could aswell be expressed as 例如,也可以表达为

new CalcFunction(){
    int calc(int a , int b){
        return a * b;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM