I am starting Java and also new to this site. I learn about arrays and methods, and unfortunately I'm stuck. The question is:
The hospital has room for 150 patients. Each room has space for 3 patients.
The hospital pays the patient $ 150 to stay. If 3 patients are occupied in each room, the hospital charges an additional $ 50. Tell the user the number of patients and show the total number of patients, the total number of all fees.
In my opinion, I would like to create a parallel array from the number of rooms and another array of the number of patients for each room.
The problem is this: every time the program asks for the following. Enter # patients in room 0 Enter # patients in room 0 Enter # patients in room 0 Instead: Enter # patients for number 1 Enter # patients for number 2 Enter # patients for number 3
So, I think this is a logical mistake, because roomNumbers [i] is not updated to room 1, room2, etc., it asks for number 0 all the time, please, every advice is welcome, as I am just starting with this new course for the next semester. Does my logic really make sense? Thank you very much.
import javax.swing.JOptionPane; public class Pingo { public static void main(String[] args) { final int MAXROOMS=50; int[] roomNumbers = new int[MAXROOMS]; int [] patientQuantity = new int [roomNumbers.length]; int numPatients=getNumberOfPatients(roomNumbers,patientQuantity); } public static int getNumberOfPatients(int[] roomNumbers, int []patientQuantity){ int numPatients=0; for(int i=0; i<patientQuantity.length; i++){ numPatients=Integer.parseInt(JOptionPane.showInputDialog("Enter amount of Patients for room:" + roomNumbers[i] )); patientQuantity[i]=numPatients; } return numPatients; } }
source share