4 Replies - 2952 Views - Last Post: 21 November 2010 - 08:10 PM Rate Topic: -----

#1 cathyd0890  Icon User is offline

  • New D.I.C Head

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

trouble with while loop for a program

Posted 21 November 2010 - 07:24 PM

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.



import.java.util*;
    
    public class WhileLoops{
    	public static void main(String[] args){
    		
    		// declare varibles
    		int = firstNum = 0, secondNum = 0;
                String even,odd,sum
   			 Scanner scan = new Scanner(System.in)
   			 
   			 //prompt user for integera and store them
			   System.out.println("Enter two numbers");
			   
			    firstNum = scan.nextInt();
   			    secondNum = scan.nextInt();
			
			
			//output results
	

 
 while(firstNum <= secondNum){
		if (currentNum%2 == 0)
    				 System.out.println( " " + even); 
   			else 
                             System.out.println( " " + odd);
   					 
	}



this is all I have and I am clueless on the rest of the codes. Can someone please help me?

This post has been edited by cathyd0890: 21 November 2010 - 07:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: trouble with while loop for a program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: trouble with while loop for a program

Posted 21 November 2010 - 07:38 PM

    while(firstNum <= secondNum){
		if (currentNum%2 == 0)
    				 System.out.println( " " + even); 
   			else 
                             System.out.println( " " + odd);
   					 
	}


A while loop that does not change inside itself the variables used to test the condition in the while() statement is automatically an infinite loop
Better to use a for() loop for what you are trying to do
for(int i = firstNum; i < = secondNum; ++i)
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: trouble with while loop for a program

Posted 21 November 2010 - 07:43 PM

yea but the professor wanted me to use while loop for one program and for loop for another.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: trouble with while loop for a program

Posted 21 November 2010 - 07:48 PM

View Postcathyd0890, on 21 November 2010 - 08:43 PM, said:

yea but the professor wanted me to use while loop for one program and for loop for another.

Good pedagogic approach to learn how different loops works
so you will have to change firstNum or secondNum within your loop or it will be infinite
a firstNum++; may be
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: trouble with while loop for a program

Posted 21 November 2010 - 08:10 PM

oh yeah that is true but can I combine that while loop with another one? I mean I have to output the odd numbers, the sum of all the evens, output all the numbers and their squares between 1 and 10 , output the sum of the squares of all the odds and output all the uppercase letter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1