Results 1 to 3 of 3
  1. #1
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Exclamation age: using if statements

    i have created the follwoing program

    Java Code:
    import java.util.scanner
    class age{
    
    public static void main (String[] args){
    
    scanner in = new scanner (system.in)
    
    int x= in.nextInt();
    if(x>5 && x<=11)
    System.out.println("you are in primary school");
    if(x>11 && x<16) 
    System.out.println ("you are in secondary school");
    if(x>16 && <=18)
    System.out.println("you are in 6th form");
    if(x>18 && <=21)
    System.out.println("you are in university");
    if(x>21)
    System.out.println("you should be in work");
    }
    }

    the code above i created a was a set of if statements, the program compiles sucessfully, but as i enter the age for e.g 7 it says you are in primary school, but when i enter the age between 17 + it does not execute second, third or fifth if statements with the message, please help me with a solution

    thank you very much.
    Last edited by Fubarable; 10-04-2009 at 10:42 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure what you posted, but it isn't code that compiles. You may wish to repost the actual code. I've also added code tags this time around to help make your code easier to read.

    Much luck.

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Welcome!

    Welcome Yasmin, to Java Forums! ;)

    Your program didn't compile, so I fixed it for you. Java is case-sensitive so the scanner class is called Scanner. Other than this, just a few typos. You forgot the x in the last few if statements. Anyways, here's the code:
    Java Code:
    import java.util.*;
    
    class age {
    	public static void main (String[] args){
    	
    		Scanner in = new Scanner(System.in);
    	
    		int x= in.nextInt();
    		if(x > 5 && x <= 11)
    			System.out.println("you are in primary school");
    		if(x > 11 && x <= 16)
    			System.out.println ("you are in secondary school");
    		if(x > 16 && x <= 18)
    			System.out.println("you are in 6th form");
    		if(x > 18 && x <= 21)
    			System.out.println("you are in university");
    		if(x > 21)
    			System.out.println("you should be in work");
    	}
    }
    Try to use code tags: [code]your code here[/code]

    Hope this helps Yasmin. ;)
    Last edited by tim; 10-04-2009 at 10:57 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Looping if statements
    By rice in forum New To Java
    Replies: 10
    Last Post: 10-02-2009, 03:50 AM
  2. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 05:24 PM
  3. While and do while statements questions
    By hungdukie in forum New To Java
    Replies: 39
    Last Post: 12-10-2008, 03:08 PM
  4. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 10:53 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •