Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 29

Thread: While loop to perform multiple steps Please help assignment due in 12 hours!!

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default While loop to perform multiple steps Please help assignment due in 12 hours!!

    Hello all! I have a assignment due tomorrow morning and I am totally clueless. I have tried reading a bunch of things on while loops but dont seem to understand it fully. This is my directions for the assignment:

    Write a program that uses a while loop to perform the following steps:
    Comment by labelling each part: //Part A, //Part B, etc...
    A.)Prompt the user to input 2 integers: firstNum and secondNum. Use 10 and 20.
    B.)Output all odd numbers between firstNum and secondNum.
    C.)Output the sum of all even numbers between firstNum and secondNum.
    D.)Output the numbers and their square between 1 and 10.
    E.)Output the sum of the square of odd numbers between firstNum and secondNum.
    F.)Output all uppercase letters.

    Again I am new to while loops and I am totally lost. I have tried reading a bunch but I am better at learning if someone shows me how to do something. I am open to all suggestions. I have just completed 8 other programs using if else statements and now trying to get the hang of loops. Thanks!

    This is my pathetic attempt so far LOL

    import java.util.Scanner;
     
    public class whileLoop
    {
    	public static void main(String[] args)
    	{
     
    		Scanner keyboard = new Scanner(System.in);
     
    		//Part A
    		int firstNum = 10;
    		int secondNum = 20;
     
    		System.out.println("Please enter two integers: ");
    		int oddNum = keyboard.nextInt();
     
    		//Part B
                    int temp = firstNum;
                    while(temp <= secondNum)
                    {
                          if(temp % 2 != 0)
                         {
                         System.out.println(""+temp);
                         }
                         temp++;
                         }
    	}
    }
    Last edited by brobertson300; March 20th, 2014 at 09:35 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    One problem I see is that statements in the loops and the if statement are not enclosed in {}s. That makes the code harder to read and understand and dangerous to change.

    Also nested statements inside the loop and if statements need to be indented to make the code easier to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Thanks for the tips!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    You need to update the code by adding the {}s and indentations to make the code readable.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Okay I tried correcting it

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    You got part of it. There should be {}s for the loops and if statements
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Can you please just help me besides critizing my curly braces? I understand its important but I need help with the actual program. I will figure out the format of it later. I need help with the logic ASAP

    --- Update ---

    Okay I think I fixed it. But i need to properly initialize firstNum and secondNum. I am so lost lol..

    --- Update ---

    updated a little. Im just trying to piece it together with what i know. If someone could help me clean it up and help with the rest that would be awesome!

    import java.util.Scanner;
     
    public class whileLoop
    {
    	public static void main(String[] args)
    	{
     
    		Scanner keyboard = new Scanner(System.in);
     
    		//Part A
    		int firstNum;
    		int secondNum;
    		int i;
     
    		System.out.println("Please enter two integers: ");
     
    		//Part B
    		int temp = firstNum;
    		while(temp <= secondNum)
    		{
    			if(temp % 2 != 0)
    		    {
    		       System.out.println(""+temp);
    		    }
    		    temp++;
     			}
    	}
    }
     
    		//Part C
    		int i=firstnumber;
    		int sum=0;
    		int sumofsqr=0;
     
    		while(i<=secondnumber)
    			{
    		    if(i%2==0)
    		    {
    		    	System.out.println(i);
    		    	sum+=i;
    		    }
    		    else
    		    {
    		    //Part E
    		    	sumofsqr+=i*i;
    		    }
    		    i++;
    			}
    				System.out.println("Sum of even " + sum + " Sum of sqr of odd " + sumofsqr);
    	}
    }

  8. #8
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Please help with multi-step program

    Hello all! I have a assignment due tomorrow morning and I am totally clueless. I have tried reading a bunch of things on while loops but dont seem to understand it fully. This is my directions for the assignment:

    Write a program that uses a while loop to perform the following steps:
    Comment by labelling each part: //Part A, //Part B, etc...
    A.)Prompt the user to input 2 integers: firstNum and secondNum. Use 10 and 20.
    B.)Output all odd numbers between firstNum and secondNum.
    C.)Output the sum of all even numbers between firstNum and secondNum.
    D.)Output the numbers and their square between 1 and 10.
    E.)Output the sum of the square of odd numbers between firstNum and secondNum.
    F.)Output all uppercase letters.

    Again I am new to while loops and I am totally lost. I have tried reading a bunch but I am better at learning if someone shows me how to do something. I am open to all suggestions. I have just completed 8 other programs using if else statements and now trying to get the hang of loops. Thanks!

    This is my pathetic attempt so far LOL its all pieced together from what i know and its missing a lot. If someone could show me how to make it all work together that would be awesome!

    import java.util.Scanner;
     
    public class whileLoop
    {
    	public static void main(String[] args)
    	{
     
    		Scanner keyboard = new Scanner(System.in);
     
    		//Part A
    		int firstNum;
    		int secondNum;
    		int i;
     
    		System.out.println("Please enter two integers: ");
     
    		//Part B
    		int temp = firstNum;
    		while(temp <= secondNum)
    		{
    			if(temp % 2 != 0)
    		    {
    		       System.out.println(""+temp);
    		    }
    		    temp++;
     			}
    	}
    }
     
    		//Part C
    		int i=firstnumber;
    		int sum=0;
    		int sumofsqr=0;
     
    		while(i<=secondnumber)
    			{
    		    if(i%2==0)
    		    {
    		    	System.out.println(i);
    		    	sum+=i;
    		    }
    		    else
    		    {
    		    //Part E
    		    	sumofsqr+=i*i;
    		    }
    		    i++;
    			}
    				System.out.println("Sum of even " + sum + " Sum of sqr of odd " + sumofsqr);
    	}
    }

  9. #9
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Please help with multi-step program

    Okay, I read the problem A.) only. then I looked at your code, you did not manage to prompt for user's input right?
    Let's fix your problem from the top 1 by 1.
    Do you know how to use the Scanner for user's input?
    Last edited by dicdic; March 21st, 2014 at 12:11 AM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    The curly braces are very important to organize the code and ensure proper execution.

    need to properly initialize firstNum and secondNum.
    What values should those two variables have? Where do those values come from? Is the Scanner class object: keyboard supposed to be used to read the user's input. The Scanner class has methods that will read the user's input.

    --- Update ---

    Same topic threads merged.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please help with multi-step program

    Yes I am familiar with Scanner. I prompted the user to input two integers but now i need to read from the keyboard the values they entered which should be 10 and 20. Above I posted the directions for the assignment in my original post. I need to initialize firstNum (10) and secondNum (20) but not sure where to initialize them.


    Quote Originally Posted by dicdic View Post
    Okay, I read the problem A.) only. then I looked at your code, you did not manage to prompt for user's input right?
    Let's fix your problem from the top 1 by 1.
    Do you know how to use the Scanner for user's input?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    I need to initialize firstNum (10) and secondNum (20)
    You can initialize them those variables with those values on the same lines when you define them just like you did with the variable: temp.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Okay I got it to work and output all odd numbers between 10 and 20... onto the next part!
    import java.util.Scanner;
     
    public class whileLoop
    {
    	public static void main(String[] args)
    	{
     
    		Scanner keyboard = new Scanner(System.in);
     
    		//Part A
    		System.out.println("Please enter two integers: ");
     
    		//Part B
    		int firstNum = keyboard.nextInt();
    		int secondNum = keyboard.nextInt();
    		int temp = firstNum;
    		while(temp <= secondNum)
    		{
    			if(temp % 2 != 0)
    		    {
    		       System.out.println(""+temp);
    		    }
    		    temp++;
     			}
    	}
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Glad you are making progress. As dicdic said, do it one step at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Part C) I need to output the sum of all even numbers between firstNum and secondNum (10,20). How would I transition into that? I have only done simple one part programs.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Parts of the code could be used for that: the loop that goes from one number to the other.
    What parts of Part C) are you having problems with? Make a list of the steps needed to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    		int i = firstNum;
    		int sum = 0;
     
    		while(i <= secondNum)
    		    {
    		    if(i % 2 == 0)
    		    {
    		    	System.out.println(i);
    		    	sum += i;
                        }
                        System.out.println("Sum of even " + sum + ");
                  }
        }


    --- Update ---

    I guess that is how I would get the sum of all even numbers between first and second Num. But not sure how to tie it into the program as a whole

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    If there is a loop that goes from the first to the last number, there could be more than one section of code in that loop that used the sequence of numbers used in the loop.

    begin loop from start to end
    test1 number and do something
    test2 number and do something else
    test3 number and do another thing
    end loop
    Report results for the tests
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Can you show me a example? not with my assignment but something general? I am a visual learner. I am not asking for someone to do my assignment becasue i want to learn it but i also need something visual

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Merge the code you have written in post#17 (the if statement for even numbers etc) with that in post#13
    something visual
    See the pseudo code in post#18.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    This is my whole program so far... steps A through E. The directions again are posted above. I am just piecing together what I know. The logic is there but the format definitely isnt.
    import java.util.Scanner;
     
    public class whileLoop
    {
    	public static void main(String[] args)
    	{
     
    		Scanner keyboard = new Scanner(System.in);
     
    		//Part A
    		System.out.println("Please enter two integers, the first must be less than second: ");
     
    		//Part B
    		int firstNum = keyboard.nextInt();
    		int secondNum = keyboard.nextInt();
    		int temp = firstNum;
    		while(temp <= secondNum)
    		{
    			if(temp % 2 != 0)
    		    {
    		       System.out.println(""+temp);
    		    }
    		    temp++;
     			}
     
    		//Part C
    		int i = firstNum;
    		int sum = 0;
     
    		while(i <= secondNum)
    			{
    		    if(i % 2 == 0)
    		    {
    		    	System.out.println(i);
    		    	sum += i;
    		    }
    		//Part D
    		    for (int i= 1; i <= 10; i++)
    		    {
    		    	System.out.println(i+" "+i*i);
    		    {
    		//Part E
    		while(firstNum <= secondNum)
     
    		    sumofsqr += i * i;
    		    }
    		    	i++;
    			}
    					System.out.println("Sum of even " + sum + " Sum of sqr of odd " + sumofsqr);
    	}
    }

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    Just use one loop that goes from start to end and has inside of the loop the tests, etc

    See post#18
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    just use if and if else statments to tie it all together?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    If the separate tests are related, then they could be tied together. Like even vs odd number: if not even then must be odd.

    The comments in the code should say what the code is doing:
    //Part D
    does not tell anyone reading the code what the code following that comment is doing. It's better to have it all in one place and not require someone to hunt for where Part D is defined.

    I will give you credit for trying to comment the code. Most students don't any at all.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: While loop to perform multiple steps Please help assignment due in 12 hours!!

    import java.util.Scanner;
     
    public class twoIntegers
    {
    	public static void main(String[] args)
    	{
     
    		Scanner keyboard = new Scanner(System.in);
     
    		//Part A
    		//Input two integers
     
    		System.out.println("Please enter two integers,"
    						  + "the first must be less than second: ");
     
    		//Part B
    		//All odd numbers between firstNum and secondNum
     
    		int firstNum = keyboard.nextInt();
    		int secondNum = keyboard.nextInt();
    		int temp = firstNum;
    		int sum = 0;
    		String oddNum = "";
    		int sumSqr = 0;
     
    		while(temp <= secondNum)
    		{
    			if(temp % 2 != 0)
    		    {
    			oddNum = oddNum + temp + " ";
    			}
    		    temp++;
    		}
    		System.out.println("The odd numbers are: " + oddNum);
     
    		//Part C
    		//Sum of all even numbers between firstNum and secondNum
     
    		temp = firstNum;
    		while(temp <= secondNum)
    		{
    		    if(temp % 2 == 0)
    		    {
    		    	sum += temp;
    		    }
    				temp++;
    		}
    		System.out.println("The sum of all even numbers: " + sum);
     
    		//Part D
    		//Output the numbers and their square between 1 and 10
     
    		int i = 1;
     
    		while(i <= 10)
    		{
    			System.out.println("" + (i * i));
    			i++;
    		}
     
    		//Part E
    		//Output the sum of square of odd numbers
     
    		temp = firstNum;
     
    		while(temp <= secondNum)
    		{
    			if(temp % 2 != 0)
    			{
    				sumSqr += temp * temp;
    			}
    				temp++;
    		}
    		System.out.println("Sum of the square of odd numbers: " + sumSqr);
     
    		//Part F
    		//Output all uppercase letter.
     
    		i = 0;
     
    		while(i < 26)
    		{
    			System.out.println((char) (i + 65));
    			i++;
    		}
    	}
    }


    --- Update ---

    This is my finished program. However the output is correct but messy. How would I output the output horizontally instead of vertically?

Page 1 of 2 12 LastLast

Similar Threads

  1. How to use a while loop to perform input validation?
    By namenamename in forum Java Theory & Questions
    Replies: 11
    Last Post: October 5th, 2013, 07:24 AM
  2. Replies: 1
    Last Post: December 5th, 2012, 10:58 PM
  3. [SOLVED] PLZ HELP! Somethng wrong with code, assignment due today! Print if multiple of...
    By Rstuart970 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 16th, 2012, 02:35 AM
  4. FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
    By DUE IN 4 HOURS in forum What's Wrong With My Code?
    Replies: 27
    Last Post: November 3rd, 2011, 08:15 AM
  5. Need help on an Assignment but its due tomorrow!
    By Mob31 in forum Paid Java Projects
    Replies: 1
    Last Post: March 2nd, 2011, 06:54 AM