Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing user-inputted characters in If/Switch statements
    primarykey
    data
    text
    <p>I am taking my first Java class, and am having a hard time finishing this assignment.</p> <p>The assignment itself is quite simple. All I have to do is prompt the user to input an integer, a character, then another integer. </p> <p>If the character inputted is +, the two integers should be added together. If the character inputted is -, the two integers should be subtracted. Etc...</p> <p><strong>The problem is that I do not know how to accept a character from the user, then use it in my If/Switch statements.</strong></p> <p>Here are my prompts:</p> <pre><code>// Prompt the user to enter an integer System.out.print("Please enter an integer number:"); int int1 = input.nextInt(); // Prompt the user to enter a character // Since I could not use characters, I am using integers to represent them. System.out.print("Enter 0 for +, 1 for -, 2 for /, 3 for %, and 4 for *"); int char1 = input.nextInt(); // Prompt the user to enter another integer System.out.print("Please enter another integer number:"); int int2 = input.nextInt(); </code></pre> <p>Here are my If/Switch statements: (I know they are redundant, but it's part of the assignment) </p> <pre><code>if (char1 == 0) { int ans = int1 + int2; System.out.println(int1 + " + " + int2 + " = " + ans); } else if (char1 == 1) { int ans = int1 - int2; System.out.println(int1 + " - " + int2 + " = " + ans); } else if (char1 == 2) { int ans = int1 / int2; System.out.println(int1 + " / " + int2 + " = " + ans); } else if (char1 == 3) { int ans = int1 % int2; System.out.println(int1 + " % " + int2 + " = " + ans); } else if (char1 == 4) { int ans = int1 * int2; System.out.println(int1 + " * " + int2 + " = " + ans); } else System.out.println("Invalid operator"); int result = 0; switch (char1) { case 0: result = int1 + int2; System.out.println(int1 + " + " + int2 + " = " + result); break; case 1: result = int1 - int2; System.out.println(int1 + " - " + int2 + " = " + result); break; case 2: result = int1 / int2; System.out.println(int1 + " / " + int2 + " = " + result); break; case 3: result = int1 % int2; System.out.println(int1 + " % " + int2 + " = " + result); break; case 4: result = int1 * int2; System.out.println(int1 + " * " + int2 + " = " + result); break; default: System.out.println("Invalid operator"); } </code></pre> <p>Everything else works just fine. I'm just trying to change from using 0 for +, 1 for -, to just using +, -, /, %, and *.</p>
    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.
 

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