【问题标题】:Using import java.util.Random; nextInt and user input使用导入 java.util.Random; nextInt 和用户输入
【发布时间】:2013-11-26 11:59:25
【问题描述】:

我对 java 还很陌生,所以这似乎是一个基本问题。我正在尝试使用随机 java.util 和 nextInt 在用户输入指定的范围内创建一个随机数,然后将其转换为一个字符,然后存储在一个数组中;

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

但是,因为我希望 nextInt 使用用户输入,并且虽然我控制了值的范围,但我猜测错误是因为 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();
        }
    }
}

【问题讨论】:

  • private boolean correctRegions = false;` 有一个额外的 ` 标记,不会编译...
  • 这比这里更属于 codereview.stackexchange...
  • 由于某种原因没有显示错误(一定是当我将代码添加到帖子中时)它是非法参数异常 n 必须是正错误

标签: java random


【解决方案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 &lt; array.length要么index &lt;= array.length-1

index &lt; array.length-1 很可能不是您想要的。

另外,如果你得到编译错误,可能是因为你没有初始化 numberOfRegions。通常,这不是错误而是警告,但在这种情况下,您的编译器可能设置为发出错误。试试看

private int numberOfRegions = 0;

【讨论】:

  • 我尝试分配 numberOfRegions 但是当它运行我的程序时,它绕过了 while 循环并且没有要求用户输入。这是因为我搞砸了 ==?
  • @user3036238 是的,这是因为运营商的原因,如果您将 = 更改为 == 您的 while 循环将不会被跳过。
  • 谢谢你在我的演讲后把它改掉,我会回复你的。对不起,基本问题,错过==哈哈
  • 是的,现在已经分配了 numberOfRegions。还更改了索引
【解决方案2】:

你必须知道 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

【讨论】:

  • 好的,谢谢你在我的演讲之后也尝试一下。
【解决方案3】:

嗯,我发现了一些东西:

您的while 条件是一个赋值:

while(correctRegions = false)

你应该写:

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

【讨论】:

  • 谢谢我不太习惯java 我从pascal开始我经常忘记==相等
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多