Can anyone see where I am going wrong here?

Discussion in 'Programming' started by Trebor29, Apr 1, 2010.

  1. #1
    Hi, can anyone see where Im going wrong here? It all seems to work fine until it gets to the bottom where the user is asked if they would like to input another number (y) or (n). When (y) is selected it just keeps asking if they would like to input another number and doesn't go back around the loop!!! or it is going back around the loop, but bypassing the main part.
    Im not sure but I think it might be something to do with the logic of the second while loop, when the user says (y) its looping around and failing, and so jumping straight back to the ("Would you like to enter another number? Yes (y) or No (n).") question. Either that or the "please input number" and number=input. are in the wrong place and should be above the second while.. maybe the second while shouldnt be a while at all..... This is going around n around n around in my head!!! :confused:

    import java.util.Scanner;
    
    //This program asks the user to input a number between 1 to 200, then adds all
    //numbers upto number input and outputs the total.
    
    class program5
    {
    	public static void main(String[]args)
    	{
    		Scanner input = new Scanner(System.in);
    		
    		int number=0, x=0, count=1, result, total=0, num=0;
    		String answer=("y");
    		
    		while (answer.equalsIgnoreCase("y"))
    		{
    			while (!(number>0 && number<=200))
    			{
    				System.out.println("Please input a number between 1-200.");	
    				number = input.nextInt();
    				
    				if (number>0 && number<=200)
    				{
    					for (x=1; x<=number; x++)
    					{
    						total=total + x;
    						
    						if (!(x ==number))
    						{
    							System.out.print(x +" + ");
    						}
    						else
    						{
    							System.out.println(x + " = "+total);
    						}
    					}
    				}
    				else
    					System.out.println("\nSorry, that number is out of range.");
    					System.out.println("---------------------------------------");
    			}
    		
    			System.out.println("Would you like to enter another number? Yes (y) or No (n).");
    			answer = input.next();				
    				
    		}
    		System.out.println("Thank you, hope to see you again soon.");
    			
    	}
    }
    Code (markup):
     
    Last edited: Apr 1, 2010
    Trebor29, Apr 1, 2010 IP
  2. nimonogi

    nimonogi Active Member

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    80
    #2
    I don't have any java knowledge but this seems to be a wrong statement: answer = input.next();

    Try this way:
    input.next();
    answer.equalsIgnoreCase(input.next());
     
    nimonogi, Apr 1, 2010 IP
  3. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have made the correction. Simply it because you were checking the number within the range after pressing 'y' where you should only ask to insert a new number. Since the number is out of range on the second run, it can't come inside that loop.

    
    import java.util.Scanner;
    
    //This program asks the user to input a number between 1 to 200, then adds all
    //numbers up to number input and outputs the total.
    
    class program5
    {
    	public static void main(String[]args)
    	{
    		Scanner input = new Scanner(System.in);
    		
    		int number=0, x=0, count=1, result, total=0, num=0;
    		String answer=("y");
    		
    		while (answer.equalsIgnoreCase("y"))
    		{
    			do { // mod by neo (expertcore.org): do-while loop used to omit the condition check at start
    				System.out.println("Please input a number between 1-200.");	
    				number = input.nextInt();
    				
    				if (number > 0 && number <= 200)
    				{
    					for (x=1; x<=number; x++)
    					{
    						total += x;
    						
    						if (x !=number) // mod by neo (expertcore.org)
    						{
    							System.out.print(x +" + ");
    						}
    						else
    						{
    							System.out.println(x + " = "+total);
    						}
    					}
    				}
    				else{ // mod by neo (expertcore.org): Inserted brackets to else
    					System.out.println("\nSorry, that number is out of range.");
    				}
    				
    				System.out.println("---------------------------------------");
    				
    			} while (number <= 0 || number > 200);	// mod by neo (expertcore.org): do-while loop used to omit the condition check at start and
    													//									condition was changed to better understanding
    		
    			System.out.println("Would you like to enter another number? Yes (y) or No (n).");
    			answer = input.next();				
    				
    		}
    		
    		System.out.println("Thank you, hope to see you again soon.");
    	}
    }
    
    Code (markup):
     
    NeoCambell, Apr 2, 2010 IP