Как бы я использовал while l oop, чтобы получить несколько входов? - PullRequest
0 голосов
/ 02 мая 2020

Я хотел бы создать программу, которая попросит пользователя ввести количество клиентов для бронирования билетов, прочитает введенный номер, выполнит l oop, чтобы узнать возраст каждого клиента, и определит цену за единицу для каждый клиент. мой код до сих пор имеет только операторы 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");
        }
    }    
}

Как мне добавить l oop, чтобы задать возраст каждого клиента?

Ответы [ 2 ]

0 голосов
/ 02 мая 2020

Ниже приведен код с использованием пока l oop

Scanner sc = new Scanner(System.in);

int customerCount = sc.nextInt();

int[] ageOfCustomers = new int[customerCount];
int i=0;
while(customerCount-- > 0) {

    ageOfCustomers[i++] = sc.nextInt();
}

System.out.println(Arrays.toString(ageOfCustomers));
0 голосов
/ 02 мая 2020

Здесь вы go:

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));
...