简体   繁体   English

一个非常简单的Java程序出现问题,未显示正确的结果

[英]Having issue with a very simple java program, not displaying proper result

here is my code that isn't working: 这是我的代码不起作用:

Scanner hello = new Scanner (System.in);
double a = 10;
double c;

System.out.print("Enter the value: ");
c = hello.nextDouble();
double f = a + c;
System.out.printf("The sum of 10 plus user entry is : ", a+c);

No syntax error whatsoever, no error displayed, this is the result : Enter the value: 100 The sum of 10 plus user entry is : 没有语法错误,也没有错误显示,这是结果:输入值:100 10加用户输入的总和为:

So there is no result in the second line,,, for the command ( a+c ) as in program. 因此,与程序中的命令(a + c)在第二行没有结果。 But if i use a ' %.2f ' before ( a+c ) command, it works fine,, like : 但是,如果我在(a + c)命令之前使用'%.2f',它可以正常工作,例如:

System.out.printf("The sum of 10 plus user entry is : %.2f", a+c);

I tried to search about the '%.2f' but got to know it is used just to ascertain that the following number is to be displayed as a number with two decimal places. 我试图搜索有关'%.2f'的信息,但知道它仅用于确定以下数字将显示为带两位小数的数字。 (kinda round off thing, i guess).. (我想把事情弄圆)

I'm totally a rookie at Java. 我完全是Java的新手。 Started studying it at college right now. 现在就开始在大学学习它。 Was just curious to know about this concept and reason behind why this program worked only with the '%.2f' typed in it, and not without it, although it showed no error. 只是想知道这个概念和原因,为什么该程序只能在键入'%.2f'的情况下工作,而不能没有它,尽管它没有显示错误。 Will be great if someone can answer it. 如果有人可以回答,那就太好了。 thanks :-) 谢谢 :-)

Java's System.out.printf() method doesn't append information; Java的System.out.printf()方法不会附加信息; it substitutes it. 它替代了它。 The '%.2f' means: "Replace this with the next argument, and convert it to a floating-point number 2 places precise." '%.2f'的意思是:“将其替换为下一个参数,并将其转换为精确的浮点数2。” Removing the '%.2f' would mean that a+c would have nowhere to go, and printf() would discard it. 删除'%.2f'意味着a+c无处可去,而printf()则将其丢弃。

Since Java's System.out.printf() method is actually based on the printf() from C/C++, you might want to check out this guide. 由于Java的System.out.printf()方法实际上是基于C / C ++的printf() ,因此您可能需要查看指南。

You are using the wrong function. 您使用了错误的功能。 You should be using 您应该使用

System.out.println(myString)

Or 要么

System.out.print(myString)

You would format your code as 您将代码格式化为

System.out.println(myExplinationString + a+c)

System.out is an instance of java.io.PrintStream class that is provided as a static field of the System class. System.out是作为System类的静态字段提供的java.io.PrintStream类的实例。 printf(String format, Object... args) is one of the methods of the PrintStream class, check this Oracle tutorial on formatting numbers . printf(String format, Object... args)PrintStream类的方法之一,请查看有关格式化数字的Oracle教程 In brief, the first argument is a format string that may contain plain text and format specifiers, eg %.2f , that are applied to the next argument(s). 简而言之,第一个参数是一个格式字符串,其中可能包含应用于下一个参数的纯文本和格式说明符,例如%.2f All format specifiers are explained in the description of the java.util.Formatter class. java.util.Formatter类的描述中解释了所有格式说明符。 Note, that double value is autoboxed to Double . 注意, double值自动装箱Double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM