public static void main(String[] args) {
int num1 = 1;
int num2 = 1;
int result = num1 * num2;
System.out.println("%d x %d = %d\n",num1,num2,result);
}
I am trying to printout a form like "1 * 10 = 10". However I get an error:
The method println(int) in the type PrintStream is not applicable for the arguments (String, int, int, int)".
I don't know what's the problem and how should I change it?
Try
System.out.println(num1+" x "+num2+" = "+result+"\n");
UPDATE: Some of you are saying this concatenation method is slower than other methods. You are right, it is slower, but does it really matter for this example?
This method is usually used to debug, not as part of the final code, and usually only once or twice on the whole code.
Faster method:
System.out.printf("%d x %d = %d\n",num1,num2,result);
Have you tried using system.out.format
like this:
System.out.format("%s x %s = %s\n",num1,num2,result);
Your current solution using println
isn't working as println
cannot format text output in this way, you would have to (as others have said) concatenate the string using the "+" operator. It's slower in most cases but for debugging purposes I shouldn't imagine it's much of a problem either way.
System.out
中有一个printf(...)
方法!
System.out.printf("%d x %d = %d\n",num1,num2,result);
The Method your using (System.out.println
) isn't made for multiple parameters. By using ,
you try to give it multiple parameters. You need the +
-operator.
Applied to your code it should look like that:
System.out.println(num1 + " x " + num2 + " = " + result);
Maybe you should look at this . This is the Documentation of PrintStream. As you can see there is no method like you are using it( System.out.println(String, Int, Int ,Int);
).
One of the simple way is that you can use the concatenation operation '+' instead of ',' to print
public static void main(String[] args) {
// TODO Auto-generated method stub
int result = 0;
for(int num1 = 1;num1 < 10;num1++){
for(int num2 = 1;num2 <10;num2++){
result = num1 * num2;
System.out.println(num1+"x"+num2+"="+result);
}
}
}
}
println as documented here currently accepts 0 or 1 arguments, reason for the mentioned error.
You need to either concatenate or use String Format to format the result if you intend to use println(only):
String Format: System.out.println(String.format("%dx %d = %d\n",num1,num2,result));
String Concatenation using StringBuffer: StringBuffer resltBuffer = new StringBuffer(num1).append(" x ").append(num2).append(" = ").append(result); System.out.println(resltBuffer.toString());
StringBuffer resltBuffer = new StringBuffer(num1).append(" x ").append(num2).append(" = ").append(result); System.out.println(resltBuffer.toString());
enter code here
int first = 20;
int second = 30;
System.out.println("before swap");
System.out.println("first value :" +first);
System.out.println("second value: " +second);
first = first - second;
second = first + second;
first = second - first;
System.out.println("after swap");
System.out.println("first value: " +first);
System.out.println("second value: " +second);
remove comma from the print statement
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.