2013-11-26 35 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

好的,谢谢你,我的演讲结束后,我也试试了。 –