java - 格式化,大十进制,请按百分比计算
问题描述
有人可以解释一下用法,而不仅仅是回答我真的很想学习如何做到这一点。这是我使用 Java 的第二个月,请解释 Java 中的格式化用法。太感谢了。对此,我真的非常感激。随意询问有关 C++ 或 python 的任何其他问题。我在格式化、大小数使用以及如何设置方面需要帮助
import java.util.*;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.math.*;
public class Main{
public static void main(String[] args)
{
Scanner myCalculation = new Scanner(System.in);
System.out.print("Welcome to the Interest Calculator \n\n");
String option = "y";
while (option.equalsIgnoreCase("y"))
{
System.out.print("Enter Loan Amount: ");
//double loanAmount = 520000;
double loanAmount = myCalculation.nextDouble();
//Get Interest rate from user
System.out.print("Enter Interest Rate: ");
// double interRate = .05375;
double interRate = myCalculation.nextDouble();
double interest = loanAmount * interRate;
System.out.printf("%.3f", interest);
System.out.println();
//prompt user to continue? if he enter y then it will Continue
//else it will stop
//System.out.println("Continue? (y:n): ");
Scanner scan = new Scanner(System.in);
boolean stop = false;
while(!stop) {
//do whatever
System.out.println("Would you like to continue? (yes or no)");
String s = scan.nextLine();
if(s.equals("no")) {
stop = true;
}
}
}
}
}
解决方案
Scanner in = new Scanner(System.in);
System.out.print("enter double");
double d = in.nextDouble(); //doubles
System.out.print("enter int");
int i = in.nextInt(); //ints
System.out.print("enter text");
String text = in.next(); //gets a string up to the first space
in.nextLine();
System.out.print("enter text w/ spaces");
String line = in.nextLine(); //gets a string up to the first space
System.out.println(d);
System.out.println(i);
System.out.println(text);
System.out.println(line);
in.close();
System.out.println(new DecimalFormat("#.###").format(4.567346344634));
推荐阅读
- angular - 使用 httpclient 进行角度单元测试
- python - 如果使用了一个值,我如何输出一个值?
- google-places-api - 如何在 Blazor Web Assembly 中设置 Google Place 自动完成地址表单?
- c++ - 在 c++ 中使用 opencv 时如何加载 Pytorch 模型(.pt)
- php - 验证同一页面上的多个 html 表单
- r - 从列表中提取元素以创建数据框
- java - 执行处理器“org.thymeleaf.spring5.processor.SpringOptionFieldTagProcessor”时出错
- haskell - 使受约束的函数成为类型类实例
- docker - Docker:VueJS 无法解析服务名称后端
- c# - 以纳秒为单位的 C# DateTime 与 UTC 和本地