An organization wants to take a trip. We need enough busses to fit everyone. (We can't tell a few people that they can't go.)
Notice that the ceil function will give us the correct number of busses. However, if we divide 30/25 we will get just one. To find the number of busses we need the ceiling (Math.ceil) function. However, ceil requires a double as an argument. Multiplying an int by 1.0 converts it to double. We could also use Math.ceil((double)numPeople/numSeats) to cast to double.
How many people are going:20 How many seats are there on a bus:16 You will need 2 busses.
1 | //How many busses are needed? package busses; import java.util.*; public class Busses { public static void main(String[] args) { Scanner keyboard=new Scanner(System.in); //do just once int numPeople, numSeats, numBusses; System.out.print("How many people are going:"); numPeople=keyboard.nextInt(); System.out.print("How many seats are there on a bus:"); numSeats=keyboard.nextInt(); int numberBusses=(int)(Math.ceil(1.0*numPeople/numSeats)); System.out.println("You will need " + numberBusses + " busses."); }//main }//class |
Notice that we have to cast to int to print 2 busses instead of 2.0 busses.