Java 程序:计算单利
原文:https://www.studytonight.com/java-programs/java-program-to-find-simple-interest
在本教程中,我们将学习如何在给定本金、利率和时间段的情况下找到单利。简单利息是计算贷款利息费用最简单的方法。但是在继续之前,如果你不熟悉 java 中算术运算符的概念,那么一定要查看关于 Java 中运算符的文章。
输入:输入本金金额:6200
输入汇率:11
输入时间段:2
输出:
单利:1364.0
程序 1:找到对 Java 的单利
在这个程序中,我们将看到当值是用户定义的时,如何使用公式找到简单的兴趣。这意味着,首先我们将要求用户初始化变量,然后我们将使用公式找到简单的兴趣。
算法:
开始
创建 Scanner 类的实例,从用户处获取输入。
为本金、利率和时间段声明变量。
要求用户初始化这些变量。
用公式计算单利。
打印简单利息的值。
停止
下面是寻找单利的 Java 代码。
//Java Program to find the simple interest
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
//Take input from the user
//Create an instance of Scanner class
Scanner sc = new Scanner(System.in);
//Declare variables
float p, r, t, si;
System.out.println("Enter the Principal : ");
p = sc.nextFloat(); //Initialize the variables
System.out.println("Enter the Rate of interest : ");
r = sc.nextFloat(); //Initialize the variables
System.out.println("Enter the Time period : ");
t = sc.nextFloat(); //Initialize the variables
sc.close();
//Calculate the simple interest
si = (p * r * t) / 100;
//Print the simple interest
System.out.println("Simple Interest is: " +si);
}
}
输入本金:2000 输入利率:5 输入时间段:2 单利为:200.0
程序 2:找到对 Java 的单利
在这个程序中,我们将找到本金、利率和预定义值的时间段。
算法:
开始
声明本金、利率和时间段的变量。
初始化变量。
用公式计算单利。
打印简单利息。
停止
下面是寻找单利的 Java 代码。
//Java Program to find the simple interest
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
//Declare and Initialize the Principle, Rate and Time Period
float P = 1500, R = 10, T = 2;
System.out.println("The entered principle amount is = " + P);
System.out.println("The entered rate is = " + R);
System.out.println("The entered time period is " + T);
// Calculate simple interest
float SI = (P * T * R) / 100;
//Print the simple interest
System.out.println("Simple interest = " + SI);
}
}
录入本金金额= 1500.0 录入利率= 10.0 录入时间段为 2.0 单利= 300.0
程序 3:找到对 Java 的单利
在这个程序中,我们将通过使用用户定义的函数来找到本金、利率和时间段。这里,我们将使用用户定义的函数来计算简单利息。
算法:
开始
创建 Scanner 类的实例,从用户处获取输入。
为本金、利率和时间段声明变量。
要求用户初始化这些变量。
调用一个方法来计算单利。
用公式计算单利。
打印简单利息的值。
停止
下面是寻找单利的 Java 代码。
//Java Program to find the simple interest
import java.util.Scanner;
public class Main
{
//User-defined program to find the simple interest
public static float simpleInterest(float principal, float rate, float time)
{
float interest = (principal*rate*time)/100;
return interest;
}
public static void main(String args[])
{
//Take input from the user
//Create an instance of Scanner class
Scanner sc = new Scanner(System.in);
//Declare variables
float p, r, t;
System.out.println("Enter the Principal : ");
p = sc.nextFloat(); //Initialize the variables
System.out.println("Enter the Rate of interest : ");
r = sc.nextFloat(); //Initialize the variables
System.out.println("Enter the Time period : ");
t = sc.nextFloat(); //Initialize the variables
sc.close();
//Call a method to calculate the simple interest
float interest = simpleInterest(p,r,t);
System.out.println("Simple interest is : " + interest);
}
}
输入本金:4500 输入利率:12 输入时间段:3 单利为:1620.0