First of all, thanks Eran and T.J. Crowder for pointing out my mistake but I have a different problem now.
Scanner scan = new Scanner (System.in);
int sum, HighRes, LowRes, Agg, Ave, Amount, Wrong, Right;
String operator, another = "y", num1, num2;
HighRes = 0;
LowRes = 0;
Agg = 0;
Amount = 0;
DecimalFormat df = new DecimalFormat("#.00");
Wrong = 0;
Right = 0;
while (another.equalsIgnoreCase("y"))
{
System.out.println("Enter the first number, second number and the operator (+, -, * or /)");
num1 = scan.next();
num2 = scan.next();
operator =scan.next();
System.out.println( "Expression is " + num1 + " " +num2 + " " +operator);
if (num1.equals("-") || num1.equals("+") || num1.equals("*") || num1.equals("/") || num1.equals("<<") || num1.equals(">>") || num1.equals("%") || num1.equals("&") || num1.equals("|") ||num2.equals("-") || num2.equals("+") || num2.equals("*") || num2.equals("/") || num2.equals("<<") || num2.equals(">>") || num2.equals("%") || num2.equals("&") || num2.equals("|")) {
System.out.println("Invalid expression");
System.out.print ("Test another expression (Type Y if yes and leave empty if no?) ");
another = scan.nextLine();
Wrong++;
}
else if (num1 != "-" || num1 != "+" || num1 != "*" || num1 != "/" || num2 != "+" || num2 != "+" || num2 != "+" || num2 != "+") {
int num3 = Integer.parseInt(num1);
int num4 = Integer.parseInt(num2);
if (num3 > -1000000000 && num3 < 1000000000 && num4 > -1000000000 && num4 < 1000000000){
if (operator.equals("+")) {
sum = num3 + num4;
System.out.println ("" + num3 + " " + operator + " " + num2 + " = " + sum);
System.out.print ("Test another expression (Type Y if yes and leave empty if no?) ");
another = scan.nextLine();
Agg = Agg + sum;
Amount++;
Right++;
if (sum > HighRes) {
HighRes = sum;
}
if (sum < LowRes){
LowRes = sum;
}
}
else if (operator.equals("-")) {
sum = num3 - num4;
System.out.println ("" + num3 + " " + operator + " " + num2 + " = " + sum);
System.out.print ("Test another expression (Type Y if yes and leave empty if no?) ");
another = scan.nextLine();
Agg = Agg + sum;
Amount++;
Right++;
if (sum > HighRes) {
HighRes = sum;
}
if (sum < LowRes){
LowRes = sum;
}
}
This is my code from my previous question, I no longer get an error, but, when I enter a correct expression, program continues to run and asks if I want to enter another expression. Correct But, when I enter incorrect one, it displays that it's incorrect and asks for a new expression but it immediately terminates.Incorrect (Evaluation is at the end of the program)
Edit: It's not the problem with the "else if", it's the "if" that does not wait for the response to the question if I want to test another expression.
Aucun commentaire:
Enregistrer un commentaire