Java新手。因此,我希望用户能够通过键入" quit"随时停止付款流程。我怎样才能做到这一点?尝试了do-while循环,但它不起作用。这是代码
public void payItems() {
Scanner s = new Scanner(System.in);
String word;
do {
System.out.println("Enter name: ");
String name = s.next();
s.nextLine();
//where should I put word = s.next
System.out.println("Enter address: ");
String address = s.nextLine();
System.out.println("Enter card number: ");
int cardNo = s.nextInt();
System.out.println("Enter card expiration date: (DDMMYY)");
int expirationDate = s.nextInt();
Customer newcust = new Customer(name, address, cardNo, expirationDate);
} while (!word.equals("quit"));
}
答案 0 :(得分:0)
您只需按以下步骤更改代码
即可 boolean status=true;
do {
Scanner s = new Scanner(System.in);
System.out.println("Enter name: ");
String name = s.nextLine();
System.out.println("Enter address: ");
String address = s.nextLine();
System.out.println("Enter card number: ");
int cardNo = s.nextInt();
System.out.println("Enter card expiration date: (DDMMYY)");
int expirationDate = s.nextInt();
System.out.println("You want to add more or quit(type continue to add another, type quit to exit): ");
String word=s.next();
if(word.equals("quit")){
status=false;
}
} while (status);
答案 1 :(得分:0)
所以你必须做这样的事情:
Scanner s = new Scanner(System.in);
System.out.println("Enter name: ");
String name = s.next();
s.nextLine();
if (name.equals("quit")
return;
//where should I put word = s.next
System.out.println("Enter address: ");
String address = s.nextLine();
if (address.equals("quit")
return;
System.out.println("Enter card number: ");
int cardNo = s.nextInt();
System.out.println("Enter card expiration date: (DDMMYY)");
int expirationDate = s.nextInt();
Customer newcust = new Customer(name, address, cardNo, expirationDate);
但它不适用于cardNo和expiration,因为这些是int值,因此您无法输入“quit”。