Results 1 to 8 of 8
Thread: Keep getting result of 0.
- 06-09-2011, 04:39 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Keep getting result of 0.
I know I had to set the variables "occupied" and "total" to 0 to avoid getting an error, but whenever I run it, I get the "rate" keep equaling to 0. I need the rate to be the number of occupied rooms divided by the number of total rooms.
Java Code:import java.util.Scanner; //gets the Scanner public class LBHotelOccupancy { //begin class public static void main(String[] args) { //begin main method int floor, floors; //The floor number and the total number of floors, respectively. Scanner input = new Scanner(System.in); //Politeness and and intro to the program System.out.println("Hello! I can calculate the occupancy rate for each floor in your hotel."); System.out.println("Please answer the following questions."); //Get the number of floors System.out.print("How many floors are in your hotel? "); floors = input.nextInt(); for(floor = 1; floor <= floors; floor++) //will increment the floor number with each loop until the total floors is reached { System.out.print("How many rooms are on floor " + floor + "? "); int total = input.nextInt(); //get total rooms on floor System.out.print("How many rooms on floor " + floor + " are occupied? "); int occupied = input.nextInt(); //get occupied rooms on floor System.out.println(); double rate = occupied / total; //calculation for occupancy rate System.out.println("The occupancy rate for floor " + floor + " is " + rate + "."); //display occupancy rate System.out.println(); } } //end main method } //end class
- 06-09-2011, 04:47 AM #2
occupied and total are ints therefore integer division is applied. 5 / 9 = 0 not 0.5555555555555
-
You're doing int division since total and occupied are both ints, and the result of an int division is always an int. So if you have 5/6 or 2/9 you'll get 0. The solution is to cast either the numerator or denominator as doubles in your division.
- 06-09-2011, 04:49 AM #4
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
Division of two ints will always result in an int; in this case, variable 'total' is always greater than or equal to variable 'occupied', so rate will always be zero.
- 06-09-2011, 04:58 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
- 06-09-2011, 04:59 AM #6
What difference will that make. Giving a variable a different name will not change the behaviour of the code.
- 06-09-2011, 05:00 AM #7
Dookie
Ignore that post. The three previous posts explain your problem.
- 06-09-2011, 05:01 AM #8
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Struts 2 error : No result defined for action / result
By sameerk in forum Web FrameworksReplies: 1Last Post: 05-17-2011, 10:15 AM -
Java Result: 255
By MadJack in forum New To JavaReplies: 14Last Post: 11-11-2010, 08:54 AM -
How to format the result
By galagali in forum New To JavaReplies: 3Last Post: 07-11-2010, 12:15 AM -
uncorrect result
By jamborta in forum New To JavaReplies: 3Last Post: 11-11-2009, 01:17 PM -
getting a random result
By gradon in forum New To JavaReplies: 2Last Post: 07-19-2007, 03:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks