Results 1 to 2 of 2
  1. #1
    mbae is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Need help with writing an integer program with jGrasp

    INSTRUCTIONS
    You will create a program that will sum the odd integers from 3 to whatever a user inputs. And you will create a loop to do it repeatedly. To do this:
    //-1 Ask the user for a positive integer value greater than 2
    //-2 Check if the user input is 3 or higher, if it is less than 3 print an error message
    //-3 Else if the user input is 3 or higher create a for loop that will keep a sum of the odd numbers between 3 and the input value. Print out the sum included in an explicit statement showing the starting (3) and ending numbers (user input value).
    //-4 Ask the user if they want to try another number. Scan in the response, while it is "yes" repeat from step 1, if the response is "no" end the program by saying "Thanks for playing!"

    Also remember that since the sentinel value is "yes"; a string needs to use name.equals rather than name= to evaluate whether the sentinel is found. This helps start the while loop.
    To do your for loop don't forget to start your count at 3 and then since we are only adding odd values make the increment add 2.


    Example output:
    Enter a positive integer: 0
    The value must be greater than two.
    Do you want to do another? yes or no
    yes
    Enter a positive integer: 10
    The sum of the ODD integers from 3 to 10 is 24
    Do you want to do another? yes or no
    no
    Thanks for playing!

    I cannot seem to get the boolean part right and have it loop again. I do not think I have my 'do' statement correct as well.

    This is what i have so far:
    PHP Code:
    import java.util.Scanner;
    import java.lang.String;
    
    
    public class test4 
    { 
    	public static void main (String[] args)
    	{
    		Scanner scan = new Scanner (System.in);
    		
    		int userNum, total = 0;
    		String playAgain;
    		boolean play;
    		
    		
    		while (play == true);
    		{
    		System.out.println("Enter a positive integer: ");
    		userNum = scan.nextInt(); 
    		}
    		if (userNum >= 3)
    		{
    			for(int count = 3; count < userNum; count +=2)
    			{
    				total = total + count;
    			}
    				
    			System.out.println("The sum of the ODD integers from 3 to " + userNum +  " is " + total);
    		}		
    		do
    		{
    			System.out.println("Do you want to do another? yes or no");
    			playAgain = scan.nextLine();
    			
    			if (playAgain.equals("yes"))
    			play = true;
    				if (playAgain.equals("no"))
    				play = false;		
    				else
    
    					if (userNum < 3)
    					System.out.println("The value must be greater than two");
    			}
    	
    		
    
    	}
    }
    Some help would be great. Thanks!

  2. #2
    mbae is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Need help with writing an integer program with jGrasp

    need this tomorrow by 1 pm :( i believe my end brackets are off too

Similar Threads

  1. Reading/Writing Integer Bytes
    By AndrewM16921 in forum New To Java
    Replies: 1
    Last Post: 04-04-2011, 12:56 AM
  2. Due Tommorow Writing a Program Please Help
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-23-2008, 04:15 AM
  3. Replies: 3
    Last Post: 08-24-2008, 02:51 PM
  4. how begin with writing a new program ?
    By dimitrist in forum New To Java
    Replies: 11
    Last Post: 05-13-2008, 04:49 AM
  5. Help needed writing a program...
    By Francis in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 03:03 PM

Posting Permissions

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