第一次循环后,while循环无法正确打印

时间:2014-03-30 23:44:10

标签: java

我正在尝试做一个do-while循环,使用户输入" y"之后程序再次迭代。或者" Y",但每次我运行程序时,都打印出类似这样的内容:

Would you like to try again?
Enter Y for yes or N for no:  [DrJava Input Box] (I enter y)
Are you a student? (no input box is shown, and it skips it)
Are you a member of staff or faculty? [DrJava Input Box] (i enter yes or no)
How many tickets do you need? [DrJava Input Box] (I enter an int, but it doesnt complete that part where it shows how many tickets sold or how much it costs)
Would you like to try again? 
Enter Y for yes or N for no: [DrJava Input Box]

这就是我的程序:

import java.util.Scanner;

public class Ticket
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        double ticketprice = 12.00;
        double result;
        double result2;
        double result3;
        char repeat;
        String input;
        String student;
        String staff;
        int ticket;

        do
        {
            System.out.println("Are you a student?");
            student = keyboard.nextLine();


            System.out.println("Are you a member of staff or faculty?");
            staff = keyboard.nextLine();


            System.out.println("How many tickets do you need?");
            ticket = keyboard.nextInt();



            if(student.equals("yes"))
            {
                System.out.println("Here is your " + ticket + " tickets for $0.00");
                }


            if(staff.equals("yes"))
            {
                result = ticketprice * .85;
                result2 = ticket * result;
                System.out.printf("Here are your " + ticket + " tickets for $%.2f\n", result2);
                }



            if(student.equals("no") && staff.equals("no"))
            {
                result3 = ticket * ticketprice;
                System.out.printf("Here are your " + ticket + " tickets, for $%.2f\n", result3);
                }


            System.out.println("Would you like to try again?");
            System.out.print("Enter Y for yes or N for no: ");
                input = keyboard.next();
            repeat = input.charAt(0);

        }
        while(repeat == 'y' || repeat == 'Y');

    }
}

我是编程的初学者,所以任何帮助都会很好。谢谢。

2 个答案:

答案 0 :(得分:0)

在循环结束时,您拨打next()以阅读"再次尝试"响应。这将读取下一个标记,但仍然会在输入流上结束该行。

下一次循环播放时,当您致电nextLine()阅读"您是学生时#34;响应,它只是立即读取该行的其余部分。

最简单的解决方案是:

  • 使用nextLine()而不是next()为您的"再试一次"提示,但这意味着你必须在"门票"中处理nextInt()左边的行结尾。问题,那么你 也必须......
  • 使用nextLine()代替nextInt()代替您的"门票"问题,然后使用Integer.parseInt()将字符串解析为int。

备用选项,因为您的所有回复似乎只是单字回复,​​所以在任何地方都使用next() / nextInt()而根本不使用nextLine()。关键是,当你混合两者时,你必须知道它们是如何相互作用的。

答案 1 :(得分:0)

问题在于这段代码:

        System.out.println("Would you like to try again?");
        System.out.print("Enter Y for yes or N for no: ");
        input = keyboard.next();
        repeat = input.charAt(0);

当你调用keyboard.next()时,它不会读取整行,所以当你的程序再次循环时,下次调用keyboard.nextLine()时,它会占用你之前输入的部分内容在循环中,它超越了自己。

最好的解决方案是在调用keyboard.next()之后添加一个keyboard.nextLine(),这样就会消耗掉该行的剩余部分,并且不会遗留下来以破坏未来的迭代次数。循环。

例如,您可以像这样更改它:

        System.out.println("Would you like to try again?");
        System.out.print("Enter Y for yes or N for no: ");
        input = keyboard.next();
        keyboard.nextLine();
        repeat = input.charAt(0);