Java printing a String containing an integer

前端 未结 9 771
执笔经年
执笔经年 2021-01-02 02:54

I have a doubt which follows.

public static void main(String[] args) throws IOException{
  int number=1;
  System.out.println(\"M\"+number+1);
}
相关标签:
9条回答
  • 2021-01-02 03:34

    A cleaner way to separate data from invariants:

    int number=1;
    System.out.printf("M%d%n",number+1);
    
    0 讨论(0)
  • 2021-01-02 03:38

    Add a bracket around your sum, to enforce the sum to happen first. That way, your bracket having the highest precedence will be evaluated first, and then the concatenation will take place.

    System.out.println("M"+(number+1));
    
    0 讨论(0)
  • 2021-01-02 03:38

    Try this:

    System.out.println("M"+(number+1));
    
    0 讨论(0)
提交回复
热议问题