gpt4 book ai didi

java - 我的程序跳过让用户输入一个变量的值

转载 作者:行者123 更新时间:2023-12-02 15:04:05 25 4
gpt4 key购买 nike

在此程序中,我使用预设值从 CarOrder 类创建 2 个对象。然后,我要求用户提供另外 2 组值来创建另外 2 个对象。不幸的是,在输入第一个的纳税状态后,它将跳过让用户在第二个上输入买家的值。为什么它随机跳过这一个问题?

public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
CarOrder speedy = new CarOrder("Speedy Rental", "Mini Cooper", 22150, 15, true);
CarOrder zip = new CarOrder("Zip Car Co.", "Ford Fusion", 27495, 6, true);

System.out.println("Enter Buyer: ");
String buyer1 = keyboard.nextLine();
System.out.println("Enter the type of car being purchased: ");
String car1 = keyboard.nextLine();
System.out.println("Enter the cost of this purchase: ");
double cost1 = keyboard.nextDouble();
System.out.println("Enter quantity of cars being purchased: ");
int quantity1 = keyboard.nextInt();
System.out.println("Enter tax status: ");
boolean tax1 = keyboard.nextBoolean();

System.out.println("Enter Buyer: ");
String buyer2 = keyboard.nextLine();
System.out.println("Enter the type of car being purchased: ");
String car2 = keyboard.nextLine();
System.out.println("Enter the cost of this purchase: ");
int cost2 = keyboard.nextInt();
System.out.println("Enter quantity of cars being purchased: ");
int quantity2 = keyboard.nextInt();
System.out.println("Enter tax status: ");
boolean tax2 = keyboard.nextBoolean();

CarOrder state = new CarOrder(buyer1, car1, cost1, quantity1, tax1);
CarOrder it = new CarOrder(buyer1, car2, cost2, quantity2, tax2);

System.out.println("Chicago Car Wholesalers" );
System.out.println("Oct. 30th, 2012");
System.out.println("New Car Order Report");

}

}

最佳答案

我认为 nextBoolean() 正在消耗 boolean 值,但保留行尾字符,然后您的 nextLine() 将其用作其输入。因此,请在询问第二个买家之前添加 keyboard.nextLine()

关于java - 我的程序跳过让用户输入一个变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15231778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com