如何使用while循环获取多个输入?

huangapple 未分类评论44阅读模式
英文:

How would I use a while loop to get multiple inputs?

问题

我想制作一个程序,让用户输入预订车票的顾客人数,读取输入的数字,执行循环以询问每位顾客的年龄,并确定每位顾客的单价。
我的代码目前只有if else语句:

if (customerP < 4) {
    System.out.println("顾客1的单价为:0");
} else {
    if (customerP < 13) {
        System.out.println("顾客1的单价为:5");
    } else {
        if (customerP < 19) {
            System.out.println("顾客1的单价为:8");
        } else {
            System.out.println("顾客1的单价为:10");
        }
    }
}

我应该如何添加一个while循环来询问每位顾客的年龄?

英文:

I'd like to make a program that asks the user to enter the number of customers for booking tickets, reads the entered number, performs the loop to ask the age of each customer, and decide the unit price for each customer.
my code so far only has the if else statements:

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

How do I add a while loop to ask the to ask the age for each customer?

答案1

得分: 0

这是您的代码翻译部分:

Scanner scanner = new Scanner(System.in);

System.out.println("输入客户数量:");
int noCustomers = scanner.nextInt();

if (noCustomers < 1) {
    throw new IllegalArgumentException("客户数量必须大于等于1");
}

int[] customerAges = new int[noCustomers];

for (int i = 0; i < noCustomers; i++) {
    System.out.println("输入客户的年龄:" + i);
    customerAges[i] = scanner.nextInt();
}

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

请注意,我只翻译了代码部分,没有包括注释或其他内容。

英文:

Here you go:

Scanner scanner = new Scanner(System.in);

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

if (noCustomers &lt; 1) {
     throw new IllegalArgumentException(&quot;No of Customers has to be 1 or greater&quot;);
}

int[] customerAges = new int[noCustomers];

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

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

答案2

得分: 0

&lt;i&gt;以下提供使用 while 循环的代码&lt;/i&gt;
Scanner sc = new Scanner(System.in);

int customerCount = sc.nextInt();

int[] ageOfCustomers = new int[customerCount];
int i=0;
while(customerCount-- &gt; 0) {
    
    ageOfCustomers[i++] = sc.nextInt();
}

System.out.println(Arrays.toString(ageOfCustomers));
英文:

<i>Below provides code using while loop</i>

Scanner sc = new Scanner(System.in);

int customerCount = sc.nextInt();

int[] ageOfCustomers = new int[customerCount];
int i=0;
while(customerCount-- &gt; 0) {
    
    ageOfCustomers[i++] = sc.nextInt();
}

System.out.println(Arrays.toString(ageOfCustomers));

huangapple
  • 本文由 发表于 2020年5月2日 16:46:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/61556653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定