Can anyone see where im going wrong here?

Discussion in 'JavaScript' 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. :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. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    javascript has nothing to do with java. at all.
     
    krsix, Apr 1, 2010 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    Have you tried something like this?

    
    if (answer.equalsIgnoreCase("n")) 
    {
    				System.out.println("Thank you, hope to see you again soon.");
    }
    
    Code (markup):
    Also have you made sure the input works correctly? Print answer on screen to verify it actually works.
     
    stephan2307, Apr 1, 2010 IP