Quote (Minkomonster @ Jan 25 2015 10:15pm)
As long as you are showing a desire to learn, I will match that desire to teach.
One thing I see wrong is your else is not wrapping a block statement
wrap everything under your else in braces { }
Go back and look at the example I posted and compare it with what you have
Okay, I might have took a lazy way out. I had that wrap fixed, but what I did was this:
Code
import java.util.Scanner;
public class Calc {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Menu: Welcome to Basic Calculator \n "
+ "1. Add \n "
+ "2. Subtract \n "
+ "3. Divide \n "
+ "4. Multiply \n "
+ "5. Generate Random Number \n "
+ "6. Quit \n ");
//Menu for Operations
String in = input.next();
char oper = in.charAt(0);
if (oper == '6') {
System.out.println("Your have chosen to Quit. Thank you for using Basic Calculator!");
System.exit(0);
}
else
if (oper == '1') {
in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);
System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 + num2;
System.out.println("Your answer is: " + result);
} else if(oper == '5') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);
System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = (num1 * num2 + num1 + num2 + 77);
System.out.println("Your answer is: " + result);
} else if(oper == '2') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);
System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 - num2;
System.out.println("Your answer is: " + result);
} else if(oper == '4') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);
System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 * num2;
System.out.println("Your answer is: " + result);
} else if(oper == '3') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);
System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 / num2;
System.out.println("Your answer is: " + result);
}
else
System.out.println("Sorry, you have entered an invlaid option. Please try again");
return;
}
{
}
}
This looks like bad coding etiquette, but it's the only way I knew how to complete the code..
The only left for me to do is loop the "else, try again" output, with a return(possibly?) and give them 3 times before I force quit the app... I'm looking into that right now. I know most of what I need to know about loops, but unsure of how to start the process of returning to a Start menu in order to ask the user for his input again.