Results 1 to 3 of 3
  1. #1
    Dev23 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Can someone figure out this error for me

    I'm trying to make a function where a user inputs numbers in a for while-loop statement, which continues to loop until they input 0 When this happens then the sum is multiplied by all the numbers entered. If there is a 4 it skips, and if a break, it stops the program. The problem i have with this is that, whenever a go to run this it gives me an answer back every time a enter a number. I only want the answer to return when i enter in a zero.

    Java Code:
    public static void main(String []args) {
    		Scanner scan= new Scanner(System.in);
    		for(int number = 1,sum = number;number!=0;System.out.println("The answer is:" + sum)){
    			System.out.println("Please enter a number:");
    			number=scan.nextInt();
    			
    			{if (number == 4)
    				{continue;}
    		else {if (number == 7)
    			{break;}
    		}
    			
    			
    		}
    		
    		sum = sum * number;	
    			
    		
    	}
    		
    		}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Then take the print statement out of the loop. Seriously was it that hard to understand?

  3. #3
    liyuyu_852000 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    you can modify the code like this:
    Java Code:
    public static void main(String []args) {
    		Scanner scan= new Scanner(System.in);
                              int sum=1;
    		for(int number = 1;number!=0;){
    			System.out.println("Please enter a number:");
    			number=scan.nextInt();
    			
    			if (number == 4){
                                                  continue;
                                          }else if (number == 7){
                                                  break;
                                          }
                                          sum = sum * number;
    		}
    		System.out.println("The answer is:" + sum)	
    }

Similar Threads

  1. Basic Java applet error, cant figure it out!
    By alacn in forum Java Applets
    Replies: 3
    Last Post: 08-03-2010, 06:25 PM
  2. Replies: 3
    Last Post: 01-11-2010, 07:48 AM
  3. Need help - I can't figure it out.
    By Joshsmith in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 10:12 PM
  4. Seems so simple yet I can't figure it out!
    By adamhaviland in forum New To Java
    Replies: 1
    Last Post: 11-03-2007, 07:26 PM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

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