There are 70 seats in the cinema hall. It is necessary to count with each sale how many tickets are left, so that after 70 tickets there is a message that all tickets are sold out. So, tell me what to change and-or what to add to the code to implement this function.

import java.util.*; public class Kino { public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println("Сколько Вам лет?"); int alt = scn.nextInt(); if (alt <= 5) { System.out.println("Цена билета: бесплатно"); } else if (alt >= 5 && alt <= 12) { System.out.println("Цена билета: 5р."); } else if (alt >= 13) { System.out.println("Цена билета: 10р."); } } } 
  • 3
    If you want the problem to be solved for you, then you should contact the freelancers exchange. Stack Overflow is a resource for people who want to learn how to solve problems on their own. And in your code there is not a single hint that you tried to implement this functionality. - neluzhin
  • you need to add the total number of tickets (70), an endless cycle that ends when there are no more tickets and at each iteration reduce the counter (70) to zero - Alexey Shimansky
  • It is almost solved, I can not figure out how to sort the number of tickets that remained. Just. - Max
  • @ Max you have here, by the way can not see anything from the countdown tickets) - Alexey Shimansky
  • Thanks for the hint, now I will try. - Max

2 answers 2

Here is an example:

 import java.util.*; public class Kino { static int countOfTickets = 70; public static void main(String[] args) { while (countOfTickets > 0) { Scanner scn = new Scanner(System.in); System.out.println("Сколько Вам лет?"); int alt = scn.nextInt(); countOfTickets--; if (alt <= 5) { System.out.println("Цена билета: бесплатно"); } else if (alt >= 5 && alt <= 12) { System.out.println("Цена билета: 5р."); } else if (alt >= 13) { System.out.println("Цена билета: 10р."); } } System.out.println("Все билеты распроданы!"); } } 

    In general, something like this:

     int tickets = 3; Scanner scn = new Scanner(System.in); while (true) { if (tickets == 0) { System.out.println("Все билеты проданы!"); break; } System.out.println("Сколько Вам лет?"); int alt = scn.nextInt(); if (alt <= 5) { System.out.println("Цена билета: бесплатно"); } else if (alt >= 5 && alt <= 12) { System.out.println("Цена билета: 5р."); } else if (alt >= 13) { System.out.println("Цена билета: 10р."); } tickets--; } 

    There is a ticket counter (in this case there are 3 of them), an infinite loop that ends when there are no more tickets ( if (tickets == 0) ) and decrease the counter at each iteration.