简体   繁体   English

如何在同一个 println “System.out.println”中打印这 2 个变量

[英]How can I print this 2 Variables in the same println “System.out.println”

I have two int variables, abdou1 and abdou2 , I wish to print the values of these.我有两个int变量, abdou1abdou2 ,我希望打印这些值。 I have tried below which does not work.我试过下面这不起作用。

public class Math1 {
    public static void main (String args[]) {
        int abdou1 = 115;
        double abdou2 = 1122.176876; 
        System.out.println(abdou1, abdou2);
    }
}

使用+号而不是逗号System.out.println(abdou1+" "+ abdou2);

This is very simple and can be achieved by using:这很简单,可以通过使用:

public static void main (String args[]) {

    int abdou1 = 115;
    double abdou2 = 1122.176876;

    System.out.println(abdou1 + " "  + abdou2);
}

The reason it works like this is that println interprets the object calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println() .它像这样工作的原因是println解释对象调用String.valueOf(x)以获得打印对象的字符串值,然后表现得好像它调用了print(String)然后println() Therefore, every parameter is treated as a String .因此,每个参数都被视为String

If you are printing objects of a custom class, you should override toString() to what you want to print.如果要打印自定义类的对象,则应将toString()覆盖为要打印的对象。

you should use "+" operator.您应该使用“+”运算符。 Since this is a numerical value, if you directly use "+" between both the numbers, it will sum up.由于这是一个数值,如果在两个数字之间直接使用“+”,则会相加。 hence use a string separator like the one mentioned below因此使用如下所述的字符串分隔符

System.out.println(abdou1+" "+ abdou2); System.out.println(abdou1+" "+ abdou2);

您可以使用

System.out.println(String.format("Int value: %d\\nDouble value: %f", abdou1, abdou2));

System class holds a static reference to out which is of type PrintStream . System类持有一个对out类型的PrintStream的静态引用。 If you see the signature of println() then you would see that it has various overloaded forms but each accepts single parameter.如果您看到println()的签名,那么您会看到它有多种重载形式,但每种形式都接受单个参数。 You can try like below.您可以尝试如下。

public class Math1 {
    public static void main (String args[]) {
        int abdou1 = 115;
        double abdou2 = 1122.176876; 
        System.out.println(abdou1 + " "+ abdou2);
    }
}

As printf method in java works same like C printf function so have to use format specifiers here to identify the data type.由于 java 中的 printf 方法与 C printf 函数的工作方式相同,因此必须在此处使用格式说明符来标识数据类型。

public class Math1 {
public static void main (String args[]) {
    int abdou1 = 115;
    double abdou2 = 1122.176876; 
    System.out.println(String.format("%d %f", abdou1, abdou2));
}

You can use these Format Specifiers for different data types您可以将这些格式说明符用于不同的数据类型

  • %c or %C Display characters %c 或 %C 显示字符
  • %d Displays a decimal (base 10 ) integer %d 显示十进制(以 10 为底)整数
  • %e or %E Display a floating point number in exponential notation %e 或 %E 以指数表示法显示浮点数
  • %f Display a floating point value in decimal format %f 以十进制格式显示浮点值
  • %s or %S Display Strings %s 或 %S 显示字符串
  • %b or %B Display boolean values %b 或 %B 显示布尔值
  • %g (%G) float or double use %f or %e as required %g (%G) 根据需要浮动或双重使用 %f 或 %e
  • %o int unsigned octal value %o int 无符号八进制值
  • %p pointer address stored in pointer %p 指针地址存储在指针中
  • %s array of char sequence of characters or String %s 字符或字符串的 char 序列数组
  • %u int unsigned decimal %u int 无符号十进制
  • %x (%X) int unsigned hex value %x (%X) int 无符号十六进制值
  • %% Display a % sign %% 显示 % 符号

You can use whitespace characters which are您可以使用空格字符

  • space ( ' ' )空间 ( ' ' )
  • tab ( '\\t' )制表符('\\t')
  • carriage return ( '\\r' )回车 ( '\\r' )
  • newline ( '\\n' )换行符 ( '\\n' )
  • ormfeed ( '\\f' ) ormfeed ( '\\f' )

For more further explanation and examples with other data types you can go through this link.有关其他数据类型的更多说明和示例,您可以访问此链接。
Format Specifiers 格式说明符

You can use printf or format eg您可以使用 printf 或 format 例如

System.out.format("index = %d and %d",i,j); System.out.format("index = %d and %d",i,j);

System.out.printf("index = %d and %d",i,j); System.out.printf("index = %d and %d",i,j);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM