为什么这段代码使用随机字符串打印出了”hello world”

    System.out.println(randomString(-229985452) + " " + randomString(-147909649));    
        
    public static String randomString(int i){    
        Random ran = new Random(i);    
        StringBuilder sb = new StringBuilder();    
        while (true)    
        {    
            int k = ran.nextInt(27);    
            if (k == 0)    
                break;    
        
            sb.append((char)('`' + k));    
        }    
        
        return sb.toString();    
    }    

从这篇博客看到的这个问题,但是他的解释,我没看明白。希望大神解释下。

慕桂英4014372
浏览 480回答 2
2回答

拉风的咖菲猫

  new Random(-229985452).nextInt(27)The first 6 numbers that the above random generates are: 851212150and the first 6 numbers that new Random(-147909649).nextInt(27) generates are: 2315181240Then just add those numbers to the integer representation of the character ` (which is 96): 8 + 96 = 104 --> h5 + 96 = 101 --> e12 + 96 = 108 --> l12 + 96 = 108 --> l15 + 96 = 111 --> o 23 + 96 = 119 --> w15 + 96 = 111 --> o18 + 96 = 114 --> r12 + 96 = 108 --> l4 + 96 = 100 --> d
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java