Question by fernand: Need Homework help – Java program “for loops”?
Hello, I need help with a java program I am doing. The program calculates the occupancy of a hotel by asking how many floors there are, how many rooms there are, how many rooms are occupied and what percentage of the rooms are occupied. The program should exclude a 13th floor and not have a number less than one for number of floors and not less than 10 rooms for the amount of rooms. I have a java program written out that compiles and runs but doesn’t give me the output that I want. Pls help. Here is the program;

//This is a program that calculates the occupancy rate for a hotel by asking the user for the number
//of floors a hotel has, the number of rooms in a floor and how many of them are occupied. The program
//then calculates the percentage of rooms that are occupied and displays the information on the screen

import java.util.Scanner;

import java.text.DecimalFormat;//imports the decimel format class to format the output

public class Testhomework7
{
public static void main (String[]args)
{
Scanner keyboard = new Scanner(System.in);//connects keyboard scanner object to keyboard
DecimalFormat formatter = new DecimalFormat(“0.00%”);//assigns “0.00” decimal amount for
//any amount output

int numFloors = 0;
int numRooms = 0;
int roomsOccupied = 0;
int total = 0; /
double occupancy = 0;

System.out.println (“Please enter the number of floors in the hotel”);
numFloors = keyboard.nextInt();

System.out.println (“Please enter the number of rooms in each floor”);
numRooms = keyboard.nextInt();

for( int floor = 1; floor <= numFloors; floor++) { total = 0; System.out.println ("Please enter the number of rooms in each floor"); numRooms = keyboard.nextInt(); System.out.println ("The number of rooms on floor" + floor +"that is occupied is" + occupied); occupancy = (roomsOccupied/numRooms) * 100; total = numFloors * numRooms; } } System.out.println ("The Hotel has " +total+ " Total Rooms."); total = total + numRooms; System.out.println (roomsOccupied+ " of the Rooms are Occupied."); System.out.println ("So " + formatter.format(occupancy) + "of the Rooms are Occupied"); occupancy = (roomsOccupied/numRooms) * 100; } } Best answer:

Answer by Tasm
Look at your for loop. It never prints the totals stuff before resetting it to 0 again. I think you just need to take out the

}
}

——————
total = numFloors * numRooms;

}
}
System.out.println (“The Hotel has ” +total+ ” Total Rooms.”);
total = total + numRooms;

What do you think? Answer below!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.