开发者

saving operators in arrays, error

To give an idea of what I want elaborate with this code is the following:

  1. Enter two numbers: 10 7
  2. Choose Operator: e.g. (+, - , * and /)
  3. What's 10 * 7?
  4. Correct!

    int[] arr = new int[5];
    
    
    System.out.println("enter two numbers: ");
    arr[1] = sc.nextInt();
    arr[2] = sc.nextInt();
    
    
    System.out.println("Choose Operator: ");
    arr[3] = sc.nextInt();
    
    
    int operator = arr[1]+arr[3]+arr[2];
    
    
    System.out.print("what's "+operator);
    int svar = sc.nextInt();
    
    
    if (svar == operator)
        System.out.println("Correct!");
    else
        System.out.pr开发者_JAVA百科intln("Wrong - the right answer is "+operator);    
    

Now I'm having a problem with running some aspects within this code. It works fine to compile but each time the program asks for to "choose operator" the compiler responses with the following error:

  • Exception in thread "main"
    • java.util.InputMismatchException at
    • java.util.Scanner.throwFor(Unknown
    • Source) at
    • java.util.Scanner.next(Unknown
    • Source) at
    • java.util.Scanner.nextInt(Unknown
    • Source) at
    • java.util.Scanner.nextInt(Unknown
    • Source)
    • at test1.main(test1.java:13)

I wonder how I'm going to deal with this one. But the goal would be to "save" the desired operator, and then put it together with arr[1] and arr[2] (shown in int operator) to "sort of" create this whole mathematical operation. But the error occurs when I choose one particular operator.

I would appreciate some help with this one. Thank you!


Firstly, you are trying to use ints and Strings interchangeably, which is not possible in a strongly-typed language such as Java.

Secondly, for this kind of calculator application you should be using a stack. For easiest implementation, have one stack for numbers and one stack for operators.


You're calling nextInt, which tries to read an integer.
Since you aren't entering an integer, you're getting an error.

To implement your idea, you would need an Operator interface with an int execute(int x, iny y) method, and a separate class for each operator.
You would then read a character from sc and find the corresponding Operator implementation for that character. (perhaps using a Map<String, Operator>)


Here

System.out.println("Choose Operator: ");
    arr[3] = sc.nextInt();

you are trying to store a '+' operator as an int, maybe you should read a line and then determine what to do with the read in input.


All you need is two Ints, one Char and one Double, if it's not a must to use arrays so this is the code for you:

int num1, num2;
double result;
char op;

System.out.println("enter two numbers: ");
num1 = sc.nextInt();
num2 = sc.nextInt();

System.out.println("Choose Operator: ");
op = System.in.read();  // Edited

switch ( op )
{
     case '+': res = num1 + num2; break;
     case '-': res = num1 - num2; break;         
     case '/': res = num1 / num2; break;
     case '*': res = num1 * num2; break;
}    

System.out.print("what's " + num1 + op + num2 );

if ( sc.nextInt() == res )
    System.out.println("Correct!");
else
    System.out.println("Wrong - the right answer is " + res );    

Enjoy, Rotem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜