简体   繁体   中英

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. 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() . Therefore, every parameter is treated as a String .

If you are printing objects of a custom class, you should override toString() to what you want to print.

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(String.format("Int value: %d\\nDouble value: %f", abdou1, abdou2));

System class holds a static reference to out which is of type PrintStream . If you see the signature of println() then you would see that it has various overloaded forms but each accepts single parameter. 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.

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
  • %d Displays a decimal (base 10 ) integer
  • %e or %E Display a floating point number in exponential notation
  • %f Display a floating point value in decimal format
  • %s or %S Display Strings
  • %b or %B Display boolean values
  • %g (%G) float or double use %f or %e as required
  • %o int unsigned octal value
  • %p pointer address stored in pointer
  • %s array of char sequence of characters or String
  • %u int unsigned decimal
  • %x (%X) int unsigned hex value
  • %% Display a % sign

You can use whitespace characters which are

  • space ( ' ' )
  • tab ( '\\t' )
  • carriage return ( '\\r' )
  • newline ( '\\n' )
  • 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

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

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

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.

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