import java.util.Scanner;
class objectDetails {
double w, t, res, amt;
String ob;
void getDetails(int n) {
Scanner get = new Scanner(System.in);
int limit = n;
System.out.println("Enter Object Details\n");
for (int i = 0; i < limit; i++) {
System.out.println("Name of appliance:\n");
ob = get.next();
System.out.println("Enter the amount of watts per hour:\n");
w = get.nextDouble();
System.out.println("Enter the amount of time in hours used:\n");
t = get.nextDouble();
res = (w * t) / 1000;
amt = (res * 4.2);
System.out.println("The amount in ruppes payable=" + amt);
}
}
}
我想要循环中所有迭代的amt之和。添加所有amt并显示应付总金额。我正在尝试自学的编码新手。谢谢
答案 0 :(得分:1)
在循环之前,
double total = 0;
然后在循环中(在计算金额后)
total += amt;
最后在循环之后,类似
System.out.printf("The total is %.2f%n", total);
答案 1 :(得分:0)
分配一个全局变量并将值存储在该变量中,最后打印该全局变量的值。 多数民众赞成