Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

hello i want to know my error on this java code :

what is wrong with my code?

the program works but the loop while stop at first result why?

public class exo7 {
   public static void main(String[] args) { 

           int nb, limite, mult;

            Scanner sc= new Scanner(System.in);


            System.out.println("Donner la table :");
            nb = sc.nextInt();

            System.out.println("Jusqu'a quel limite :");
            limite =sc.nextInt();


           mult = 1;
            while(mult>=limite);
            {
                System.out.println(mult+"*"+nb+"="+ (mult*nb));
                nb=nb+1;
            }

1 Answer

Hi Morgan

I had to add a few things to get this to run in the workspace but assuming that isn't the issue you're experiencing. I think the issue is caused by the semicolon after the condition statement in the while loop as this seems to exhibit the behaviour you've described.

I'm not an expert in Java however so perhaps that is there for a reason which I don't understand :).

Oh and beware as I think you've created an infinite loop or at least the values I gave it while not being able to understand the question (sorry don't read French) just started throwing out lots of results after I'd removed the semicolon.

Hope it helps!

OK so I kept playing with your code and I was just wondering if the idea was to multiply through values until you reach the specified limit as in if 5 was the multiplying value and 12 was the limit it would produce:

1*5=5
2*5=10
3*5=15
4*5=20
5*5=25
6*5=30
7*5=35
8*5=40
9*5=45
10*5=50
11*5=55
12*5=60

If so I tweaked a couple of things in your code below to make it do so if not it was fun to mess around with it anyway :)

import java.util.Scanner;

public class exo7 { 

public static void main(String[] args) {

  int nb, limite, mult;

  Scanner sc= new Scanner(System.in);


  System.out.println("Leave the table :"); // according to google translate
  nb = sc.nextInt();

  System.out.println("Up to what limits :"); // according to google translate
  limite =sc.nextInt();


  mult = 1;
  while(mult<=limite) // changed this to less than so it would only loop for as long as mult was less than limit
        {
            System.out.println(mult+"*"+nb+"="+ (mult*nb));
            mult++; // incrementing one to mult with each iteration

        }
}
}