我正在使用printf函数创建一个摄氏到华氏转换表。
在我的笔记中,我看到我应该能够在printf的%之后使用^标志对输出进行居中。但是,当我运行程序时,Netbeans总是给我一条错误消息。
我想知道是否有人能帮我集中我的产出。我的代码如下。
我已看过已获解答的问题。然而,它们与我到目前为止在课堂上被教授的内容不一致--我不能使用它。
任何帮助都将不胜感激。
public class CelsToFahrTable {
/**
* Main argument prints a Celsius to Fahrenheit conversion table
* using integer Celsius degrees from 0 to 40
*/
public static void main(String[] args)
{
// declare variables
double Cels; //control variable (Celsius Temperature)
System.out.printf("%10s\t%10s%n", "Celsius", "Fahrenheit");
//prints headers Celsius and Fahrenheit at the top to signify what the data
//below means. Celsius should take up 10 columns then tab to Fahrenheit
//which again takes up 10 columns
for ( Cels = 0; Cels <= 40; Cels++)
//creates count-controlled loop using for statement
//Cels initialized to 0, continues expression until 40 and increments
//by 1 degree each time
System.out.printf("%10.0f%10.1f%n", Cels, 1.8*Cels+32.0);
//creates table showing Celsius temperature followed by its corresponding
//Fahrenheit temperature. Celsius temp is 10 columns with no decimals.
//Fahrenheit temp is 10 columns with a 1 decimal precision.
//Note: I tried to center each field using "^" but it gave me an error
//every time
}//end main"^“将放在决赛中:
System.out.printf ("%^10.0f%^10.1f%n", Cels, 1.8*Cels+32.0" -然而,当我输入该代码时,我会在Netbeans中得到一个错误。
发布于 2013-09-29 18:32:49
如果您查看Formatter javadoc,Java没有一个标志来对printf的输出进行中心,并且没有^标志。
您的注释可能是指熔岩库,它附带了C函数的“高级”Java端口。正如Lava 规格说明中关于标志的部分中所指定的,^是以输出为中心的标志。
正如上面其他地方所指定的,您可以使用来自图书馆的图书馆方法,也可以设计一种你自己的方法。
https://stackoverflow.com/questions/19081329
复制相似问题