public class Main
{
/**
* This is the main routine for this assignment.
*/
public static void main(String[] args)
{
//This is to clear the terminal window.
//It will flush the terminal window so it is clear for new output.
System.out.print("f");
//Declare the variables
int n; //N is for the upper limit to count up to
int a; //A is for the divisor for apple numbers
int b; //B is for the divisor for banana numbers
boolean run= true;
//create an object of Scanner class
Scanner input= new Scanner(System.in);
//Prompt the user to enter a number
// that will be an upper limit for my program to count up to
System.out.println("Please enter a number that will serve as an upper limit");
n= input.nextInt();
while (!(n>= 1 && n<=1000000)){
System.out.println("You need to enter a number between 1 and 1,000,000");
n=input.nextInt(); //Prompts the user to input correct number
}
//Prompt the user to enter a second number
//that will be the first divisor
System.out.println("Please enter a number that will serve as the first divisor");
a= input.nextInt();
while (!(a>= 1 && a<=100)){
System.out.println("You need to enter a number between 1 and 100");
a=input.nextInt(); //Prompts the user to input correct number
}
//Prompt the user to enter a third number
//that will be the second divisor
System.out.println("Please enter a number that will serve as the second divisor");
b= input.nextInt();
while (!(b>= 1 && b<=100)){
System.out.println("You need to enter a number between 1 and 100");
b=input.nextInt(); //Prompts the user to input correct data
}
//Create a counter that starts at 1
int c= 1;
//Create a counter for apple and banana numbers
int ab= 0;
//while loop continues
//as long as c is less or equal to n
// and as long as ab is less than 5
while(c <= n && ab < 5){
if( c%a==0 && c%b==0){
System.out.println( c + " Apple and Banana");
ab++;
}else if ( c%a==0&& c%b!=0){
System.out.println( c + " Apple");
}else if ( c%b==0 && c%a!= 0){
System.out.println( c + " Banana");
}else if (c%b!=0 &&| c%a!= 0) {
c++;
}
}
input.close();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…