Results 1 to 3 of 3

Thread: While help?

  1. #1
    07.350 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default While help?

    I'm making a program that if you enter a whole number from 1 to 9 and 0 to exit. Code a pyramid structure by using only odd numbers. The pyramid needs to be centered without hard keying in spaces you must use a loop to print out the appropriate number of spaces to align the pyramid.

    The part I'm having trouble on is to get the loop to continue after one entry is submitted and then when the user enter a number >9 to give the error invalid entry. It works fine when a number <0 is entered and the invalid entry prints. Also I'm having trouble center aligning the pyramid without hard keying it in with spaces. any help would be great!!


    Java Code:
    import java.util.Scanner;
    public class Pyramid{
    	public static void main(String[] args )
    	{
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter a whole number from 1 to 9 <0 to quit>");
    		int number = input.nextInt();
     
    		    while(number<=9|| number<=1){
    		    	
    		
    			if(number==1||number==2){
    				System.out.println( 1 );
    				number=1;
    				break;
    			}
    			if(number==3||number==4){
    				System.out.println(  1  );
    				System.out.println(333);
    				number=3;
    				break;
    			}
    			if(number==5||number==6){
    				System.out.println(  1   );
    				System.out.println( 333 );
    				System.out.println(55555);
    				number=4;
    				break;
    			}
    			if(number==7||number==8){
    				System.out.println(   1   );
    				System.out.println(   333 );
    				System.out.println(  55555);
    				System.out.println(7777777);
    				number=7;
    				break;
    			}
    			if(number==9){
    				System.out.println(   1   );
    				System.out.println(   333 );
    				System.out.println(  55555);
    				System.out.println(7777777);
    				System.out.println(999999999);
    				number=9;
    				break;
    			}
    			if (number < 0) {
    			      System.out.println("number  is an invalid entry. Please try again");
    			      number= -1;
    			      break;
    			}      
    			if (number > 9) {
    				   System.out.println("number  is an invalid entry. Please try again");
    				   number=15;
    				   break;
    				   
    			}	   
    			if (number==0){
    				System.out.println("Thanks for playing");
    				break;
    			}	
    		    }
    			
    		    while(number !=0){
    				System.out.println("Enter a whole number from 1 to 9 <0 to quit>");
    				input.nextInt();
    				break;
    			
    			
    			           
    		 }
    }
    }

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: While help?

    First, you may want to use a switch-case statement.

    Java Code:
    switch(number)
    {
    	case 0:
    	//do something
    	break;
    	
    	case 1:
    	case 2:
    	//do something
    	break;
    	
    	case 3:
    	case 4:
    	//do something
    	break;
    	
    	case 5:
    	case 6:
    	//do something
    	break;
    	
    	/// ...and so on
    	
    	default:
    	//all other cases
    	break;
    }
    Second, you should try to determine how many spaces are needed on either side of the number. For example, if you need 1 space on both sides of a three you would do something like:

    Java Code:
    int spaces = 1;
    for(int i = 0; i < spaces; i++)
    {
    	System.out.print(' ');
    }
    for(int i = 0; i < number; i++)
    {
    	System.out.print(number);
    }
    for(int i = 0; i < spaces; i++)
    {
    	System.out.print(' ');
    }
    System.out.println();
    I hope this was of some help.
    Last edited by AndrewM16921; 02-21-2013 at 06:03 AM.

  3. #3
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: While help?

    Ok do you think your while loop condition is right??
    Java Code:
    while(number<=9|| number<=1) // I think it should be number<=9|| number>=1
    Secondly you are using while loop two times... i prefer you to use do while if you have studied it.........

Posting Permissions

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