gpt4 book ai didi

java - 我将如何使用 while 循环来获取多个输入?

转载 作者:行者123 更新时间:2023-12-01 16:57:54 25 4
gpt4 key购买 nike

我想制作一个程序,要求用户输入订票的客户数量,读取输入的数字,执行循环询问每个客户的年龄,并确定每个客户的单价。到目前为止,我的代码只有 if else 语句:

if (customerP <4){
System.out.println("The unit Price of customer 1 is: 0");
}else {
if(customerP <13) {
System.out.println("The unit price of customer 1 is: 5");
}else {
if(customerP <19) {
System.out.println("The unit price of customer 1 is: 8");
}else {
System.out.println("The unit price of customer 1 is: 10");
}
}
}

如何添加 while 循环来询问每个客户的年龄?

最佳答案

给你:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter No of Customers: ");
int noCustomers = scanner.nextInt();

if (noCustomers < 1) {
throw new IllegalArgumentException("No of Customers has to be 1 or greater");
}

int[] customerAges = new int[noCustomers];

for (int i = 0; i < noCustomers; i++) {
System.out.println("Enter age for customer: " + i);
customerAges[i] = scanner.nextInt();
}

System.out.println("Ages: " + Arrays.toString(customerAges));

关于java - 我将如何使用 while 循环来获取多个输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61556653/

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