• April 12th, 2011, 05:17 PM
jokneez
hi, i have to write a program for my intro to programming class and i cant get this code to work :( the assignment is :

Write a program that prompts the user to enter two positive integers r1 and r2 and displays all numbers from 100 to 1000, ten per line, that are divisible by r1 and r2. Also, if the user doesn't enter a number from 100-1000, ask them to enter it again.

the code that i have wrote so far is:

Code :

```import java.util.Scanner; public class TheProject {     public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);   int r1, r2;     System.out.println("Enter two positive numbers:"); r1 = keyboard.nextInt(); r2 = keyboard.nextInt();   while (r1 > 0 && r2 > 0) { for (int number = 100; number <= 1000; number++) { if (number%r1 == 0 && number%r2 == 0)   System.out.println(number);   }     }     } }```
• April 12th, 2011, 05:22 PM
SuisseToni
You seem to be on the right lines though there is an example of where English language leads to ambiguity.

What output are you getting and what output do you expect?

Suisse
• April 12th, 2011, 06:09 PM
Junky
Quote:

Originally Posted by jokneez
i cant get this code to work

This provides ZERO information.
• April 13th, 2011, 04:18 PM
clockworkworks
you need a conditional statement “if” not a while loop, you could use a recursive method by using if else , that will solve your problem or at least give you a start...
• April 14th, 2011, 10:42 AM
dr3wmurphy
Quote:

Originally Posted by clockworkworks
you need a conditional statement “if” not a while loop, you could use a recursive method by using if else , that will solve your problem or at least give you a start...

In an Intro to Programming class, I doubt that recursion has been covered.

As far as the program is concerned, you have a nested loop in which the outer loop, if r1 and r2 are both positive integers, will have stuck you in an infinite loop.

Make the while statement into an if-else statement, where the program re-asks the user for numbers if r1 and r2 are invalid, else the program executes normally.