好久不写普通Java,一上手发现简单的部分都忘记怎么写了……
趁着这次练习,赶紧记一下
package org.example;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入贴现率(非百分比):");
double r0 = scanner.nextDouble();
double r = 1 + r0;
System.out.print("请输入计算年数:");
int t = scanner.nextInt();
while (t > 1){
r = r * (1 + r0);
t--;
}
//贴现因子 a
double a = 1 / r ;
System.out.print("贴现因子为:");
System.out.printf("%.4f", a);
}
}
输出结果:
顺便一提:
Java中的pringtf函数输出保留位数的小数时会自动四舍五入
标签:Java,scanner,输入输出,基础,System,print,r0,out From: https://www.cnblogs.com/yansans/p/18065987