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");
}
}
}