首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >COMP SCI 101 -使用Printf对中字段

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

Stack Overflow用户
提问于 2013-09-29 17:55:04
回答 1查看 155关注 0票数 1

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

在我的笔记中,我看到我应该能够在printf的%之后使用^标志对输出进行居中。但是,当我运行程序时,Netbeans总是给我一条错误消息。

我想知道是否有人能帮我集中我的产出。我的代码如下。

我已看过已获解答的问题。然而,它们与我到目前为止在课堂上被教授的内容不一致--我不能使用它。

任何帮助都将不胜感激。

代码语言:javascript
复制
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

"^“将放在决赛中:

代码语言:javascript
复制
System.out.printf ("%^10.0f%^10.1f%n", Cels, 1.8*Cels+32.0" 

-然而,当我输入该代码时,我会在Netbeans中得到一个错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-29 18:32:49

如果您查看Formatter javadoc,Java没有一个标志来对printf的输出进行中心,并且没有^标志。

您的注释可能是指熔岩库,它附带了C函数的“高级”Java端口。正如Lava 规格说明中关于标志的部分中所指定的,^是以输出为中心的标志。

正如上面其他地方所指定的,您可以使用来自图书馆图书馆方法,也可以设计一种你自己的方法

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19081329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档