2014-08-31 159 views
0

我是新來的。我想問一個問題,因爲我沒有在搜索中找到我想要的東西。替換第n個字符串

這是通過問題和輸出的問題。這是流程:

public class change{ 
    public static void main(String args[]){ 
     Random rand = new Random(); 
     int number = 9999 + rand.nextInt(190000); 
     int replace = 1 + rand.nextInt(5); 
     String numcon = Integer.toString(number); 
     String display = ????numcon????; 

我想要的是將numcon的某個或第n個位置的字符替換爲「_」。像這樣: 假設numcon已經隨機化爲「1234567」,替換在1到6之間隨機化。這應該是System.out.print(display)的外觀。

replace/display 
1  /"_23456" 
2  /"1_3456" 
3  /"12_456" 
4  /"123_56" 
5  /"1234_6" 
6  /"12345_" 

回答

0
public class change{ 
    public static void main(String args[]){ 
    Random rand = new Random(); 
    int number = 9999 + rand.nextInt(190000); 
    int replace = 1 + rand.nextInt(5); 
    byte[] numcon = Integer.toString(number).getBytes(); 
    numcon[replace] = '_'; 
    String display = new String(numcon); 
+0

謝謝,它的工作原理雖然。 – 2014-08-31 15:33:07

相關問題