Function giving incorrect result

rickyjoepr

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.");
  }
}
jhamon

osumand 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
  • etc.

If you want to get the decimal value of this division, you can

  • define your rate as double type in order to contain decimal numbers.
  • cast at least one of your varaible as a double in the operation

You should end with something like:

double rate = (((double) osum)/rsum)*100;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cast address of function to UINTPTR_T giving me incorrect result

Topological Sort giving incorrect result

Datetime comparison is giving incorrect result

TObjectList<T>.IndexOf giving incorrect result

Laravel ORM is giving an incorrect result on a simple query

Eigen LLT Module Giving incorrect result?

Golang program giving incorrect result in Windows 10

C#: ShortDatePattern giving incorrect result

Date difference using Periods is giving incorrect result

Number comparison in C is giving incorrect result

C# sort with Comparer is giving incorrect result

NSRegularExpression is giving incorrect result from the numberOfRanges method

Dataframe Row to JSON giving incorrect result

Concat function not giving desired result

Using len with get() function giving incorrect statistics?

tan function giving incorrect values in matlab

Verilog's display function is giving an incorrect output?

Date function returning incorrect result

Kernel adapted from CUDA documentation giving incorrect result

Count Aggregation on filtered nested object giving incorrect result

MS Access Case sensitive query giving incorrect result

C Program to read number character by character giving incorrect result

SQL Server Date time conversion giving incorrect result?

openGL drawing GL_LINES giving incorrect result

time_t to boost date conversion giving incorrect result

Simple program to compare an array running but giving incorrect result

Why does persistenceEnabled: true result in my queries giving incorrect results?

Sorted and sort function not giving same result

Returning a function in a React hook giving unexpected result