如何在句子中间插入一个变量,其中()是
System.out.println("Since there are (trying to insert int bluerocks here) there must be less than 12 normal rocks "+bluerocks);
答案 0 :(得分:1)
你有一些选择。
使用字符串连接,就在您想要的位置(注意引号数):
System.out.println("Since there are (" + bluerocks
+ ") there must be less than 12 normal rocks");
...或使用符号替换,通过printf
:
System.out.printf("Since there are (%d) there must be less than 12 normal rocks",
bluerocks);
答案 1 :(得分:0)
您可以通过多种方式执行此操作,Java支持内联字符串连接,因此这是有效的:
System.out.println("Since there are " + bluerocks + " there must be less than 12 normal rocks ");
或者,您可以使用format
方法:
System.out.format("Since there are %d there must be less than 12 normal rocks \n", bluerocks);
答案 2 :(得分:0)
您可以使用String
concatenation operator +
和println()
之类的
System.out.println("Since there are " + bluerocks
+ " there must be less than 12 normal rocks");
System.out.printf("Since there are %d there must be "
+ "less than 12 normal rocks%n", bluerocks);
答案 3 :(得分:0)
这里的很多人都给了你正确的答案
System.out.println("Since there are " + bluerocks + " there must be less than 12 normal rocks");
或
System.out.format("Since there are %d there must be less than 12 normal rocks \n", bluerocks);
或
System.out.printf("Since there are %d there must be than 12 normal rocks%n", bluerocks);
但如果这似乎很复杂或困难,那么你总是可以在多个打印陈述中将其分解
System.out.print("Since there are ");
System.out.print(bluerocks);
System.out.print(" there must be less than 12 normal rocks\n");
一切都是关于学习,不需要在这里大惊小怪。