【问题标题】:convert string to arithmetic operation [duplicate]将字符串转换为算术运算[重复]
【发布时间】:2013-10-25 01:45:51
【问题描述】:

我正在做一个向学生展示数学测验的程序。我无法弄清楚如何采用输入问题类型并将该字符串转换为算术运算符。这是该部分代码的方法。请和谢谢!

public static String getUserChoice(String choice) {
    Scanner in = new Scanner(System.in);

    System.out.println("Please enter the symbol that corresponds to one of the following problems\n"
            + "Addition (+)\n Subtraction (-)\n or Multiplication (*): ");
    choice = in.next();
        if ("+".equals(choice)){
            return +;
        }
        }
    return choice;

更新 如果它有助于了解我在做什么,这是整个代码。

    public static void main(String[] args) {
    int digit = 0;
    int random = 0;

    String result1 = getUserChoice("");

    digit = getNumberofDigit1(digit);

    int number1 = getRandomNumber1(digit);
    int number2 = getRandomNumber2(digit);

    System.out.println(number1 + result1 + number2);
    getCorrectAnswer(number1, result1, number2);

}

public static String getUserChoice(String choice) {
    Scanner in = new Scanner(System.in);

    System.out.println("Please enter the symbol that corresponds to one of the following problems\n"
            + "Addition (+)\n Subtraction (-)\n or Multiplication (*): ");
    choice = in.next();


    return choice;
}

public static int getNumberofDigit1(int digit) {
    Scanner in = new Scanner(System.in);

    System.out.println("Enter a 1 for problems with one digit, or a 2 for two-digit problems: ");
    digit = in.nextInt();

    return digit;
}


public static int getRandomNumber1(int numbers) {
        int random = 0;
            if (numbers == 1) {
                random = (int) (1 + Math.random() * 9);
            } else if (numbers == 2) {
                random = (int) (10 + Math.random() * 90);
            }
         return random;   
}

public static int getRandomNumber2(int numbers) {
    int random2 = 0;
            if (numbers == 1) {
                random2 = (int) (1 + Math.random() * 9);
            } else if (numbers == 2) {
                random2 = (int) (10 + Math.random() * 90);
            }
            return random2;
}
public static void getCorrectAnswer(int number1, String result1, int number2) {
}

public static void getUserAnswer() {
    Scanner in = new Scanner(System.in);

}

public static void CheckandDisplayResult() {
}

【问题讨论】:

  • 你想做什么?如果你有someNumber1someNumber2,并且你想对它们求和,if("+".equals(choice)) { return (someNumber1 + someNumber2); }
  • 老实说,我什至不认为您知道自己在做什么。哈哈。我会用你试图做的一切的例子来回答。给我一分钟

标签: java string math replace


【解决方案1】:

这是解决问题的一种方法:

private static Scanner input;
public static void main(String[] args) {
    input = new Scanner(System.in);
    final String result1 = getUserChoice();
    final int digit = getNumberofDigit1();
    final int number1 = getRandomNumber(digit);
    final int number2 = getRandomNumber(digit);
    System.out.println(number1 + result1 + number2);
    final int userAnswer = input.nextInt();
    final int correctAnswer = getCorrectAnswer(number1, result1, number2);
    System.out.println( (userAnswer == correctAnswer) ? "Ok" : ("Wrong, right is: " + correctAnswer));
    input.close();
}

public static String getUserChoice() {
    System.out.println("Please enter the symbol that corresponds to one of the following problems\n" + "Addition (+)\n Subtraction (-)\n or Multiplication (*): ");
    return input.next();
}

public static int getNumberofDigit1() {
    System.out.println("Enter a 1 for problems with one digit, or a 2 for two-digit problems: ");
    return input.nextInt();
}

public static int getRandomNumber(final int numbers) {
    return (int) ( (numbers == 1) ? (1 + Math.random() * 9) : (10 + Math.random() * 90) );  
}

public static int getCorrectAnswer(final int number1, final String result1, final int number2) {
    if ("+".equals(result1))
        return number1+number2;
    else if ("-".equals(result1))
        return number1-number2;
    else if ("*".equals(result1))
        return number1*number2;
    return -1;
}

这里还有一点 getCorrectAnswer 部分:

public interface IMyOperator {
    public int operation(final int a, final int b);
};

static class classSum implements IMyOperator {
    public int operation(final int a, final int b) {
        return a+b;
    }
}

static class classSub implements IMyOperator {
    public int operation(final int a, final int b) {
        return a-b;
    }
}

static class classMul implements IMyOperator {
    public int operation(final int a, final int b) {
        return a*b;
    }
}

final static HashMap<String, IMyOperator> operators = new HashMap<String, IMyOperator>(3) {{
    put("+", new classSum());
    put("-", new classSub());
    put("*", new classMul());
}};

public static int getCorrectAnswer(final int number1, final String result1, final int number2) {
    return operators.get(result1).operation(number1, number2);
}

【讨论】:

    【解决方案2】:

    首先,我想告诉你如何测试它是否是你想要的。

    您始终可以通过执行以下操作来检查以确保它是字符串、int 或 w/e。

    Scanner scan = new Scanner(System.in);
    while(scan.hasNext()) {
      if(scan.hasNextInt()) {
        int response = scan.nextInt();
      }
    }
    

    这里是你想做的代码。

    import java.util.Random;
    import java.util.Scanner;
    
    public class Main {
    
        static Scanner in = new Scanner(System.in);
    
        public static void main(String[] args) {
            // Math Quiz v1.0 //
            String printme = "Please choose the quiz type:\n\n"
                    + "(1) Addition\n"
                    + "(2) Subtraction\n"
                    + "(3) Multiplication\n"
                    + "(4) Division\n"
                    + "(5) Modulus\n\n\n";
    
            System.out.println(printme);
    
            int reponse = in.nextInt();
    
            // Setup //
            int a = new Random().nextInt(100);
            int b = new Random().nextInt(100);
    
            int answer = -1;
    
            switch(reponse) {
                case 1:
                    System.out.println("What is " + a + " + " + b + "?");
                    answer = in.nextInt();
                    if(answer == a + b) {
                        System.out.println("Your right!");
                    } else {
                        System.out.println("You fail!");
                    }
                    break;
                case 2:
                    System.out.println("What is " + a + " - " + b + "?");
                    answer = in.nextInt();
                    if(answer == a - b) {
                        System.out.println("Your right!");
                    } else {
                        System.out.println("You fail!");
                    }
                    break;
                case 3:
                    System.out.println("What is " + a + " * " + b + "?");
                    answer = in.nextInt();
                    if(answer == a * b) {
                        System.out.println("Your right!");
                    } else {
                        System.out.println("You fail!");
                    }
                    break;
                case 4:
                    System.out.println("What is " + a + " / " + b + "?");
                    answer = in.nextInt();
                    if(answer == a / b) {
                        System.out.println("Your right!");
                    } else {
                        System.out.println("You fail!");
                    }
                    break;
                case 5:
                    System.out.println("What is " + a + " % " + b + "?");
                    answer = in.nextInt();
                    if(answer == a % b) {
                        System.out.println("Your right!");
                    } else {
                        System.out.println("You fail!");
                    }
                    break;
                default:
                    System.out.println("Error, enter an integer between 1 & 5.");
                    break;
            }
        }
    }
    

    【讨论】:

    • 为什么System.exit(0) 到处都是?对我来说似乎没有必要。
    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多