我正在嘗試做一個do-while循環,使得程序在用戶輸入「y」或「Y」後再次迭代,但每次運行程序時,它都會打印出來是這樣的:while循環在第一個循環後沒有正確打印
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');
}
}
我是一個初學者到編程,所以任何幫助將是一件好事。謝謝。
[使用後nextInt跳躍nextLine()()(可能重複http://stackoverflow.com/questions/13102045/skipping-nextline -after使用-nextint) –