我正在设计一个用于打印单个电话电话引脚的程序。该计划应包括 1.客户名称 2.客户的地址 3.电话号码 4.以前的抄表 5.电流表读数。
该计划还将包括消耗的单位数量,并使用以下内容打印账单费用....
如果我消耗的单位为负数,则不会显示任何费用......我该怎么做?
System.out.println("输入当前读数"); CREAD = input.nextInt(); //获取整数输入并将其存储在当前读数
中input.close();
int unit= (pread - cread);
System.out.println("Your Consumed unit is " + unit);
if (unit >= 0 && unit <= 100) {
System.out.println("Your cost is 2.50");
}
else if (unit >= 100 && unit <= 200) {
System.out.println("Your cost is 2.25");
}
else if (unit >= 200 && unit <=300) {
System.out.println("Your cost is 1.90");
}
if (unit >= 300) {
System.out.println("Your cost is 1.65");
}
答案 0 :(得分:1)
只需添加另一个输入提示,并将其存储在新变量中。这是改进的代码,代码中的注释中解释了所有更改:
import java.util.Scanner;
public class BillingSystem {
public static void main(String[] args) {
// declare variables
String name,addr;
int pread, cread,tel; //New variables here
// input
Scanner input = new Scanner (System.in);
System.out.println("Enter The Customer Name");
name= input.nextLine();
//Scanner address = new Scanner (System.in); no need for this
System.out.println("Address of The Customer");
addr= input.nextLine();
//Scanner telephone = new Scanner (System.in); no need for this
System.out.println("Enter telephone number");
tel=input.nextInt(); //get integer input and store it in tel
System.out.println("Enter Previous reading");
pread=input.nextInt(); //get integer input and store it in pread
System.out.println("Enter Current reading");
cread=input.nextInt(); //get integer input and store it in cread
}
}