Java 程序计算电费

编写一个 Java 程序 使用 Else If 语句和示例计算电费。此 Java 程序 要求用户输入消耗的单位。接下来,它会计算总电费。如果电力公司对不同单位收取不同的费率,则此方法很有用。在此示例中,我们使用 Else If 语句。

提示:Java Else If 语句 检查第一个条件,如果为 TRUE,则执行该块内的语句。如果条件为 FALSE,则检查下一个(Else If 条件),依此类推。

import java.util.Scanner;

public class ElectricityBill1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Units;
		double Amount, Sur_Charge, Total_Amount;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Units that you Consumed  : ");
		Units = sc.nextInt();
  
	  	if (Units < 50)
	  	{
	        Amount = Units * 2.60;
	  		Sur_Charge = 25;  	
	  	} 
	  	else if (Units <= 100)
	  	{
	  		// For the First Fifty Units Charge = 130 (50 * 2.60)
	  		// Next, we are removing those 50 units from total units
	  		Amount = 130 + ((Units - 50 ) * 3.25);
	  		Sur_Charge = 35; 	
	  	}
	  	else if (Units <= 200)
	  	{
	  		// First Fifty Units charge = 130, and 50 - 100 is 162.50 (50 * 3.25)
	  		// Next, we are removing those 100 units from total units
	  		Amount = 130 + 162.50 + ((Units - 100 ) * 5.26);
	  		Sur_Charge = 45; 	
	  	}
	  	else
	  	{
	  		/* First Fifty Units = 130, 50 - 100 is 162.50, 
	  		 and 100 - 200 is 526 (100 * 5.65)
	  		Next, we are removing those 200 units from total units */
		   	Amount = 130 + 162.50 + 526 + ((Units - 200 ) * 7.75); 
		   	Sur_Charge = 55; 
		}
		
		Total_Amount = Amount + Sur_Charge;
		System.out.println("\n Electricity Bill  =  " + Total_Amount);
	}

}
Program to Calculate Electricity Bill 1

Java 程序使用方法计算电费

此 电费计算程序 与第一个示例相同。但我们将逻辑分离并将其放在一个单独的方法中。

import java.util.Scanner;

public class Example2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Units;	
		double Total_Amount;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Units that you Consumed  : ");
		Units = sc.nextInt();
  
		Total_Amount = ElecBill(Units);
		System.out.println("\n Electricity Bill  =  " + Total_Amount);
	}
	public static double ElecBill(int Units)
	{
		double Amount, Sur_Charge, Total_Amount;
		if (Units < 50)
	  	{
	        Amount = Units * 2.60;
	  		Sur_Charge = 25;  	
	  	} 
	  	else if (Units <= 100)
	  	{
	  		Amount = 130 + ((Units - 50 ) * 3.25);
	  		Sur_Charge = 35; 	
	  	}
	  	else if (Units <= 200)
	  	{
	  		Amount = 130 + 162.50 + ((Units - 100 ) * 5.26);
	  		Sur_Charge = 45; 	
	  	}
	  	else
	  	{
		   	Amount = 130 + 162.50 + 526 + ((Units - 200 ) * 7.75); 
		   	Sur_Charge = 55; 
		}
		
		Total_Amount = Amount + Sur_Charge;
		return Total_Amount;
	}
}
 Please Enter the Units that you Consumed  : 95

 Electricity Bill  =  311.25

我试试另一个值。

 Please Enter the Units that you Consumed  : 450

 Electricity Bill  =  2811.0

这种类型的Java程序很有用,如果电力公司有统一的费率。例如:如果您消耗的单位在 300 到 500 之间,那么每单位的费用固定为 7.75 卢比,等等。

import java.util.Scanner;

public class Example3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Units;
		double Amount, Sur_Charge, Total_Amount;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Units that you Consumed  : ");
		Units = sc.nextInt();
		
		if (Units > 500)
	  	{
	  		Amount = Units * 9.65;
	  		Sur_Charge = 85;  	
	  	} // Otherwise (fails), Compiler will move to Next Else If block 
	  	else if (Units >= 300)
	  	{
	  		Amount = Units * 7.75;
	  		Sur_Charge = 75; 	
	  	} 
	  	else if (Units >= 200)
	  	{
	  		Amount = Units * 5.26;
	  		Sur_Charge = 55; 	
	  	}  
	  	else if (Units >= 100)
	  	{
	  		Amount = Units * 3.76;
	  		Sur_Charge = 35; 	
	  	} // Otherwise (fails), Compiler will move to Else block   	
	  	else
	  	{
		   	Amount = Units * 2.25; 
		   	Sur_Charge = 25; 
		}
		Total_Amount = Amount + Sur_Charge;
		System.out.println("\n Electricity Bill  =  " + Total_Amount);
	}
}
 Please Enter the Units that you Consumed  : 750

 Electricity Bill  =  7322.5

我试试另一个值。

 Please Enter the Units that you Consumed  : 250

 Electricity Bill  =  1370.0

评论已关闭。