Java实例:手机计费程序

来自CloudWiki
跳转至: 导航搜索

手机计费程序

某电信公司设计了一个手机话费套餐,套餐价格为58元/月,套餐国内免费通话时长为250min,超出部分按0.25元/min分收费;提供免费流量为50MB,超出部分按0.3元/MB收费,


输入:用户的实际通话时长 和实际数据流量;


输出:该用户本月的实际话费。

问题解决

初始代码

package task5;

import java.util.Scanner;

public class MethodTest {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入实际通话时长(分钟):");
		float call_time=sc.nextInt();
		System.out.println("请输入实际数据流量(M):");
		float data_flow=sc.nextInt();
		
		float total =58;
		if(call_time >250) {
			total += 0.25*(call_time-250);
		}
		if(data_flow >50) {
			total += 0.3*(data_flow-50);
		}
			
		System.out.println("本月实际话费为"+total);
		sc.close();
	 }
}	 
	


带返回值的方法

package task5;

import java.util.Scanner;

public class MethodTest {
	
	public static float getPay(float c,float d) {
		float total =58;
		if(c >250) {
			total += 0.25*(c-250);
		}
		if(d >50) {
			total += 0.3*(d-50);
		}
		//System.out.println("本月实际话费为"+total);
		return total;
	}
	

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入实际通话时长(分钟):");
		float call_time=sc.nextInt();
		System.out.println("请输入实际数据流量(M):");
		float data_flow=sc.nextInt();
		float t=getPay(call_time,data_flow);
		System.out.println("本月实际话费为"+t);
		sc.close();
	 }
}	 
	

不带返回值的方法

package task5;

import java.util.Scanner;

public class MethodTest {
	
	public static void getPay(float c,float d) {
		float total =58;
		if(c >250) {
			total += 0.25*(c-250);
		}
		if(d >50) {
			total += 0.3*(d-50);
		}
		System.out.println("本月实际话费为"+total);
		//return total;
	}
	

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入实际通话时长(分钟):");
		float call_time=sc.nextInt();
		System.out.println("请输入实际数据流量(M):");
		float data_flow=sc.nextInt();
		getPay(call_time,data_flow);
		
		sc.close();
	 }
}