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

Thread: Problems with a "if-then and if-then-else" statements

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problems with a "if-then and if-then-else" statements

    import java.util.*;
    import java.io.*;
    public class TEster {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    			int x;
    			char Guess;
    			Scanner Number_input = new Scanner( System.in );
    			System.out.println("Enter a number: ");
    			x = Number_input.nextInt();
    		if (x >=50) {
    			Guess = 'Y';
    		} else if (x <=50) {
    			Guess = 'N';		
    	System.out.print("Number:" + Guess);
    		}
     
     
     
    		}
     
     
    	}

    Why won't Y or N print when i enter a number? I can input a number but after that nothing happens, Y or N should print for "Guess" right?


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Problems with a "if-then and if-then-else" statements

    import java.util.*;
    import java.io.*;
    public class TEster {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    			int x;
    			char Guess;
    			Scanner Number_input = new Scanner( System.in );
    			System.out.println("Enter a number: ");
    			x = Number_input.nextInt();
    		if (x >=50) {
    			Guess = 'Y';
    		} 
                    else if (x <=50) {
    	                Guess = 'N';		
    	                System.out.print("Number:" + Guess);
    		}
            }        
    }

    If you look at your if/else if statement, you'll see your problem. What happens when you enter a number higher than 50? What happens when you enter a number lower than 50? What happens when you enter 50 itself? I suggest reading The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics) for more info about it.

Similar Threads

  1. Replies: 1
    Last Post: December 15th, 2011, 08:29 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Problems Making a "POST" to a Socket
    By haroldjclements in forum Java Networking
    Replies: 0
    Last Post: October 11th, 2011, 05:28 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM