java - 在java中使用扫描仪的问题

标签 java java.util.scanner

我有一项任务,我们应该为书店创建一个程序,帮助他们评估他们的业务。为此,我们假设要求用户提供各种输入,例如书籍的代码、书籍的单册成本、当前手头的书籍数量、预期的类(class)注册人数等。但是,当用户输入无效条目时,我遇到问题,例如当预期注册的条目无效时,程序会从头开始询问问题,而不是再次询问相同的问题。

谁能帮我看看我做错了什么?这是我的代码。

提前致谢。

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Welcome to College Store Sale Class.");
    System.out.println("Please enter all the info below as asked.");
    System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. ");



    double bookCost;
    int bookAvailable;
    String ending = sc.next();
    int prosepectiveEnrollment;
    String rOrO;    
    String nOrU;

    while(!ending.equals("exit")){



        System.out.print("Please enter the cost of book ($>=0). ");
        bookCost = sc.nextDouble();

        if(bookCost >= 0){

            System.out.print("Please enter the number of books available (n>=0). ");
            bookAvailable = sc.nextInt();

            if(bookAvailable >= 0){

                System.out.print("Please enter prospective class enrollement > 0: ");                   
                prosepectiveEnrollment = sc.nextInt();

                if(prosepectiveEnrollment > 0){

                    System.out.print("Is the book Required or Optional (ignoreCase)? ");
                    rOrO = sc.next();

                    if((rOrO.equalsIgnoreCase("Required") || rOrO.equalsIgnoreCase("Optional"))){

                        System.out.print("Is the book New or Used? Enter in 'N' or 'U' format. ");
                        nOrU = sc.next();
                        if((nOrU.equalsIgnoreCase("New") || nOrU.equalsIgnoreCase("Used"))){

                            System.out.println("Thanks for correct info, below is your result");

                        }

                        else{

                            System.out.println("(E) Please enter either 'New' or 'Used'. ");
                        }

                    }
                    else{

                        System.out.println("(D) Please enter either 'Required' or 'Optional'. ");
                    }

                }
                else{

                    System.out.println("(C) Number of prospective class enrollement must be > 0.");
                }
            }
            else{

                System.out.println("(B) Number of books should be >= 0.");
            }

        }

        else{

            System.out.println("(A) Cost should be >= 0. ");
        }
    }

    if(ending.equalsIgnoreCase("exit"))
        System.out.println("See ya later..!! Bbyee..");
    sc.close();
}

最佳答案

我已经尝试解决这个问题,也许你可以这样做,并让我知道这是否适合你

    public class Questionaire {

    public static void main(String[] args) {

        double bookCost = 0.0;
        int bookAvailable = 0;
        int prosepectiveEnrollment = 0;
        String rOrO = "";    
        String nOrU = "";

        try (Scanner sc = new Scanner(System.in)){
            System.out.println("Welcome to College Store Sale Class.");
            System.out.println("Please enter all the info below as asked.");
            System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. ");

            String ending = sc.next();
            System.out.println("Your response "+ending);

            while(!ending.equalsIgnoreCase("exit")){

                bookCost = promptQuestion("Please enter the cost of book ($>=0).", param -> {
                    return param >= 0;
                }, bookCost,sc);
                System.out.println("Your response "+bookCost);

                bookAvailable = promptQuestion("Please enter the number of books available (n>=0). ", param -> {
                    return param >= 0;
                }, bookAvailable, sc);
                System.out.println("Your response "+bookAvailable);

                prosepectiveEnrollment = promptQuestion("Please enter prospective class enrollement > 0: ", param -> {
                    return param > 0;
                }, prosepectiveEnrollment, sc);
                System.out.println("Your response "+prosepectiveEnrollment);

                rOrO = promptQuestion("Is the book Required or Optional (ignoreCase)? ", param -> {
                    return param.equalsIgnoreCase("Required") || param.equalsIgnoreCase("Optional");
                }, rOrO, sc);
                System.out.println("Your response "+rOrO);

                nOrU = promptQuestion("Is the book New or Used? Enter in 'N' or 'U' format. ", param -> {
                    return (param.equalsIgnoreCase("New") || param.equalsIgnoreCase("Used")
                            || param.equalsIgnoreCase("N") || param.equalsIgnoreCase("U"));
                }, nOrU, sc);
                System.out.println("Your response "+nOrU);

                System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. ");
                ending = sc.next();
                System.out.println("Your response "+ending);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static Integer promptQuestion(String question, Validation<Integer> validation, Integer input, Scanner sc) {
        do {
            System.out.println(question);
            input = sc.nextInt();
        } while(!validation.validate(input));
        return input;
    }

    static Double promptQuestion(String question, Validation<Double> validation, Double input, Scanner sc) {
        do {
            System.out.println(question);
            input = sc.nextDouble();
        } while(!validation.validate(input));
        return input;
    }

    static String promptQuestion(String question, Validation<String> validation, String input, Scanner sc) {
        do { 
            System.out.println(question);
            input = sc.next();
        } while(!validation.validate(input));
        return input;
    }

    @FunctionalInterface
    private static interface Validation<T> {
        boolean validate(T t);
    }
}

关于java - 在java中使用扫描仪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41690543/

相关文章:

Java时间戳转换错误

java - Java中的图形问题

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 如何更改Java中的分隔符?

java - 在Python中无法连接到stomp(activemq)推送数据服务器,而在Java中却成功

java - 正确对齐控制台输出表中的列并从零开始计数

java - 将列表中的值输入 sparql 查询

Java Scanner hasNextDouble() 在第一个实例后无法正常工作

Java - 如何将字符串扫描到数组中?

java - 我需要鼠标监听器事件处理程序中或外部的数据