7 Replies - 241 Views - Last Post: 07 November 2012 - 09:17 PM Rate Topic: -----

#1 Finitesky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-November 12

Problem with my loop!

Posted 06 November 2012 - 08:28 PM

Hey guys i'm having some problems with my loop. I am trying to create a program that prompts the user to enter and starting number and a stopping number and also a factor value. The program will then display the the range of the two values that are divisible by the factor. ie. start is 10 and stop is 30 and factor is 3, numbers displayed would be 12,15,18,etc up to 30. I have made the program run fine when only displaying the range of the start-stop values, once I include the factor I am able to execute the program but nothing is displayed after the factor is entered. If anyone has any idea's please help.
import java.util.Scanner;
public class Factor {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		
		
		
	int start, stop, count, factor;
		
	Scanner scan = new Scanner (System.in);
		
	System.out.print("Enter the first number");
		
	start = scan.nextInt();
		
	System.out.print("Enter the second number");
		
		stop = scan.nextInt();
		
		
		
System.out.print("Enter the Factor value");
factor = scan.nextInt();	
		
count = start;
		
while (count <= stop)
{
	if(count % factor ==0){
		count ++;
		System.out.println ("The Number of Even Numbers Are:" + count);}
       }
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with my loop!

#2 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Problem with my loop!

Posted 06 November 2012 - 10:18 PM

My guess is you have an infinite loop. If your first count % factor != 0, your count is never updated, and your loop condition is never updated.
Was This Post Helpful? 1
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem with my loop!

Posted 06 November 2012 - 11:17 PM

Let me try and explain why that is happening in steps.

Suppose you give start as 10, stop as 20 and factor as 3.

1.count will take the value 10.
2.while(10<=20) //true
3.if( 10 % 3==0)//false

4.the above 2 steps keep repeating infinitely as the 'count' is not being incremented.

You may want to look into that aspect of preventing an infinite loop by letting 'count' as a separate variable initialized to 0.

You could change loop condition to while(start<=stop) and increment 'start' every single time the loop is executed and increment 'count' only when the factor condition is reached.You could replace 'count' by 'start' in your if condition.

Also make the print statement to get executed once it exits the while loop.

regards,
Raghav

This post has been edited by raghav.naganathan: 06 November 2012 - 11:26 PM

Was This Post Helpful? 1
  • +
  • -

#4 Finitesky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-November 12

Re: Problem with my loop!

Posted 06 November 2012 - 11:20 PM

thanks updated my code and the program runs but is adding 1 to the final count and i'm not sure why?
ie
Enter the first number 1
Enter the second number 20
Enter the Factor value 3
3
6
9
12
15
18
21




while (count <= stop)
	{
	count++;
	if(count % factor ==0)
	{
		System.out.println ( count);}
    }
	
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem with my loop!

Posted 06 November 2012 - 11:30 PM

I believe you didn't understand what I meant.

Please make the changes I suggested in your original code and I am sure your program will work :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#6 ShinryuuKai  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 03-November 12

Re: Problem with my loop!

Posted 06 November 2012 - 11:39 PM

Move count++; outside the if block and that infinite loop breaks, also I suggest using a For loop instead of while so you can omit that line entirely. I exclusively reserve while for when I'm not doing simple counter incrementation since Fors do that for you.

My most recent project with a while loop involved a card game shuffle, in this game cards can be right-side-up or up-side-down so my while loop used an RNG to use as a condition, and rngVal % 3 == 0 was my condition for a valid card, meaning my condition in the while loop was while (rngVal % 3 !=0 ) keep finding a new rngVal. To test for card positioning I checked for rngVal % 2 == 0, 0 being right-side-up 0 being upside down and the actual rngVal determining what value the card had within a suit based on multiples of 3 and 6, 3 & 6 were the same card but 3 was uoside down etc etc. But you're just crunching numbers and adding one to your loop counter so using a while is overkill.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with my loop!

Posted 07 November 2012 - 11:25 AM

instead of

while (count <= stop)  
{  
     count++;  


which bring count a stop + 1 just do
while (count < stop)  
{  
     count++;  



or put the count++ after the test
Was This Post Helpful? 1
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem with my loop!

Posted 07 November 2012 - 09:17 PM

View Postpbl, on 07 November 2012 - 11:55 PM, said:

instead of

while (count <= stop)  
{  
     count++;  


which bring count a stop + 1 just do
while (count < stop)  
{  
     count++;  



or put the count++ after the test


pbl I have a suggestion. Why not let the counter variable stay as a separate entity and not be involved in the while condition.

What I suggested the OP was to keep the start and the stop condition in the while loop

Something like this.

int count=0;

while(start<=stop)
{
  if(start % factor==0)
  {
    count++;
  }
start++;
}



And then in the end, print the value of count after the loop.

Is this a good way to do the program? Please correct me if I am wrong.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1