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: What is Wrong with my code

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post What is Wrong with my code

    well it works except it always shows the else part of the statement

    import java.util.Scanner;
    import java.util.Random;
     
    public class eightball
    	{
     
    	public static void main(String[] args)
    	{
    		Scanner scan = new Scanner(System.in);
    		Random random = new Random();
     
    		int z;
     
    		System.out.println("type in your yes or no question");
    		scan.nextLine();		
     
    		z = random.nextInt(20) + 1;
     
     
    		int w = (z);
    		System.out.println("the anser to your Question is ");
     
    		if (w < 4)
    		System.out.print("Yes.");
    		if (w < 8 && w > 4)
    		System.out.print(" No.");
    		if (w < 12 && w > 8)
    		System.out.print(" it could happen");
    		if (w < 16 && w > 12)
    		System.out.print(" can not tell");
    		else
    		System.out.print(" never");
     
    			}	
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: What is Wrong with my code

    Try

    import java.util.Scanner;
    import java.util.Random;
     
    public class eightball {
     
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		Random random = new Random();
     
    		int z;
     
    		System.out.println("type in your yes or no question");
    		scan.nextLine();
     
    		z = random.nextInt(20) + 1;
     
    		int w = (z);
    		System.out.println("the anser to your Question is ");
     
    		if (w < 4)
    			System.out.print("Yes.");
    		else if (w < 8 && w > 4)
    			System.out.print(" No.");
    		else if (w < 12 && w > 8)
    			System.out.print(" it could happen");
    		else if (w < 16 && w > 12)
    			System.out.print(" can not tell");
    		else
    			System.out.print(" never");
     
    	}
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 05:30 PM
  2. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  3. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM
  4. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 03:38 AM
  5. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM