• Post Reply Bookmark Topic Watch Topic
  • New Topic

While Loop  RSS feed

 
Lique Reads
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I not quite sure why this code won't work. It compiles correctly, but won't print anything.
Thanks for your assistance.

/**
*
* To calculate the first 400 numbers that are divisible by 13
*/

public class Thirteen3
{

public static void main(String[] args)
{

int limit = 400;
int test = 1;
while (limit <400)
{
if (test%13==0)
System.out.println("numbers divisible by 13 are " + test);
test++;
limit++;
}
System.out.println(+ test);
}
}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code will only enter the while loop if limit is less than 400, but you initialise limit to 400, so the loop is never entered.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using code tags will maintain the formatting,
and helps to see what your code is doing,

as mentioned, you won't enter the while loop,
if (test < limit) is likely what you were thinking

(and then there's prob'ly other changes you want to make,


[ February 13, 2008: Message edited by: Bill Shirley ]
 
jean mas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the goal is to have 400 numbers that can be divided by 13 then more changes need to be made I think.

public class Thirteen3
{

public static void main(String[] args)
{

int limit = 0;
int test = 1;
while (limit <400)
{
test++;
if (test%13==0)
System.out.println("numbers divisible by 13 are " + test);
limit++;
}
System.out.println(+ test);
}
}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!