Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在System.out.format中使用NumberFormat导入_Java_For Loop_Netbeans_System.out_Money Format - Fatal编程技术网

Java 在System.out.format中使用NumberFormat导入

Java 在System.out.format中使用NumberFormat导入,java,for-loop,netbeans,system.out,money-format,Java,For Loop,Netbeans,System.out,Money Format,我正在做一个学校作业的复利项目。我尝试使用System.out.format();并使用money.format设置变量investment、interest和investTotal的格式。我不知道为什么,但它总是给我一个错误 “格式说明符“%.2f”、参数2、3和4的值类型“String”无效”我已经尝试解决这个问题很长时间了,但似乎仍然找不到原因 --A /** *@param指定命令行参数 */ 公共静态void main(字符串[]args){ //飞溅 //不变的 //反对 扫描仪输入

我正在做一个学校作业的复利项目。我尝试使用System.out.format();并使用money.format设置变量investment、interest和investTotal的格式。我不知道为什么,但它总是给我一个错误 “格式说明符“%.2f”、参数2、3和4的值类型“String”无效”我已经尝试解决这个问题很长时间了,但似乎仍然找不到原因

--A

/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//飞溅
//不变的
//反对
扫描仪输入=新扫描仪(System.in);
NumberFormat money=NumberFormat.getCurrencyInstance();
//变数
双重投资;
双倍投资总额;
双倍费率;
双重利益;
国际年;
//输入
做
{
系统输出打印(“输入年度投资(最低100.00美元存款):”;
投资=输入.nextDouble();
} 
同时(投资<100);
做
{
系统输出打印(“输入输入输入利率(%):”;
rate=input.nextDouble()/100;
} 

while(rategetCurrencyInstance返回字符串,因此无法使用%.2f格式化

您最好看看NumberFormat是如何工作的:

如您所见,格式化的结果是一个字符串,当您将String.format与%.2f一起使用时,您应该输入一个数字,例如: 系统输出格式(“%2s |%.2f\n”,1.001,1.005)


我不确定您使用NumberFormat想要得到什么,如果您进行分类,我将能够进一步帮助您解决这个问题。

%.2f需要一个数字。而您传递的是一个字符串(因为money.format()返回一个字符串)。为什么要先用money.format()格式化数字,然后尝试使用%.2f?谢谢。我已经解决了。我刚刚输出了一个表,该表将显示您的投资额、利率、年份等,但感谢您的帮助:)
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // SPLASH 
    // CONSTANT 
    // OBJECT 
    Scanner input = new Scanner(System.in); 
    NumberFormat money = NumberFormat.getCurrencyInstance();

    // VARIABLES
    double investment; 
    double investTotal; 
    double rate; 
    double intrest; 
    int year; 

    // INPUT 
    do 
    {
        System.out.print("Enter yearly investment (min $100.00 deposit): ");
        investment = input.nextDouble(); 
    } 
    while (investment < 100); 

    do 
    {
        System.out.print("Enter intrest rate (%): ");
        rate = input.nextDouble()/100;
    } 
    while (rate <= 0); 

    do
    {
        System.out.print("Enter number of years: ");
        year = input.nextInt(); 
    }
    while (year <= 0 || year > 15);  

    // PROCESSING 
    investTotal = investment;
    for (int perYear = 1; perYear <= year; perYear++)
    {
        intrest = investTotal*rate;
        investTotal = (investment+intrest);
        System.out.format("%2s | %.2f | %.2f | %.2f\n", perYear, money.format(investment), money.format(intrest), money.format(investTotal));
        investTotal = investTotal + investment;
    }
    // OUTPUT 
}