【问题标题】:Java: How to calculate answer from string and intJava:如何从字符串和整数计算答案
【发布时间】:2016-04-17 05:41:20
【问题描述】:

所以我最近开始编写一个计算器,它将提供两个 1-50 的随机数和一个 *, +- 符号。但是我不太确定如何实际检查用户输入的答案是否正确,因为我实际上无法计算答案。任何帮助都会非常感谢。 下面的代码(也很抱歉缺少注释)

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();

【问题讨论】:

    标签: java eclipse string int boolean


    【解决方案1】:

    您必须使用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(arr[select]) {
        case "*": result = firstnumber * secondnumber;
                  break;
        case "+": result = firstnumber + secondnumber;
                  break;
        case "-": result = firstnumber - secondnumber;
                  break;
        default: break;
    }
    

    【讨论】:

    • 如果他使用 java 1.7 或更高版本,他也可以使用带有字符串的 switch 语句;)
    • 哦,谢谢,我什至不知道。 oO我会将其添加到我的答案中。 :)
    • 因为提到firstnumber2 应该被称为secondnumber而被赞成
    【解决方案2】:

    有几种方法可以解决这个问题。一个好的样式如下:

    @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);
    

    这种方法的优点是可以通过其他操作轻松扩展。

    编辑:
    基本思想是将每个表达式“*”、“/”、“+”、“-”映射到CalcFunction 的一个实例,该实例在calc 中实现了该操作。由于CalcFunctionFunctional Interface,所以可以用lambda 表示。

    (a , b) -> a * b
    

    例如也可以表示为

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

    【讨论】:

    • 您的回答很好,但它可以使用更多解释,因为 OP 显然是初学者,特别是因为您使用的是相当复杂的构造,例如 lambda 和函数式接口。
    • @mhlz 我已经用(希望有用的)解释编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 2015-03-14
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多