在java中使用scanner的问题

时间:2017-01-17 06:40:17

标签: java java.util.scanner

我有一项任务,我们假设为书店创建一个程序,帮助他们估算自己的业务。为此,我们假设要求用户提供各种输入,例如书籍的代码,书籍的单一复制成本,当前的书籍数量,预期的课程注册等等。但是,当用户输入无效输入时,我遇到问题,例如当预期登记无效时,程序开始从问题开始提问而不是再次提出相同的问题。

任何人都可以帮我找出我做错了什么吗?这是我的代码。

提前致谢。

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

2 个答案:

答案 0 :(得分:0)

你在循环中问问题。当您的某个条件失败时,程序将继续超过所有后续条件(如果它们也不正确)并再次开始LOOP。要纠正此问题,您可能会在循环内发生每个条件。像

这样的东西

while(错误条件回答):            再问一次

要挑战自己(并改进),请将代码尽可能少地保留!

祝你的编码旅程好运:))

答案 1 :(得分:0)

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

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