我有这个Java代码。这是一个简单的小东西,我试图通过除去占用房间的总数,但房间的总数来获得酒店的入住率。但是,当我尝试编译代码时,我得到了:
C:\Users\Jerome\Documents\Computer Concepts and Programming\Lab04\JRGlab04Num07\JRGlab04Num07.java:62: error: variable totalORooms might not have been initialized
totalORooms += numORooms;
^
C:\Users\Jerome\Documents\Computer Concepts and Programming\Lab04\JRGlab04Num07\JRGlab04Num07.java:63: error: variable totalRooms might not have been initialized
totalRooms += numRooms;
^
C:\Users\Jerome\Documents\Computer Concepts and Programming\Lab04\JRGlab04Num07\JRGlab04Num07.java:68: error: variable totalORooms might not have been initialized
ORate = totalORooms / totalRooms;
^
C:\Users\Jerome\Documents\Computer Concepts and Programming\Lab04\JRGlab04Num07\JRGlab04Num07.java:68: error: variable totalRooms might not have been initialized
ORate = totalORooms / totalRooms;
^
4 errors
Tool completed with exit code 1
那么为什么它会忘记我已经说过的变量。
import java.util.Scanner; //Needed for Inputting
import java.text.DecimalFormat;//Needed for Rounding
/**
The pourpose of this lab is:
To calculate the occupancy rate of a hotel
by JEROME GERO Lab 04 Number 07
*/
public class JRGlab04Num07
{//Begin Class
public static void main(String[] args)
{//Begin Main Method
//Variables and introduction
double totalFloors, floor, numRooms, numORooms;
double totalRooms;
double totalORooms;
double ORate;
System.out.println("Welcome to the Hotel Occupancy Calculator.\nPlease enter the number of floors we will be dealing with:");
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0.00");
totalFloors = keyboard.nextInt();
floor = 0;
while(totalFloors < 1)
{
System.out.println("No, lets try that again.");
totalFloors = keyboard.nextInt();
}
System.out.println("Okay");
while(totalFloors > 0)
{
++floor;
System.out.println("Now enter the number of rooms on floor " + floor);
numRooms = keyboard.nextDouble();
while(numRooms < 10){
System.out.println("The Number of rooms nust be greater than 10.\nPlease enter the number of rooms.");
numRooms = keyboard.nextDouble();
}
System.out.println("Now enter the number of occupied rooms on that floor:");
numORooms = keyboard.nextDouble();
while(numORooms > numRooms){
System.out.println("No, the Number of Occupied rooms can not be more than the number of rooms on this floor.\n" +
"Please enter that again.");
numORooms = keyboard.nextDouble();
}
totalORooms += numORooms;
totalRooms += numRooms;
--totalFloors;
}
ORate = totalORooms / totalRooms;
System.out.println(ORate);
}//End Main Method
}//End Class
答案 0 :(得分:1)
您没有为totalRooms分配值。该错误告诉您它尚未初始化。
double totalRooms = 0.0;