java System.out.println() strange behavior long string

前端 未结 5 1331
再見小時候
再見小時候 2021-01-12 17:31

Can somebody explain me why this code does not print the numbers?

      String text = new String("SomeString");
      for (int i=0; i<1500; i++)          


        
5条回答
  •  暖寄归人
    2021-01-12 18:06

    There are a couple things you could try. I'll give you an example of both.

    First, in Java you can simply add strings together. Primitives such as int should be automatically converted:

      String text = new String("SomeString");
    
      for (int i = 0; i < 1500; i++) {
                text += i;
      }
    
      System.out.println(text);
    

    Second, if the first method still isn't working for you then you can try to explicitly convert your int to a String like so:

      String text = new String("SomeString");
    
      for (int i = 0; i < 1500; i++) {
                text += Integer.toString(i);
      }
    
      System.out.println(text);
    

提交回复
热议问题