Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>here is the one approach to your problem:</p> <pre><code>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; } </code></pre> <p>and here is a little bit another getCorrectAnswer part:</p> <pre><code>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&lt;String, IMyOperator&gt; operators = new HashMap&lt;String, IMyOperator&gt;(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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload