有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java系统。出来println和字符串参数

当我写作时:

System.out.println("Give grade: ", args[0]);

它给出了错误:

The method println(String) in the type PrintStream is not applicable for the arguments (String, String).

为什么会这样?然而,当我尝试写作时

System.out.println("Give grade :");
System.out.println(args[0]);

没有显示错误。有没有办法把上面的内容写在一行println()


共 (6) 个答案

  1. # 1 楼答案

    您可以使用的另一种方法是format。它接受任意数量的参数,并以各种方式格式化它们。您应该熟悉其他语言的模式,它们非常标准

    System.out.format("Give grade: %s%n", args[0]);
    
  2. # 2 楼答案

    你可以:

    System.out.println("Give grade: " + args[0]);
    

    或者以C风格:

    System.out.printf("Give grade: %s%n", args[0]);
    
  3. # 3 楼答案

    ^{}javadoc中,它注意到它只接受一个参数

    相反,您可以连接数据以形成单个String参数:

    System.out.println("Give grade: " + args[0]);
    

    您可能需要检查^{}

    System.out.printf("Give grade: %s\n", args[0]);
    

    请注意,上面的方法在Java5之后就可用了(但您肯定使用的是Java7或Java8)

  4. # 4 楼答案

    System.out.println(String text);在内部调用^{}方法,它需要一个参数

    您可以将它们连接到字符串文本,并像下面那样传递它

    System.out.println("Give grade: " + args[0]);
    
  5. # 5 楼答案

    一行。这只是内联进行字符串连接

    System.out.println("Give grade: "+ args[0]);
    
  6. # 6 楼答案

    两个有效的参数只取一个,失败的参数取两个。你有Javascript或Python背景吗?Java强制执行参数类型和计数(如C)

    试试看

    System.out.println("Give grade: " + args[0]);

    或者

    System.out.printf("Give grade: %s%n", args[0]);