2015-01-15 33 views
2

我在java編寫了四個月 - 現在仍然是一個新手!我想用一個框架在Java中創建一個隨機化的不重複數組來找到座標。我已經初始化了我想要的數組中的字符,但是我一直在打印輸出中停止,破折號和括號等。我錯過了什麼?我不想下去圖書館路線的僱用。創建一個隨機編碼陣列使用字母和數字從ASCII與指定的幀,並沒有重複在Java

final char[] randomSquare = {'C', 'O', 'D', 'I', 'N', 'G'}; 
// the Grid used that will be randomly filled 
char[][] grid; 

int A = 65; 
int Z = 90; 
int num0 = 48; 
int num9 = 57; 

// create a Random numbers generator 
Random ran = new Random(); 

grid = new char[randomSquare.length][randomSquare.length]; 

// loop to print randomSquare header for grid array 
for (int j = 0; j < 1; j++) { 
    System.out.print(" "); 
    System.out.print(randomSquare); 
} 
System.out.println(); 

// print randomSquare at position 0 all the way down 
for(int i = 0; i < randomSquare.length; i++) { 
    // the letter at the beginning of the row 
    System.out.print(randomSquare[i] + " "); 

    // the Grid contents for that line 
    int index = 0; 
    for(int j = 0; j < randomSquare.length; j++) { 

     if (A >= 65 && A <= 90){ 

      index = ran.nextInt(A + Z); 
     } 

     if (num9 <= 57 && num9 >= 48) { 

      index = ran.nextInt(num0 + num9); 
     } 
     System.out.print("" + (char)index); 
    } 
    System.out.println(); 
    index++; 
} 
return grid; 
+1

首先,您對A,num9等使用if語句,但在初始化它們之後再也不會設置這些值。不知道你需要他們什麼。其次,'ran.nextInt(int n)'將返回一個隨機數到該數字。所以基本上,你可以得到各種符號。例如,'ran.nextInt(A + Z)'可以獲得155以內的任何數字。#60是一個<符號。查看http://www.asciitable.com/。您還需要修復'ran.nextInt()' – Ascalonian

回答

0
下面

解決您的問題用AZ之外字符和0-9

for(int i = 0; i < randomSquare.length; i++) { 
     // the letter at the beginning of the row 
     System.out.print(randomSquare[i] + " "); 

     // the Grid contents for that line 
     int index = 0; 
     for(int j = 0; j < randomSquare.length; j++) { 

      if(ran.nextBoolean()){ 
      if (A >= 65 && A <= 90){ 

       index = A + ran.nextInt(Z - A); 
      } 
      }else 
      if (num9 <= 57 && num9 >= 48) { 

       index = num0 + ran.nextInt(num9 - num0); 
      } 
      System.out.print("" + (char)index); 
     } 
     System.out.println(); 
     index++; } 

    for(int i = 0; i < randomSquare.length; i++) { 
     // the letter at the beginning of the row 
     System.out.print(randomSquare[i] + " "); 

     // the Grid contents for that line 
     int index = 0; 
     for(int j = 0; j < randomSquare.length; j++) { 

      if(ran.nextBoolean()){ 
      if (A >= 65 && A <= 90){ 

       index = A + ran.nextInt(Z - A); 
      } 
      }else 
      if (num9 <= 57 && num9 >= 48) { 

       index = num0 + ran.nextInt(num9 - num0); 
      } 
      System.out.print("" + (char)index); 
     } 
     System.out.println() 

; 
     index++; } 
+0

感謝您的回覆,代碼比我試圖實現的準確得多。我只需要找到一種方法來排除現在正在打印的重複字符;) – rusty

0

我不認爲這是做你期望它做的事情,它不清楚你的願望。我將重新修改您的代碼以生成隨機數字或字母,但您可能需要仔細考慮您的代碼。

// the Grid contents for that line 


//perhaps index isn't the best name 
int index = 0; 
for(int j = 0; j < randomSquare.length; j++) { 

    //keep generating new indexe until we get one within desired range 
    while (!(index >= 65 && index <= 90 || index <= 57 && index >= 48)) 
     index = ran.nextInt(Z-num0)+num0; // gives numbers in a range from num0 to Z (48 to 90) 

    System.out.print("" + (char)index); 
} 
System.out.println(); 

//not sure why this would be necessary 
index++; 
0

從我的理解要填充的字母和數字,避免重複隨機陣列,也只是試試fiollowing:

//The array containing letters and numbers. 
char[] lettersAndNumbers = {'0','1','2','3','4','5','6','7','8','9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U','V','W','X','Y','Z'}; 

    // the new list to populate 
List<String> lc=new ArrayList<String>(); 
Random ran = new Random(); 
int i=0; 
while(i<20) { 
     //Choose a random index for to take an element from the array 
    int rand=ran.nextInt(lettersAndNumbers.length); 
     //put this element in the list if it doesn't exist 
    if(!lc.contains(String.valueOf(lettersAndNumbers[rand]))) { 
     lc.add(String.valueOf(lettersAndNumbers[rand])); 
     i++; 
    } 
} 

    //Convert your list to an array 
Object[] myArray=lc.toArray(); 
for (int j=0;j<myArray.length;j++){ 
    System.out.println(myArray[j]); 
} 
  • 創建一個包含所有的字母和數字的字符數組。
  • 創建一個列表並使用此數組中的隨機元素填充它(如果它們不在列表中)。
  • 將您的列表轉換爲數組。
0

我不知道我是否正確理解您要做的事。但是,如果你想要一個包含唯一引用的東西的集合,不要使用列表或數組,請使用HashSet

相關問題