如何将System.out.println()放入变量中

  • 本文关键字:变量 println System out java
  • 更新时间 :
  • 英文 :


这里有我想要使用的代码。我基本上想把字符串和变量一起使用,但把它保存在一个变量中

Int a = 27;
Int b = 22;
String question = System.out.println("what is " + a + "+" + b);
System.out.println(question);

您正试图将System.out.println("what is " + a + "+" + b);的返回值分配给变量question,但System.out.println不返回任何值。你必须首先创建你的变量,然后打印它,就像这样:

Int a = 27;
Int b = 22;
String question = "what is " + a + "+" + b;
System.out.println(question);

您可以像在System.out.println.中那样轻松地为变量连接字符串

所以

String question ="what is " + a + "+" + b;

会胜任这项工作。

从字符串变量question中删除System.out.println((。因此,字符串问题="什么是"+a+"+"+b;当你想提问时,用System.out.println((打印可变问题

试试这个,使用字符串串联:

Int a = 27;
Int b = 22;
String questionStr = "what is " + a + " + " + b;
System.out.println(questionStr);

最新更新