【问题标题】:Java printing a string containing multiple integersJava 打印包含多个整数的字符串
【发布时间】:2013-08-05 04:56:27
【问题描述】:

今天刚开始学习java,似乎无法弄清楚这一点。我正在学习 learnjavaonline.org 上的教程,它教你一些东西,然后要求你编写代码来做特定的事情,然后检查输出以查看其是否正确。问题是,如果它不正确,它不会说明原因,或者给你一个正确代码的例子。

它希望我使用所有原语输出一个字符串“H3110 w0r1d 2.0 true”

我想出了这个

public class Main {
public static void main(String[] args) {
    char h = 'H';
    byte three = 3;
    short one = 1;
    boolean t = true;
    double ten = 10;
    float two = (float) 2.0;
    long won = 1;
    int zero = 0;

    String output = h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;
    System.out.println(output);
}

}

但它输出86.0 w0r1d 2.0 true

我怎样才能使它不添加所有整数,而是连续显示它们?

【问题讨论】:

  • post中使用String.format

标签: java string integer output


【解决方案1】:

您得到的结果是,本质上,您是在依赖隐式转换时在打印数值变量之前对它们进行算术运算。

连字符也是数字! H 在 ascii 表中的值为 72,因此您基本上是在指示 Java 程序打印以下结果: 72 + 3 + 1 + 10.0(等于 86.0)

这样的数字和符号混合输入的字符串连接可能会出现问题,因为隐式转换正在发挥作用。 为了确保内容符合您的要求,而不使用显式强制转换,可以在每个数值之间使用任一字符串,如下所示:

    char h = 'H';  // This is a numeral! Capital H has value 72 in Ascii table
    byte three = 3;
    short one = 1;
    boolean t = true; // not a numeral
    double ten = 10;
    float two = (float) 2.0;
    long lOne = 1;
    int zero = 0;

    System.out.println(h + "" + three + "" + one + "" + (int) ten + " w"  
        + zero + "r" + lOne + "d " + two + " " + t );

请注意我需要如何将 ten 转换为 int 类型,以丢失小数点...

上面的例子不是使用字符串连接的好例子! 对于一个适当的解决方案,这可能更针对有更多经验的人,是尝试使用字符串格式,如下所示:

System.out.println(String.format("%s%s%s%s w%sr%sd %s %s", h, three, one,
    (int) ten, zero, lOne, two, t));

另一种方法是使用这样的消息格式,这可能不是此分配的最佳选择,因为浮点数将打印为整数。还需要导入java.text.MessageFormat

// please note: the double and the float won't print decimals!
// note: import java.text.MessageFormat for this
System.out.println(MessageFormat.format("{0}{1}{2}{3} w{4}r{5}d {6} {7}", h,
    three, one, (int) ten, zero, lOne, two, t));

More examples from the Ascii table.

【讨论】:

    【解决方案2】:
    public class Main {
        public static void main(String[] args) {
    
           int b = 3110;
           int d = 0;
           String e = "orld";
           double f = 2;
           boolean g = true;
           System.out.println("H" + b + " " + "w" + d + e + " " + f + " " + g);
        }
    }
    

    【讨论】:

    • 欢迎来到 Stack Overflow!您可以将文本缩进四个空格来创建一个代码块。另外,您能否为您的代码添加一些解释?
    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
    【解决方案3】:

    诀窍是让编译器将+ 解释为字符串连接(然后默默地将数字转换为字符串),而不是添加两个数字。这意味着 + 的两个参数之一必须是字符串,而不是 - 作为前三个参数 - 数字(是的,char 是数字)。

    希望数字直接相邻但在它们之间有空格的情况并不常见,例如:

    String output = h + " " + three + " " + one + " " + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;
    

    如果你真的不想有空格,那么就让第一个参数为空字符串:

    String output = "" + h ....
    

    您也可以将 h 从 char 更改为 String。

    【讨论】:

      【解决方案4】:

      您可以使用包装类的 toString 或 valueOf 方法将您的数字转换为字符串(猜想您还没有),或者只是将所有原语填充到打印行中而不使用 String output

      system.out.println(h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t);
      

      所有你需要寻找的是在 printline 语句中有一个字符串。这意味着如果您只想打印我们基于数字的数据类型,您可以使用system.out.println("" + youNumberVariable)

      您还可以选择在您的输出声明output = "" + theRest; 的开头添加一个空字符串,以强制所有后续值进入字符串,就像在 printline 语句中所做的那样。

      其中大部分不是非常漂亮的编码,但对于学习过程来说已经足够了。

      【讨论】:

      • 谢谢,这非常有帮助。如果我有足够的声誉(第一次使用stackoverflow),我会投票赞成:P
      【解决方案5】:

      一个简单而丑陋的方法是对每个数值使用String.valueOf

      如:

      String output = h + String.valueOf(three); // + etc...
      

      编辑

      morgano 的方法也完全有效 - 为此 +1。

      在更一般的主题上,您可能希望将String.concat 用于String 连接,或者更好的是StringBuilder 对象。

      This SO 页面包含很多您可以在此问题上使用的信息。

      【讨论】:

        【解决方案6】:

        这一行的问题:

        String output = h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;
        

        操作是从左到右执行的,所以它首先求和h + three(其计算结果为int),然后是one,然后是ten。到目前为止,您有一个数值(int),然后将“汇总”到String。试试这样的:

        String output = "" + h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;
        

        在第二种情况下,您的表达式将从 String 对象开始,将其余操作评估为 Strings。

        您当然可以在开头使用"" 或任何其他等于String 的值,例如String.valueOf(h)。在最后一种情况下,您不需要将String.valueOf() 用于其他操作数,因为第一个操作数已经是字符串。

        【讨论】:

        • 这是我的假设,但不知道如何解决它。是否有另一种方法可以在字符串开头不使用空格的情况下做到这一点?
        • 您可以使用String.valueOf(h)(仅用于第一个值,而不是所有值),因此您可以在不使用“”的情况下以字符串开头。我会用这个更新我的答案。
        【解决方案7】:

        在添加之前,我会使用 String.valueOf 将每个数值显式转换为 String。像这样:

        String output = h + String.valueOf( three ) + String.valueOf( one ) + String.valueOf( ten ) + " " + "w" + String.valueOf( zero ) + "r" + String.valueOf( won ) + "d " + String.valueOf( two ) + " " + t;
        

        【讨论】:

          猜你喜欢
          • 2012-11-06
          • 1970-01-01
          • 2016-01-03
          • 1970-01-01
          • 2015-02-08
          • 1970-01-01
          • 1970-01-01
          • 2015-11-22
          • 1970-01-01
          相关资源
          最近更新 更多