简体   繁体   中英

New To Java, Can't figure out to use a while loop to end user input

I'm new to Java and i'm currently trying to make a while loop that will loop if the user inputs Yes. Also i want it it to prompt the user to input "endinput" if they instead put Stop. Any ideas? Just started Java programming and this is stumping me as I can't figure how to do continue.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String[]
        title=new String[100],
        author=new String[100],
        publisher=new String[100],
        ISBN=new String[100],
        endinput=new String[100];
        boolean Stop = false;
        boolean Yes = true;
        double[] price=new double[100];

        System.out.println("Welcome To Kieran's Bookstore");

        System.out.println("Input The Title:");
        title[0] = scan.next(); 
        System.out.println("Input The Author:");
        author[0] = scan.next();
        System.out.println("Input The Price Of The Book:");
        scan.next();
        System.out.println("Input The Publisher:");
        publisher[0]= scan.next();
        System.out.println("Input The ISBN:");
        ISBN[0]=scan.next();
        System.out.println("Would you like to continue?(Yes/Stop)");
        scan.next();

        while(!Stop)
            {
            System.out.println("Input The Title:");
            title[0] = scan.next(); 
            System.out.println("Input The Author:");
            author[0] = scan.next();
            System.out.println("Input The Price Of The Book:");
            scan.next();
            System.out.println("Input The Publisher:");
            publisher[0]= scan.next();
            System.out.println("Input The ISBN:");
            ISBN[0]=scan.next();
            System.out.println("Would you like to continue?(Yes/Stop)");
            scan.equals(Stop);
        }
        while (!Stop) {System.out.println("Please Type 'endinput':");
          scan.next();
        }
        System.out.println("Please Type 'endinput':");
        scan.next();
        if (scan.equals("endinput"))System.exit(0);
        scan.close();

You only need to use one while to achieve that . Assign a variable to the scan.next() and do the checking using if .

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String[] title = new String[100], author = new String[100], publisher = new String[100], ISBN = new String[100],
            endinput = new String[100];
    // boolean Stop = false;
    boolean Yes = true;
    double[] price = new double[100];

    while (Yes) {
        System.out.println("Welcome To Kieran's Bookstore");
        System.out.println("Input The Title:");
        title[0] = scan.next();
        System.out.println("Input The Author:");
        author[0] = scan.next();
        System.out.println("Input The Price Of The Book:");
        scan.next();
        System.out.println("Input The Publisher:");
        publisher[0] = scan.next();
        System.out.println("Input The ISBN:");
        ISBN[0] = scan.next();
        System.out.println("Would you like to continue?(Yes/Stop)");
        String ans = scan.next();
        if (ans.equals("endinput") || (ans.equals("Stop"))) {
            Yes = false;
            System.exit(0);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM