How do I solve a problem using both If and Switch statements in the same program?
-
Hi, I am taking my first Java class and struggling with this assignment because the compiler doesn't seem to like redundancy. The assignment itself is quite simple. All I have to do is prompt the user to input an integer, a character, then another integer. If the character inputted is +, the two integers should be added together. If the character inputted is -, the two integers should be subtracted. Etc... I am supposed to achieve this using If statements and then a Switch statement (in the same program). This is kind of silly considering you would never solve the same thing twice using different methods in real life, but it's part of the assignment. When I use the If and Switch statements by themselves, my program works just fine. But when I try to have them working both at the same time, the compiler keeps saying "variable is already defined in method main(string[])" How can I fix this? Here is my code: import java.util.Scanner; public class IfSwitch { public static void main(String[] args) { //Create a scanner Scanner input = new Scanner(System.in); //Prompt the user to enter an integer, single character, then another integer System.out.print("Please enter an integer number:"); int int1 = input.nextInt(); // Using int instead of char b/c I don't know how to use inputted char System.out.print("Enter 0 for +, 1 for -, 2 for /, 3 for %, and 4 for *"); int char1 = input.nextInt(); System.out.print("Please enter another integer number:"); int int2 = input.nextInt(); 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"); switch (char1) { case 0: int ans = int1 + int2; System.out.println(int1 + " + " + int2 + " = " + ans); break; case 1: int ans = int1 + int2; System.out.println(int1 + " - " + int2 + " = " + ans); break; case 2: int ans = int1 + int2; System.out.println(int1 + " / " + int2 + " = " + ans); break; case 3: int ans = int1 + int2; System.out.println(int1 + " % " + int2 + " = " + ans); break; case 4: int ans = int1 + int2; System.out.println(int1 + " * " + int2 + " = " + ans); default: System.out.println("Invalid operator"); } } } Thanks a bunch for your help!
-
Answer:
Inside the switch block you have declared variable result several times. Use this code instead: package study; import java.util.Scanner; public class IfSwitch { public static void main(String[] args) { //Create a scanner Scanner input = new Scanner(sytem Resources and Information. This website is for sale!); //Prompt the user to enter an integer, single character, then another integer System.out.print("Please enter an integer number:"); int int1 = input.nextInt(); System.out.print("Enter 0 for +, 1 for -, 2 for /, 3 for %, and 4 for *"); int char1 = input.nextInt(); System.out.print("Please enter another integer number:"); int int2 = input.nextInt(); 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 = -1; 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); default: System.out.println("Invalid operator"); } } } Few suggestions If you are not able to figure out the error then just paste the code in eclipse which shows you the error immediately in RED. http://stackoverflow.com is good forum for such kind of questions. Happy coding :)
Harshal Jain at Quora Visit the source
Related Q & A:
- How do I solve the problem of downloading word document attached to my email?Best solution by support.google.com
- How do I solve a DNS error?Best solution by Yahoo! Answers
- How can I edit a picture using photoshop?Best solution by Yahoo! Answers
- How can I record a video using my webcam?Best solution by Answerbag.com
- How do I send a message using yahoo mail?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.