0

I'm doing a program that presents the student with a math quiz. I am having trouble figuring out how to take the input problem type and turning that string into the arithmetic operator. Here is the method for that part of the code. Please and thanks!

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;

Update Here's the entire code if it helps see what I am doing.

    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() {
}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Jarod Wood
  • 1
  • 1
  • 4
  • 5
  • What do you want to do? If you have `someNumber1` and `someNumber2`, and you want to sum them, `if("+".equals(choice)) { return (someNumber1 + someNumber2); }` – nhgrif Oct 25 '13 at 01:48
  • I honestly don't even think you know what your doing. haha. Ill give an answer with an example of everything your trying to do. just give me a minute – Zeveso Oct 25 '13 at 01:53

2 Answers2

0

First, I want to show you how to test to see if it is what you want.

You can always check to make sure it is a string, or int, or w/e by doing the following.

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

Here is code for what you want to do.

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;
        }
    }
}
Zeveso
  • 1,274
  • 3
  • 21
  • 41
0

here is the one approach to your problem:

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

and here is a little bit another getCorrectAnswer part:

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);
}
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57