简体   繁体   English

PrintStream 类型中的方法 println(int) 不适用于参数 (String, int, int, int)

[英]The method println(int) in the type PrintStream is not applicable for the arguments (String, int, int, int)

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".我正在尝试打印像“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)". PrintStream 类型中的方法 println(int) 不适用于参数 (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

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.您当前使用println的解决方案不起作用,因为println无法以这种方式格式化文本输出,您必须(正如其他人所说)使用“+”运算符连接字符串。 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.您使用的方法(System.out.println ) 不适用于多个参数。 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.这是 PrintStream 的文档。 As you can see there is no method like you are using it( System.out.println(String, Int, Int ,Int); ).如您所见,没有像您使用它的方法( 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. 此处记录的 println当前接受 0 或 1 个参数,这是上述错误的原因。

You need to either concatenate or use String Format to format the result if you intend to use println(only):如果您打算使用 println(only),则需要连接或使用字符串格式来格式化结果:

  • String Format: System.out.println(String.format("%dx %d = %d\n",num1,num2,result));字符串格式: 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 进行字符串连接: 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从打印语句中删除逗号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PreparedStatement 类型中的 setString(int, String) 方法不适用于参数 (int, String[]) - The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, String[]) 类型中的方法不适用于参数(int) - The method in the type is not applicable for the arguments (int) Java Writer 错误:Writer 类型中的方法 write(char[], int, int) 不适用于 arguments (int, double, int, String) - Java Writer error: The method write(char[], int, int) in the type Writer is not applicable for the arguments (int, double, int, String) 方法 sum(int, int, int, int) 不适用于参数 (int) - The method sum(int, int, int, int) is not applicable for the arguments (int) ArrayList 类型中的方法 set(int, Int)<int> 不适用于 arguments (int, int) 长度无法解析或不是字段</int> - The method set(int, Int) in the type ArrayList<Int> is not applicable for the arguments (int, int) length cannot be resolved or is not a field main类型的方法(double [])不适用于参数(int []) - The method (double[]) in the type main is not applicable for the arguments (int[]) ArrayBoss 类型中的方法 (int[]) 不适用于参数 () - The method (int[]) in the type ArrayBoss is not applicable for the arguments () 类型X中的方法X不适用于参数(int) - The method X in the type X is not applicable for the arguments (int) prosedure_jsp类型的pro(int,int)方法不适用于参数() - The method pro(int, int) in the type prosedure_jsp is not applicable for the arguments () Arrays 类型中的 asList(T[]) 方法不适用于参数 (int, int) - The method asList(T[]) in the type Arrays is not applicable for the arguments (int, int)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM