代码之家  ›  专栏  ›  技术社区  ›  RedAISkye

Java中的单倒逗号有什么用?[副本]

  •  0
  • RedAISkye  · 技术社区  · 8 年前

    我刚开始学习Java编程语言,有一件事我不明白。

    因此,下面的代码用于计算两个给定数字的和,基本上,这是我的主要方法:

    public class Addition{
    
    public static void main(String[]args){
    
        Scanner add = new Scanner ( System.in );
    
        System.out.println("Enter the first number:"+' ');
    
        int num1 = add.nextInt();
    
        System.out.println("Enter the second number:"+' ');
    
        int num2 = add.nextInt();
    
        int calculate = num1 + num2;
    
        System.out.println(num1 + ' ' + "+" + ' ' + num2 + "=" + ' ' + calculate);
    
        add.close();
    
    }
    

    所以 ' ' 给定的是空间,代码只是用于计算2个给定数字的和

    例如,两个数字分别是15和5。因此输出应该如下:

    Enter the first number: 
    15
    Enter the second number: 
    5
    15 + 5 = 20
    

    但是没有!输出如下所示:

    Enter the first number: 
    15
    Enter the second number: 
    5
    47 + 5 = 20
    

    这里应该是15而不是47。所以我用一个较短的代码替换了代码,如下所示:

    System.out.println(num1 + " + " + num2 + "= " + calculate);
    

    这修复了我的问题,输出显示如我所预期的,但我很好奇。怎么了 ' ' ? 当我把代码设置为

    (num1 + ' ' + "+" + num2 + "=" + ' ' + calculate)
    

    则不显示输入值 num1 在输出中,值 num1 num1 当我把空间 ' '

    基本上,我是在问这是怎么回事 ' ' ?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Pavneet_Singh    8 年前

    因为 ' ' 是字符, " " String .

    添加 int 一串 一串

    添加 智力 char 是具有字符代码(ASCII)的数字操作,将导致 智力 .