Java COMP SCI 101-使用Printf对中字段

Java COMP SCI 101-使用Printf对中字段,java,netbeans,printf,Java,Netbeans,Printf,我正在使用printf函数创建一个摄氏到华氏的转换表 在我的笔记中,我看到我应该能够在printf的%之后立即使用^flag将输出居中。但是,当我运行程序时,Netbeans总是给我一条错误消息 我想知道是否有人能帮我集中精力。我的代码如下 我已经仔细阅读了已经回答过的问题。然而,它们与我迄今为止在课堂上所学的并不一致,因此,我不能使用它 任何帮助都将不胜感激 public class CelsToFahrTable { /** * Main argument prints a Celsiu

我正在使用printf函数创建一个摄氏到华氏的转换表

在我的笔记中,我看到我应该能够在printf的%之后立即使用^flag将输出居中。但是,当我运行程序时,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
--但是,当我键入该代码时,Netbeans中出现了一个错误。

如果您查看javadoc,Java没有一个标志来将printf的输出居中,也没有^flag

您的注释可能指的是,它带有C printf函数的高级Java端口。根据Lava中关于标志部分的规定,^是居中输出的标志


如其他地方所述,您可以使用或Desive中的方法。

您试图将^放在何处?我相信^是位运算符。不是电源。^将放在最后:System.out.printf%^10.0f%^10.1f%n,Cels,1.8*Cels+32.0--但是当我键入该代码时,我在Netbeans中遇到一个错误。不要盲目地按照您的注释操作。请阅读中的printf格式规则。非常感谢您提供的信息。通过这篇文章,我了解到“^”不是有效的标志。因为,我的教授没有教我们如何使用StringUtils.center方法,所以我不会使用它。但我在代码中使用的最后一个printf中修改了列宽,使其看起来居中。而且看起来很不错。
System.out.printf ("%^10.0f%^10.1f%n", Cels, 1.8*Cels+32.0"