Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

javascript - What should I do to fix my While Loop, it keeps looping

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();

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...