7 Replies - 6814 Views - Last Post: 12 March 2012 - 07:03 PM Rate Topic: -----

#1 cathyd0890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-October 10

Having an issue with while loops in java program

Posted 18 November 2010 - 06:36 PM

I am not asking anyone to do my assignment for me, I just have no clue what I am suppose to do. I don't know where to start or what exactly the program is looking for. If someone would kindly explain or would help me out with this, I will definitely appreciate all the help I can get.

So my assignment is to write a program that used while loops to perform 6 different things.
1. prompt the users to input two integers: firstNum and secondNum (firstNum must be less than secondNum).
2. output all the odd numbers between firstNum and secondNum inclusive
3. output the sum of all the even numbers between firstNum and secondNum inclusive
4. output all the numbers and their squares between 1 and 10
5. output the sum of the squares of all the odd numbers between firstNum and secondNum inclusive
6. output all the uppercase letter.
and lastly there is a hint: if currentNum % 2 = 0, then the currentNum is an even number. Otherwise it is an odd number.

Once again, I am not expecting or asking anyone to do a whole code or my entire assignment for me. I just really need help because I am so confused with the whole program. Thank You.

Is This A Good Question/Topic? 0
  • +

Replies To: Having an issue with while loops in java program

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Having an issue with while loops in java program

Posted 18 November 2010 - 06:55 PM

I personally think that For loops are the better choice here...but use while loops if you were told to use them.

The point of a loop is to make repetitive processes simpler and shorter in terms of writing code. A while loop has a condition which it checks before every iteration. For example, the following sums up the numbers from 1-10.
int sum = 0;
int i = 1;
while(i <= 10)
{
  sum += i;
  i++;
}


Using loops in Steps 2-6 will certainly help ;)
Was This Post Helpful? 0
  • +
  • -

#3 cathyd0890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-October 10

Re: Having an issue with while loops in java program

Posted 18 November 2010 - 08:46 PM

I actually have to do two programs but for the exact problem like the professor want one in a form of while loop and the other in a for loop. I don't understand the concept of the program : so the user have to input two numbers and the output has to tell if it is odd or even and the sum of it? but how do I turn that into a while loop? do I put if and else statement into the while loop too? sorry for all these question, I'm just trying to figure how to even go about to write the program
Was This Post Helpful? 0
  • +
  • -

#4 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Having an issue with while loops in java program

Posted 18 November 2010 - 09:11 PM

Always good to think of an algorithm

To find the odd numbers between #1 and #2, Step #2
- Prompt user for input of number 1 and store input
- Prompt user for input of number 2 and store input
- Compare both numbers to see which is lower
- Start the loop at the lower number
- Each time, check for odd--if a mod 2 results in 1, it is odd
- If odd, print the number out
- Repeat loop

Scanner in = new Scanner(System.in);

int n;
int n2;

System.out.println("Enter first number: ");
n = Integer.parseInt(in.nextLine().trim());

System.out.println("Enter second number: ");
n2 = Integer.parseInt(in.nextLine().trim());

for(int i = (n2 > n ? n : n2); i <= (n2 > n ? n2 : n); i++)
{
  if(i % 2 == 1)
  {
    System.out.print(i + " ");
  }
}


Was This Post Helpful? 0
  • +
  • -

#5 cathyd0890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-October 10

Re: Having an issue with while loops in java program

Posted 18 November 2010 - 09:19 PM

does that mean for each step I have to create a different while loop?
Was This Post Helpful? 0
  • +
  • -

#6 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Having an issue with while loops in java program

Posted 18 November 2010 - 09:24 PM

Steps 2-5 can be done in 1 loop, Step 6 must have a separate loop (to print out all uppercase letters, you want to use a loop printing output starting with a char at 'A' and ending at 'Z').

Sometimes, it's better to use separate loops to make it more clear what you are trying to accomplish. I would personally use a different loop for each for clarity especially if you are just learning loops, but whatever floats your boat is good :)
Was This Post Helpful? 0
  • +
  • -

#7 cathyd0890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 31-October 10

Re: Having an issue with while loops in java program

Posted 19 November 2010 - 10:57 PM

I still have no clue how to write this program. I think I am screwed =/.
Was This Post Helpful? 0
  • +
  • -

#8 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Having an issue with while loops in java program

Posted 12 March 2012 - 07:03 PM

Apparently I have no idea what I am doing. I am finding it hard to understand the loops. Am I right for using a while loop, or should I use an if else loop. I have read some examples but they are not making sense. I guess I will get back to reading more on the loops. I was hoping to pick this up through some repetition and eventually learn how the syntax interacted in the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1