Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: please help with simple program!!!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default please help with simple program!!!

    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:

    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);
     
    		}
     
     
    		}
     
     
    	}
    }
    Last edited by jokneez; April 12th, 2011 at 05:25 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: please help with simple program!!!

    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

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: please help with simple program!!!

    Quote Originally Posted by jokneez View Post
    i cant get this code to work
    This provides ZERO information.

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help with simple program!!!

    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...

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Location
    Carbondale, IL
    Posts
    4
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please help with simple program!!!

    Quote Originally Posted by clockworkworks View Post
    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.
    Last edited by dr3wmurphy; April 14th, 2011 at 10:45 AM.

Similar Threads

  1. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  2. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 01:47 PM
  3. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM
  4. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  5. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM