0

So I was assigned the task of displaying all numbers divisible by two, five, and every third number in a range which I'm sure you can see how I did that. However I am having trouble as I was asked to use while loops, and I would also like to avoid having to reinitialize the integers start and end every time I create a list but I'm unsure as to how to do this. Any help would be greatly appreciated.

import java.util.Scanner;
public class main
{
    public static void main (String[] args){
        int start, end;
        
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter a starting integer: ");
        start = scan.nextInt();
        System.out.print("Enter an ending integer: ");
        end = scan.nextInt();
        
        System.out.println("All numbers divisible by two in this range are: ");
        for(start = start ; start <= end; start++){
        if(start%2==0){
            System.out.print(start + "\n");
        }
        }
        
        //re-intitializes int start and end
        System.out.print("Please re-enter your starting integer: ");
        start = scan.nextInt();
        System.out.print("Please re-enter your ending integer: ");
        end = scan.nextInt();
        
        System.out.println("All numbers divisible by five in this range are: ");
        for(start = start; start <= end; start++){
        if(start%5==0){
            System.out.print(start + "\n");
        }
        }
        
        System.out.print("Please re-enter your starting integer: ");
        start= scan.nextInt();
        System.out.print("Please re-enter your ending integer: ");
        end = scan.nextInt();
        
        System.out.println("Every third number in this range is: ");
        for(start = start ; start <= end; start+=3){
            System.out.print(start + "\n");
        
    }

   }
}

Edited by falconpunch: n/a

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Majestics
0

You need an array of integer to place the number's which are use for division. Like

int dividend[] = {2,5};

Then you need a while loop

while(i<dividend.length)
if(start % a[i])

I have checked your code... Look at this line

for(start = start ; start <= end; start++){

You have done a blunder , try int i instead of start. and i want you to figure it out why.

Edited by Majestics: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.