Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with if statement.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with if statement.

    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.

    Do you like add or subtract?add
    What is the result of:29+13 44
    Try again
    The supposed is asking him subtraction not addition.




    Any help friends?
    Thanks in advance.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Need help with if statement.

    Hello dreamer!
    if(like== "add")
    You should use the String's equals() method instead of the == operator when comparing Strings.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with if statement.

    Thank you so much andreas90.

    it really helped me out...

    Thanks again andreas90.

Similar Threads

  1. not a statement?
    By frozen java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 23rd, 2011, 08:57 PM
  2. If statement help
    By Legion of Daughters in forum Loops & Control Statements
    Replies: 12
    Last Post: September 6th, 2011, 08:25 AM
  3. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM
  4. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM
  5. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM