将字符串转换为算术运算

时间:2013-10-25 01:45:51

标签: java string math replace

我正在做一个向学生提供数学测验的程序。我无法弄清楚如何获取输入问题类型并将该字符串转换为算术运算符。以下是该部分代码的方法。拜托,谢谢!

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() {
}

2 个答案:

答案 0 :(得分:0)

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

您可以通过执行以下操作来检查以确保它是字符串,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;
        }
    }
}

答案 1 :(得分:0)

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

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