2013-11-26 36 views
1

我對java很新,所以這可能看起來像一個基本問題。我嘗試使用隨機java.util和nextInt在用戶輸入指定的範圍內創建一個隨機數,然後將其轉換爲字符,然後將其存儲在數組中;使用import java.util.Random; nextInt和用戶輸入

gridCells[x][y] = (char)(r.nextInt(numberOfRegions) + 'a'); 

但是,因爲我想nextInt使用的用戶輸入,雖然IM控制值的範圍,因爲nextInt認爲numberOfRegions可能是0即時猜測錯誤是怎麼引起的?

// Map Class 
import java.util.Random; 

public class map 
{ 
    // number of grid regions 
    private int numberOfRegions; 
    private boolean correctRegions = false 

    // grid constants 
    private int xCord = 13; // 13 so the -1 makes 12 for a 12x12 grid 
    private int yCord = 13; 

    // initiate grid 
    private int[][] gridCells = new int[xCord][yCord]; 

    Random r = new Random(); 

    map() { } 

    // ask for number of regions 
    public void regions() 
    { 
     keyboard qwerty = new keyboard(); // keyboard class 
     while(correctRegions = false) 
     { 
      System.out.print("Please enter the number of regions: "); 
      numberOfRegions = qwerty.readInt(); 
      if(numberOfRegions < 2) // nothing less then 2 accepted 
      { 
       correctRegions = false; 
      } 
      else if(numberOfRegions > 4) // nothing greater then 4 accepted 
      { 
       correctRegions = false; 
      } 
      else 
      { 
       correctRegions = true; 
      } 
     } 
    } 

    // fills the grid with regions 
    public void populateGrid() 
    { 
     for(int x =0; x<gridCells[x].length-1; x++) // -1 to avoid outofboundsexception error 
     { 
      for(int y =0; y<gridCells[y].length-1; y++) 
      { 
       gridCells[x][y] = (char)(r.nextInt(numberOfRegions) + 'a');  
      } 
     } 
    } 

    public void showGrid() 
    { 
     for(int x=0;x<gridCells[x].length-1; x++) 
     { 
      for(int y=0; y<gridCells[x].length-1; y++) 
      { 
       System.out.print(gridCells[x][y] + " "); 
      } 
      System.out.println(); 
     } 
    } 
} 
+5

什麼是錯誤? – mvieghofer

+0

private boolean correctRegions = false;' 有一個額外的'標記,將不會編譯... – wxyz

+0

這屬於codereview.stackexchange比這裏更... –

回答

1
public void populateGrid() 
{ 
    for(int x =0; x<gridCells[x].length-1; x++) // -1 to avoid outofboundsexception error 
    { 
     for(int y =0; y<gridCells[y].length-1; y++) 
     { 
      gridCells[x][y] = (char)(r.nextInt(numberOfRegions) + 'a'); 
     } 
    } 
} 

這是假的,要麼你做index < array.lengthindex <= array.length-1

index < array.length-1很可能不是你想要的。

此外,如果您遇到編譯錯誤,可能是因爲您沒有初始化numberOfRegions。通常情況下,這不是一個錯誤,而是一個警告,但也許你的編譯器設置爲在這種情況下發出錯誤。嘗試

private int numberOfRegions = 0; 
+0

我試着分配numberOfRegions,但是當它運行我的程序時,它繞過了while循環,並沒有要求用戶輸入。這是因爲我搞砸了==? –

+0

@ user3036238是的,這是因爲操作符,如果將'='更改爲'==',您的while循環將不會被跳過。 – Octoshape

+0

謝謝你,在我的演講結束後病倒了,並且生病回到你身邊。對不起,基本的問題,感到糟糕的錯過==哈哈 –

0

嗯,我已經發現了一些:

while條件賦值:

while(correctRegions = false) 

你應該寫:

while(correctRegions == false) // == is a boolean operator, = is an assignment 
+0

謝謝我不是很習慣java我是用pascal開始的我經常忘記==平等 –

1

你必須知道如何在Java .util.random的作品。

Random r = new Random(); 

int number = r.nextInt(numberOfRegions); 

這將產生一個從零(0)到你的numberRegions的整數。 從生成的烏爾可能範圍內的隨機數的排除零,這樣做

int number = 1 + r.nextInt(numberOfRegions); 

與此,可產生的最小數目是1

int number = 2 + r.nextInt(numberOfRegions); 

與此,最小數目可產生的是2

...and so on 
+0

好的,謝謝你,我的演講結束後,我也試試了。 –