Java 计算单利程序

编写一个Java程序来计算单利,并附带一个示例。此单利计算背后的公式为=(本金*利率*年数)/100。

此Java程序允许用户输入本金、总年数和利率。使用这些值,该程序将使用上述公式计算单利。

import java.util.Scanner;

public class SimpleInterest1 {

	private static Scanner sc;
	public static void main(String[] args) 
	{
		double PAmount, ROI, TimePeriod, simpleInterset;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Principal Amount : ");
		PAmount = sc.nextDouble();
		
		System.out.print(" Please Enter the Rate Of Interest : ");
		ROI = sc.nextDouble();
		
		System.out.print(" Please Enter the Time Period in Years : ");
		TimePeriod = sc.nextDouble();
		
		simpleInterset = (PAmount * ROI * TimePeriod) / 100;
		
		System.out.println("\n The Simple Interest for Principal Amount " + PAmount + " is = " + simpleInterset);
	}
}
Calculate Simple Interest 1

使用函数计算单利的Java程序

此程序与上面的程序相同,但这次我们创建了一个不同的方法来计算单利。

import java.util.Scanner;

public class ScI2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		double PAmount, ROI, TimePeriod;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Principal Amount : ");
		PAmount = sc.nextDouble();
		
		System.out.print(" Please Enter the Rate Of Interest : ");
		ROI = sc.nextDouble();
		
		System.out.print(" Please Enter the Time Period in Years : ");
		TimePeriod = sc.nextDouble();
		
		calSimpleIntr(PAmount, ROI, TimePeriod);
		
	}
	public static void calSimpleIntr(double PAmount, double ROI, double TimePeriod)
	{
		double SI;
		
		SI = (PAmount * ROI * TimePeriod) / 100;
		
		System.out.println("\n The Total Amount " + PAmount + " is = " + SI);
	}
}
Program to Calculate Simple Interest using functions

程序不从方法中打印单利,而是返回一个值。接下来,我们将返回的值分配给Java主程序中的另一个变量。

import java.util.Scanner;

public class SimIn3 {

	private static Scanner sc;
	public static void main(String[] args) 
	{
		double PAmount, ROI, TimePeriod, SI;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Principal Amount : ");
		PAmount = sc.nextDouble();
		
		System.out.print(" Please Enter the Rate Of Interest : ");
		ROI = sc.nextDouble();
		
		System.out.print(" Please Enter the Time Period in Years : ");
		TimePeriod = sc.nextDouble();
		
		SI =  calSimInt(PAmount, ROI, TimePeriod);
		System.out.println("\n The Simple Interest for Principal Amount " + PAmount + " is = " + SI);
		
	}
	public static double calSimInt(double PAmount, double ROI, double TimePeriod)
	{
		double SI;
		
		SI = (PAmount * ROI * TimePeriod) / 100;
		
		return SI;
	}
}
Calculate Simple Interest 3

评论已关闭。