System.out.format问题。 %d%正

时间:2015-01-06 03:38:22

标签: java

它一直给我一个错误。 我对%d%n部分感到困惑。任何人都可以向我解释? 我想要出现金额。 谢谢

System.out.println("Thank you for choosing withdrawl.");
System.out.println("How much would u like to withdrawl?");
System.out.println("1=100");
System.out.println("2=200");
System.out.println("3=300");
System.out.println("4=400");
System.out.println("5=500");
withdrawlmoney = user.nextInt();
switch(withdrawlmoney){
    case 1:  withdrawlmoneynumber = "100";
        break;
    case 2:  withdrawlmoneynumber = "200";
        break;
    case 3:  withdrawlmoneynumber = "300";
        break;
    case 4:  withdrawlmoneynumber = "400";
        break;
    case 5:  withdrawlmoneynumber = "500";
        break;
    default: withdrawlmoneynumber = "Invalid amount";
        break;
}
System.out.println(withdrawlmoneynumber);
System.out.format("the value you want to withdrawl is: %d%n" , withdrawlmoneynumber);

3 个答案:

答案 0 :(得分:6)

格式代码"%d"表示打印十进制数字。但是你传递的是Stringwithdrawalmoneynumber的数据类型应为数字。

格式代码"%n"表示打印行分隔符,它取决于平台。它可能是"\n""\r\n""\r",具体取决于代码的运行位置。

答案 1 :(得分:0)

更容易实现:

System.out.println("The value you want to withdrawl is: " + withdrawlmoneynumber);

这适用于任何数据类型。

答案 2 :(得分:0)

尝试...

 System.out.format("the value you want to withdrawl is: %s\n" , withdrawlmoneynumber);

withdrawlmoneynumber是字符串..

  

格式应为%s

表示根据Format

的字符串

我猜你的意思是%n是\ n表示换行