Java 程序计算简单利息


2022 年 3 月 19 日, Learn eTutorial
2151

要在 Java 中计算简单利息。我们需要用户的本金、每年利息和时间周期。

计算简单利息的公式是什么?

我们可以使用以下公式计算简单利息:

简单利息 (SI) = (P * R * T )/100 

其中,

  •  P 是本金。
  •  R 是年利率。
  •  T 是以年为单位的时间周期。

如何在 Java 中读取浮点数?

要从用户读取浮点数,我们可以使用 Java Scanner 类中的 nextFloat() 函数。要读取浮点值,我们必须编写如下代码:

System.out.print("Enter the Principal amount : ");
p = sc.nextFloat();
 

如何实现计算简单利息的 Java 程序?

首先,我们必须声明类 SimpleInterest。然后打开 main 函数。然后将变量 p、r、t、si 声明为 float。从用户读取本金到变量“p”中。将年利率读取到变量“r”中。从用户读取时间周期到变量“t”中。然后计算简单利息为“si = p * n * r / 100”。然后使用 system.out.println() 函数显示 si

算法

步骤 1:使用 public 修饰符声明类 SimpleInterest。

步骤2:打开main()开始程序,Java程序执行从main()开始

步骤 3:将变量 p、r、t、si 声明为 float。

步骤 4:从用户读取本金到变量 p 中。

步骤 5:将利率读取到变量 r 中。

步骤 6:将时间周期读取到变量 t 中。

步骤 7:计算 si=p*i*t/100

步骤 8:显示简单利息为 si


为了计算 SI,我们使用以下 Java 概念,我们建议您学习这些概念以更好地理解。

Java 源代码

                                          import java.util.Scanner;
public class SimpleInterest
{
    public static void main(String args[]) 
    {
        float p, r, t,si;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the Principal amount : ");
        p = sc.nextFloat();//Read Principla amount
        System.out.print("Enter the Rate of interest per year : ");
        r = sc.nextFloat();//Read rate of interset
        System.out.print("Enter the Time period in year : ");
        t = sc.nextFloat();//Read the time period
        sc.close();
        si = (p * r * t) / 100;//calculate the simple interest
        System.out.print("Simple Interest is: " +si);
    }
}
                                      

输出

OUTPUT 1

Enter the Principal amount :3000
Enter the Rate of interest per year : 7
Enter the Time period in year : 4
Simple Interest is:840.0

OUTPUT 2

Enter the Principal amount :6000
Enter the Rate of interest per year : 10
Enter the Time period in year : 5
Simple Interest is:3000.0