Java 格式化包含多个字符串的长system.out.print(&;整数

Java 格式化包含多个字符串的长system.out.print(&;整数,java,Java,假设你有以下几点: String cake = "whatever"; int cakeNumber = 509; String whyNot = "another string"; double number = 1; system.out.println("I like "+ cake +" and I eat " + cakeNumber + " blah blah prolonging this string because " + whyNot + " and so on "

假设你有以下几点:

String cake = "whatever";
int cakeNumber = 509;
String whyNot = "another string";
double number = 1;

system.out.println("I like "+ cake +" and I eat " + cakeNumber + " blah blah     prolonging this string because " + whyNot + " and so on " + number + ".");

所以,这没有多大意义,也没有必要,因为这只是一个例子。我的问题是,考虑到多个字符串和变量之间有一个加号,这看起来像是一团糟。有没有一种方法可以格式化这个println,或者通过减少加号来稍微整理一下它呢?

一种方法,如果你想减少行数和加号,你可以将它分成若干个
系统输出。print

System.out.print("I like "+ cake); 
System.out.print(" and I eat " + cakeNumber);
System.out.print(" blah blah     prolonging this string because ");
System.out.println(whyNot + " and so on " + number + ".");

请尝试
printf

例如,你可以写

System.out.printf("I like %s and I eat %d blah blah     prolonging this string because %s and so on %0.f.\n", cake, cakenumber, whyNot, number);
还要注意,许多IDE(如Eclipse)将允许您轻松地跨多行跨越字符串。但是,
printf
的优点是可以将字符串与变量分开,还可以更好地控制数字的显示。例如,请注意
%.0f
。通常,您可以指定值应占用多少空间(包括填充)以及使用多少小数位。

然后您可以尝试:

int age = 24;
double salary = 4500.25;
String name = "Marcelo";
String message = "Hi, my name is %s and I'm %d years old and I earn %f.";

System.out.println(String.format(message,name,age,salary));
请记住,
String.format
使用默认区域设置:)

快乐编码。

使用或


我有点喜欢静态导入,比如:

import static java.text.MessageFormat.format;

System.out.println(format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));

(也称为) 在这里,您可以选择很多语法(仅列出一些,可能还有更多):

%n
表示系统的特定换行符。这是必需的,因为
printf
不会在操作后自动插入换行符


多行字符串连接
一些示例输出供您考虑

$ javac Foo.java
$ java Foo

--- Original ---
I like whatever and I eat 509 blah blah     prolonging this string because another string and so on 1.0.

--- whitespace is your friend---
I like whatever and I eat 509 blah blah     prolonging this string because another string and so on 1.0.

--- Also, look into printf ---
I like "whatever" and I eat 00509 blah blah     prolonging this string because "another string" and so on  1.00
当您开始想要对参数进行任何类型的非默认格式设置时(零填充、空格填充、floting point值上仅显示2位数字等等),printf将非常有用

Foo.java
怎么样
System.out.format
?如果你想让它看起来好看,只要每隔一段时间换几行就可以了。我这样做,我从不生气(尽管在代码方面我有极端的强迫症…。这可以简化为
printf()
。是的,
printf
更适合于基本类型格式。所以你必须关心的是它是
%s
还是
%d
这正是我要搜索的!非常感谢!请稍候,是否可以更改字母%s和%d?不,不可以。请参阅
java.util.Formatter
的文档,以了解更多关于转义序列可能是什么以及这些转义序列意味着什么的信息。@LawrenceLo回到C时代,在运行时您无法确定printf函数的参数类型(C缺少方法重载)。为了避免这个问题,%后面的字母指定了参数的类型。%s表示字符串,%d表示整数,%f表示浮点数。这应该总是匹配相应变量的类型。我们经常使用
MessageFormat
,并将格式字符串
I like{0}和I eat{1}等等作为加载的资源。这样,您就可以添加另一个资源来翻译成另一种语言。这也是为什么arg编号
{1}stuff{0}
优于printf
%s
,因为这些单词在另一种语言中可能以不同的顺序出现,但不必重新排列变量参数。
System.out.println(MessageFormat.format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));
import static java.text.MessageFormat.format;

System.out.println(format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));
System.out.printf("I like %s and I eat %d blah blah    prolonging this string because %s and so on %f.%n",
                  cake, cakeNumber, whyNot, number);
                  
System.out.format(...)
System.out.printf(...)
System.out.print(String.format(...)) // you will need to include the line break in the format
System.out.println(String.format(...)) // line break will be caused by println()
import static java.lang.String.format;
System.out.print(format(...))
System.out.println(format(...))
System.out.println(new Formatter().format(...))
System.out.println("I like " + cake + " and I eat " +
                   cakeNumber + " blah blah    prolonging this string because" +
                   whyNot + " and so on " + number + ".");
$ javac Foo.java
$ java Foo

--- Original ---
I like whatever and I eat 509 blah blah     prolonging this string because another string and so on 1.0.

--- whitespace is your friend---
I like whatever and I eat 509 blah blah     prolonging this string because another string and so on 1.0.

--- Also, look into printf ---
I like "whatever" and I eat 00509 blah blah     prolonging this string because "another string" and so on  1.00
public class Foo {
   public static void main( String args[] ) {
      String cake = "whatever";
      int cakeNumber = 509;
      String whyNot = "another string";
      double number = 1;

      System.out.println("\n--- Original ---");
      System.out.println("I like "+ cake +" and I eat " + cakeNumber + " blah blah     prolonging this string because " + whyNot + " and so on " + number + ".");

      System.out.println("\n--- whitespace is your friend---");
      System.out.println("I like "+ cake 
         +" and I eat " + cakeNumber 
         + " blah blah     prolonging this string because " 
         + whyNot + " and so on " 
         + number + ".");


      System.out.println("\n--- Also, look into printf ---");
      //  https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
      System.out.printf("I like \"%s\" and I eat %05d blah blah"
         +"     prolonging this string because \"%s\" and so on %5.02f\n" 
         , cake 
         , cakeNumber 
         , whyNot
         , number );
   }

}