I know something is wrong with my variables storing the info.
At the end of the program, it prints 0.0 for rate.
I have been trying to figure out the error in logic or syntax in my program. I believe it may have something to do with the variables being 0, I have also tried inserting the function into the loop when count == floor.
import javax.swing.*;
public class pickanumber {
public static void main(String[] args) {
int floor=1, rooms, occupants, rsum = 0, osum = 0;
String amount;
do {
amount = JOptionPane.showInputDialog("Enter total floors.");
floor = Integer.parseInt(amount);
} while (floor <= 0);
for (int count = 1; count <= floor; count++) {
if (floor==13) count++;
do {
amount = JOptionPane.showInputDialog("Enter total rooms on floor "+count+".");
rooms = Integer.parseInt(amount);
} while(rooms <= 9);
rsum += rooms;
do {
amount = JOptionPane.showInputDialog("Enter occupants on floor "+count+".");
occupants = Integer.parseInt(amount);
} while ((occupants > rooms)||(occupants < 0));
osum += occupants;
}
int rate = (osum/rsum)*100;
JOptionPane.showInputDialog("occupancy is at "+rate+"% capacity.");
}
}
osum
and rsum
are both integers, they represent numbers without decimal.
Any math opertion using only those 2 integers will return an integer.
So osum/rsum
will return
0
if osum < rsum
1
if rsum <= osum < 2*rsum
If you want to get the decimal value of this division, you can
rate
as double
type in order to contain decimal numbers.double
in the operationYou should end with something like:
double rate = (((double) osum)/rsum)*100;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments