如何在 while 循环中将所有双打加在一起,java
how to add together all doubles in a while loop, java
while (read.hasNext()) {
String roomType = read.nextLine();
int rooms = read.nextInt();
double price = read.nextDouble();
double roomIncome = (double) (rooms*price);
double tax = (double) (price*rooms*defaultTax);
double totalIncome = roomIncome;
roomIncome++;
read.skip("\s+");
System.out.println("Room type: " + roomType + " | No. of rooms: " + rooms + " | Room price: " + price + " | income: " + roomIncome + " | tax: " + tax);
System.out.println("The total room income is: " + totalIncome);
我需要将所有 roomIncome 加在一起得到 totalIncome 输出。目前我的输出是:
Room type: Single | No. of rooms: 5 | Room price: 23.5 | income: 118.5 | tax: 23.5
The total room income is: 117.5
Room type: Double | No. of rooms: 3 | Room price: 27.5 | income: 83.5 | tax: 16.5
The total room income is: 82.5
Room type: Suite | No. of rooms: 2 | Room price: 50.0 | income: 101.0 | tax: 20.0
The total room income is: 100.0
我的预期输出应该是所有 roomIncome 加在一起的总和。所以在这种情况下,它应该加起来为 300。相反,它向 roomIncome 加了 1,并给了我 100
的输出
如果要打印所有房型的总收入,需要在循环外维护变量,在循环内不断加值,如下:
double allRoomsTotalIncome = 0.0;
while (read.hasNext()) {
String roomType = read.nextLine();
int rooms = read.nextInt();
double price = read.nextDouble();
double roomIncome = (double) (rooms*price);
double tax = (double) (price*rooms*defaultTax);
double totalIncome = roomIncome;
allRoomsTotalIncome = allRoomsTotalIncome + totalIncome;
roomIncome++;
read.skip("\s+");
System.out.println("Room type: " + roomType + " | No. of rooms: " + rooms + " | Room price: " + price + " | income: " + roomIncome + " | tax: " + tax);
}
System.out.println("The total room income is: " + allRoomsTotalIncome);
while (read.hasNext()) {
String roomType = read.nextLine();
int rooms = read.nextInt();
double price = read.nextDouble();
double roomIncome = (double) (rooms*price);
double tax = (double) (price*rooms*defaultTax);
double totalIncome = roomIncome;
roomIncome++;
read.skip("\s+");
System.out.println("Room type: " + roomType + " | No. of rooms: " + rooms + " | Room price: " + price + " | income: " + roomIncome + " | tax: " + tax);
System.out.println("The total room income is: " + totalIncome);
我需要将所有 roomIncome 加在一起得到 totalIncome 输出。目前我的输出是:
Room type: Single | No. of rooms: 5 | Room price: 23.5 | income: 118.5 | tax: 23.5
The total room income is: 117.5
Room type: Double | No. of rooms: 3 | Room price: 27.5 | income: 83.5 | tax: 16.5
The total room income is: 82.5
Room type: Suite | No. of rooms: 2 | Room price: 50.0 | income: 101.0 | tax: 20.0
The total room income is: 100.0
我的预期输出应该是所有 roomIncome 加在一起的总和。所以在这种情况下,它应该加起来为 300。相反,它向 roomIncome 加了 1,并给了我 100
的输出如果要打印所有房型的总收入,需要在循环外维护变量,在循环内不断加值,如下:
double allRoomsTotalIncome = 0.0;
while (read.hasNext()) {
String roomType = read.nextLine();
int rooms = read.nextInt();
double price = read.nextDouble();
double roomIncome = (double) (rooms*price);
double tax = (double) (price*rooms*defaultTax);
double totalIncome = roomIncome;
allRoomsTotalIncome = allRoomsTotalIncome + totalIncome;
roomIncome++;
read.skip("\s+");
System.out.println("Room type: " + roomType + " | No. of rooms: " + rooms + " | Room price: " + price + " | income: " + roomIncome + " | tax: " + tax);
}
System.out.println("The total room income is: " + allRoomsTotalIncome);