Possible Duplicate:
In java, in this program it suddenly stops running properly at a certain point of code? but it compiles? any ideas what could be the issue?
import java.io.IOException;
import java.util.*;
public class task2 {
public static void main (String [] args) throws IOException {
int a;
int b;
String y;
String x;
Scanner input = new Scanner(System.in);
System.out.println("Please enter number A:");
a = input.nextInt();
System.out.println("\nPlease enter number B:");
b = input.nextInt();
System.out.println("\nLastly, enter A if you wish it to be the dividor and/or subtractor, or if you wish it to be B, please enter B :");
y=input.next();
System.out.println("\nWhat would you like to do? Multiply (*), Divide (/), Subtract (-) or Add (+)? Please enter the symbol of which process you would like to have completed:");
x=input.next();
if (y.equals("b"+"B")) {
if (x.equals("*")) {
System.out.println("\nThe product of these numbers is:" + (a*b));}
else if (x.equals("/")) {
System.out.println("\nThe quotient of these numbers is:" + (a/b));}
else if (x.equals("+")) {
System.out.println("\nThe sum of these numbers is:" + (a+b));}
else if (x.equals("-")) {
System.out.println("\nThe difference of these numbers is:" + (a-b));
}
}
else
if (y.equals("a"+"A")){
if (x.equals("*")) {
System.out.println("\nThe product of these numbers is:" + (b*a));}
else if (x.equals("/")) {
System.out.println("\nThe quotient of these numbers is:" + (b/a));}
else if (x.equals("+")) {
System.out.println("\nThe sum of these numbers is:" + (b+a));}
else if (x.equals("-")) {
System.out.println("\nThe difference of these numbers is:" + ((b-a)));
}
}
}
}
A small program to do calculations on a set of two numbers. What I want is the user to enter the numbers and type of operation (including order) they want.
The result of the calculation does not display? any ideas? all help greatly appreciated!
You’re comparing y and x against A+a and B+b
What you should do is :
(y.equals("B") || y.equals("b"))Use the pipes
||as anORstatement and not concatenating them with+Here’s the working code.