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 7 of 7

Thread: Else-if statment failure

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Else-if statment failure

    Ok, Ive been working on this,
    import java.util.Scanner;
    public class Levels 
    {
    	public static void main(String[] args)
    	 {
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Pick a number 1 - 10:");
    		int WinnerPoints;
    		WinnerPoints = 0;
    		System.out.println("Your current amount of Points is " + WinnerPoints);
    		int numberPicked = keyboard.nextInt();
    		switch (numberPicked)
    		{
    		case 1:
    		case 2:
    		case 3:
    		case 4:
    		case 5:
    		case 6:
    		case 8:
    		case 9:
    		case 10:
    			System.out.println("Sorry, you lost :(");
    			break;
    		case 7:
    			System.out.println("YAY! You Won!");
    			WinnerPoints = 0+5;
    			System.out.println("Welcome to level 2!");
    			break;
    			default:
    				System.out.println("Thats not 1 - 10...");
    				break;
    				}
    				System.out.println("Your Current Amount of Points is " + WinnerPoints);
    				if (WinnerPoints > 5)
    					System.out.println("Begin Test..");
    				else if (WinnerPoints == 0)
    					System.out.println("Good Bye");
     
     
    }
    }
    But at the very end where it displays:
    	if (WinnerPoints > 5)
    					System.out.println("Begin Test..");
    				else if (WinnerPoints == 0)
    					System.out.println("Good Bye");
    This does not work, my IDE says there is no error, but the code won't work, what do I do?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Else-if statment failure

    Won't work in what way? nothing is printed? don't get the desired results? what are the desired results?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Else-if statment failure

    Quote Originally Posted by newbie View Post
    Won't work in what way? nothing is printed? don't get the desired results? what are the desired results?
    Ok, this code here:
    	if (WinnerPoints > 5)
    					System.out.println("Begin Test..");
    				else if (WinnerPoints == 0)
    					System.out.println("Good Bye");
    Is supposed to print out: "Begin Test" if you have 5 WinnerPoints, but if you have 0, it is supposed to say "Good Bye" but this whole statement isn't being recognized by the program.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Else-if statment failure

    Because the condition is, if WinnerPoints is GREATER than 5; print message.
    WinnerPoints can never exceed 5 in your program.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Else-if statment failure

    Quote Originally Posted by newbie View Post
    Because the condition is, if WinnerPoints is GREATER than 5; print message.
    WinnerPoints can never exceed 5 in your program.
    Ohh I see, but I fixed it another way, just put a System.exit command under the losing numbers,

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Else-if statment failure

    System.exit isn't the best approach to take, easier and better to change the condition to equal to or greater than.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Else-if statment failure

    Quote Originally Posted by newbie View Post
    System.exit isn't the best approach to take, easier and better to change the condition to equal to or greater than.
    Ok, thanks for the help, I will go back and make it run smoother once I finish my project.

Similar Threads

  1. [SOLVED] My string is not activating my if statment.
    By ZeroLRS in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2011, 06:11 PM
  2. Communications Link Failure
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: June 14th, 2011, 07:03 PM
  3. [SOLVED] JSCH Auth Failure
    By techwiz24 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 30th, 2011, 05:17 PM
  4. Switch Statment
    By 3XiLED in forum Java Theory & Questions
    Replies: 2
    Last Post: March 31st, 2010, 05:11 PM
  5. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 12:37 PM