package e.power.bhd;
import java.util.Scanner;
公共类EPowerBhd {
public static void main(String[] args) {
int accountNum = 1;
double cMeter;
double pMeter;
double eUsage;
double totalDueAmount = 0.0;
double Tot = 0.0;
Scanner input = new Scanner(System.in);
while (accountNum != 0) {
// user enter account no
System.out.print("Enter Account number (0 to stop ) : ");
accountNum = input.nextInt();
// user enter current meter reading
System.out.print("Current meter reading : ");
cMeter = input.nextDouble();
// user enter previous meter reading
System.out.print("Previous meter reading : ");
pMeter = input.nextDouble();
eUsage = cMeter - pMeter;
System.out.print("Electricity Usage(in kWh) : " + eUsage);
System.out.println();
if (eUsage >= 1 && eUsage <= 200) {
totalDueAmount = 0.218 * eUsage;
} else if (eUsage > 200 && eUsage <= 300) {
totalDueAmount = 0.334 * eUsage;
} else if (eUsage > 300 && eUsage <= 600) {
totalDueAmount = 0.516 * eUsage;
} else if (eUsage > 600 && eUsage <= 900) {
totalDueAmount = 0.546 * eUsage;
} else {
totalDueAmount = 0.571 * eUsage;
}
System.out.println(); // total amount for the account is displayed
System.out.println("Amount number : " + accountNum + "," + " your charge is " + "RM" + totalDueAmount);
Tot = totalDueAmount++;
System.out.println();
} // loop exit and displa the total amount for all accounts
System.out.println("Total Amount collected = " + "RM" + Tot);
System.out.println("________________________________");
System.out.println("--- DONE ----");
System.out.println("________________________________");
}
}
我已经完成了一个java程序,用户输入帐号,当前抄表和以前的当前读数,然后计算电力usag 它将计算帐户的用电量,直到用户输入0结束循环,并计算并显示所有帐户的总金额。
在此程序中,如果用户首先输入0,则应显示 &#34;未处理任何帐户。&#34;只有并终止。
附上程序代码和程序的详细信息。
https://www.dropbox.com/s/85mm1w09tl7c1n3/1.png?dl=0 https://www.dropbox.com/s/8w0ngjt9em70q7w/2.png?dl=0
任何帮助主要是赞赏..
答案 0 :(得分:0)
有两种方法可以做到。
使用continue
关键字。请看下面。
while (accountNum != 0) {
// user enter account no
System.out.print("Enter Account number (0 to stop ) : ");
accountNum = input.nextInt()
if(accountNum==0){
continue;
}
// user enter current meter reading
System.out.print("Current meter reading : ");
cMeter = input.nextDouble();
//rest of your code
使用//rest of your code
Sytem.exit(-1)
答案 1 :(得分:0)
你的程序中的错误的答案是它是一个永无止境的循环。它始终是真实的,因为您刚刚询问accountNum
的值并且没有添加条件语句。
使用以下代码替代您的代码,它可以正常运行。
import java.util.Scanner;
public class Yehey {
public static void main(String[] args) {
int accountNum = 1;
double cMeter;
double pMeter;
double eUsage;
double totalDueAmount = 0.0;
double Tot = 0.0;
Scanner input = new Scanner(System.in);
while (true) {
// user enter account no
System.out.print("Enter Account number (0 to stop ) : ");
accountNum = input.nextInt();
if (accountNum==1) {
// user enter current meter reading
System.out.print("Current meter reading : ");
cMeter = input.nextDouble();
// user enter previous meter reading
System.out.print("Previous meter reading : ");
pMeter = input.nextDouble();
eUsage = cMeter - pMeter;
System.out.print("Electricity Usage(in kWh) : " + eUsage);
System.out.println();
if (eUsage >= 1 && eUsage <= 200) {
totalDueAmount = 0.218 * eUsage;
} else if (eUsage > 200 && eUsage <= 300) {
totalDueAmount = 0.334 * eUsage;
} else if (eUsage > 300 && eUsage <= 600) {
totalDueAmount = 0.516 * eUsage;
} else if (eUsage > 600 && eUsage <= 900) {
totalDueAmount = 0.546 * eUsage;
} else {
totalDueAmount = 0.571 * eUsage;
}
System.out.println(); // total amount for the account is displayed
System.out.println("Amount number : " + accountNum + "," + " your charge is " + "RM" + totalDueAmount);
Tot = totalDueAmount++;
System.out.println();
// loop exit and displa the total amount for all accounts
System.out.println("Total Amount collected = " + "RM" + Tot);
System.out.println("________________________________");
System.out.println("--- DONE ----");
System.out.println("________________________________");
} else {
System.out.println("Thank you for using my program.");
break;
}
}
}
}
添加了更改:
1.将while的内部条件更改为true
2.如果accountNum保持为1,则添加if语句以执行代码,如果程序的值更改为其他值,则断开程序
3.如果accountNum的值不等于零,则添加... System.out.println("Thank you for using my program."); break;
(注意:请澄清您的问题或疑问,以避免标记您的问题或减少投票。)