I want to write a program that ask the student about his favorite mathematical operations “addition or subtraction”. " I will use Scanner input in this step"
If he write addition as an answer for the question then “ I will ask him to subtract two random numbers “.
If he write subtraction as an answer for the question then “I will ask him to add two random numbers “.
I wrote the program below:
import java.util.Scanner;
public class gradee {
public static void main(String[] args) {
int num1=(int)(Math.random()*100);
int num2=(int)(Math.random()*100);
Scanner input= new Scanner(System.in);
//ask him whether he like to add or subtract
System.out.print("Do you like add or subtract?");
String like=input.next();
if(like== "add"){
System.out.print("What is the result of:"+num1+"-"+num2);
int answer=input.nextInt();
if(num1-num2==answer)
System.out.print("That is right");
else
System.out.print("Try again");
}
else{
System.out.print("What is the result of:"+num1+"+"+num2);
int answer1=input.nextInt();
if(num1+num2==answer1)
System.out.print("That is right baby");
else
System.out.print("Try again");
}
}
}
The problem that I am facing is that:
The program will execute else statement anyway:
for example:
This is the output when the student said he like addition.
The supposed is asking him subtraction not addition.Do you like add or subtract?add
What is the result of:29+13 44
Try again
Any help friends?
Thanks in advance.