Java循环问题读取错误

Java循环问题读取错误,java,Java,好的,我试着得到这个输出: 死亡0:6个字母 死亡1:6个字母 等等: 现在它打印出了0:然后整个读取文件的字母 这就是执行循环的类,我发布了其他方法,这些方法是从不同的java包中执行的 public void populateDice() { //Loop through the 16 dice times for (int row = 0; row < NUMBER_OF_DICE; row++){ //Create an instance of clas

好的,我试着得到这个输出: 死亡0:6个字母 死亡1:6个字母 等等:

现在它打印出了0:然后整个读取文件的字母

这就是执行循环的类,我发布了其他方法,这些方法是从不同的java包中执行的

public void populateDice()
{

    //Loop through the 16 dice times
    for (int row = 0; row < NUMBER_OF_DICE; row++){

    //Create an instance of class Die using the no-argument constructor
        //Die die = new Die();

    //Loop through the 6 sides of the die:
        for (int col= 0; col < NUMBER_OF_SIDES; col++)
        {

            Die die = new Die();
    //Add each of the 6 letters to the die ArrayList representing 
    //the die letters by calling method addLetter in class Die 
           die.addLetter(diceData.get(col).toString());

    //Display the letters of each die by calling method displayAllLetters() in class Die on a separate row
           System.out.println("Die " + row + ":");
           die.displayAllLetters();

    }


    }
   }


public void displayAllLetters()       
{

    try{

    //Loop through all sides of the die and display the data
    for(int x = 0; x < NUMBER_OF_SIDES; x++)
    {
        System.out.println(diceStore.get(x));
    }
    }
    catch(Exception ex)
    {
        System.out.printf("ERROR %s", ex.toString());

    }

}
public void populateDice()
{
//循环16次骰子
for(int row=0;row<骰子数;row++){
//使用无参数构造函数创建类Die的实例
//模具=新模具();
//在模具的6个侧面进行循环:
for(int col=0;col<边数;col++)
{
模具=新模具();
//将6个字母中的每一个添加到表示
//通过调用类die中的addLetter方法来调用die字母
die.addLetter(diceData.get(col.toString());
//通过调用die类中的displayAllLetters()方法在单独的行上显示每个die的字母
System.out.println(“模具”+行+:”);
die.displayAllLetters();
}
}
}
public void displayAllLetters()
{
试一试{
//在模具的所有侧面循环并显示数据
对于(int x=0;x<边数;x++)
{
System.out.println(diceStore.get(x));
}
}
捕获(例外情况除外)
{
System.out.printf(“错误%s”,例如toString());
}
}

按照代码的结构,第一个for循环是die0。一旦你到达内部for循环,你就可以创建新的模具,添加1个字母,然后告诉它打印所有的字母

我认为您想要的是初始化所有边,然后打印字母,这样您就可以在内部for循环之外显示allletters()

 //Loop through the 16 dice times
for (int row = 0; row < NUMBER_OF_DICE; row++)
{

    //Create an instance of class Die using the no-argument constructor
    Die die = new Die();

    //Loop through the 6 sides of the die:
    for (int col= 0; col < NUMBER_OF_SIDES; col++)
    {

        //Die die = new Die(); --- remove this and do it in the above loop ^
        //Add each of the 6 letters to the die ArrayList representing 
        //the die letters by calling method addLetter in class Die 
        die.addLetter(diceData.get(col).toString());

        //Display the letters of each die by calling method    displayAllLetters() in class Die on a separate row
       //System.out.println("Die " + row + ":"); -----move this out of the for-loop
       //die.displayAllLetters(); ----- move this out of the for-loop

    }
    System.out.println("Die " + row + ":");
    die.displayAllLetters();

}
//循环16次骰子
for(int row=0;row<骰子数;row++)
{
//使用无参数构造函数创建类Die的实例
模具=新模具();
//在模具的6个侧面进行循环:
for(int col=0;col<边数;col++)
{
//模具=新模具();---取下此模具,并在上面的循环中执行此操作^
//将6个字母中的每一个添加到表示
//通过调用类die中的addLetter方法来调用die字母
die.addLetter(diceData.get(col.toString());
//通过调用die类中的displayAllLetters()方法在单独的行上显示每个die的字母
//System.out.println(“Die”+行+:”)----将其移出for循环
//die.displayAllLetters();----将其移出for循环
}
System.out.println(“模具”+行+:”);
die.displayAllLetters();
}

很难知道在哪里。请展示一个简短但完整的程序来演示问题。(顺便说一句,为什么你有一个名为
Die
的class
class
变量,这一点特别不清楚…)我为你清理了它这仍然不是一个简短但完整的程序。首先,它从一个方法声明开始。。。