-2

I am a novice programmer taking an intro to programming course and I have to come up with a program that creates the minimum number of coins for a number entered between 1-99.

I have done this, now I'm trying to output those coins.

scanner Userinput = new Scanner(System.in);

int stuff = Userinput.nextInt();
int q = stuff/quarters;
String A = "Number of Quarters";

System.out.println(A,q);

Now what I want to happen is the output to look like this

Number of Quarters: (whatever my number of quarters is)

I can get it to work by doing

System.out.println("Number of Quarters:");
System.out.println(q);

And it outputs as

Number of Quarters:
(quarters)

I need them to be right next to one another but cannot figure out how to do it?

4

6 回答 6

2

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings

You can modify A to store directly the result q :

String A = "Number of Quarters: "+q;
System.out.println(A);

Or print it directly:

System.out.println(A+" "+q);

于 2013-11-04T10:25:11.910 回答
1

作为连接q"Number of Quarters: "预先连接的替代方法:

System.out.print("Number of Quarters: ");
System.out.println(q);
于 2013-11-04T10:27:49.053 回答
0

System.out是类PrintStream的一个实例。
PrintStream类没有方法println(String, int)
您应该使用另一种方法,例如println(String)println(int)

System.out.println(A);
System.out.println(q);
于 2013-11-04T10:29:25.463 回答
0

System.out.println()函数自动在要打印的行尾打印一个换行符。这就是为什么您要在两行中获得输出。为了避免打印时终止换行符,您可以使用System.out.print()函数或System.out.printf()函数。

System.out.print("Number of quarters: ");
System.out.println(q);

或者

System.out.printf("Number of quarters : %s\n", q);
于 2013-11-04T10:32:10.633 回答
0

连接之外的另一种方式

System.out.print("Number of Quarters: ");
System.out.print(q);

使用print代替println

于 2013-11-04T10:33:16.257 回答
0

或者您可以使用 printf 而不是 println ,您可以打印相同的内容 n 从下一个开始当您想要打印多个输出时不要使用 println

于 2016-07-30T11:58:19.103 回答