我一直在为一个分配程序遇到问题。分配是用Java生成代码,允许用户输入状态,状态鸟和状态花,然后将其打印出来,或者只是退出程序。我实际上大部分时间都完美地处理了编写数组的异常。我在方法存储中有数组,但会反弹到方法介绍,以便我可以利用if语句。这导致循环始终重置为零。我通过添加一个名为COUNTER的INT变量修复此问题,该变量将在方法外增加,然后将变量i,j和k设置为等于它以修复上一个问题。问题,它不起作用,所以我改回程序,并没有它不会访问存储方法,我不知道为什么。
感谢。
import java.util.*;
public class states {
//create string in order to store state data
int i,j,k = 0;
String theState;
String bird;
String flower;
String[][] states = new String[i][j];
boolean intro;
int counter = 0;
//scanner to get data
Scanner input= new Scanner(System.in);
//INtro method, maybe have it throw a bulean out back to other code
public String intro ()
{
//System.out.println("Wlecome to this program.");
System.out.println("Enter a state or select none to exit.");
String initial = input.nextLine();
if(initial.equalsIgnoreCase("None"))
{
this.intro = false;
exit(initial);
//return intro;
return initial;
}
else
{
store(states, initial);
this.intro = true;
//return intro;
return initial;
}
}
//storing data
public void store(String a[][], String initial)
{
for (int i=0; i < states.length;i++)
{
theState = initial;
System.out.println("Please Enter State Bird.");
String bird = input.nextLine();
for (int j = counter; j < states.length; j++)
{
System.out.println("Please Enter State Flower.");
String flower = input.nextLine();
for (int k = 0; j < states.length; k++)
{
states[i][j]="Your state is: " + theState +"Its Bird is: " + bird +"Its flower is: " + flower;
intro();
}
}
}
/*states[i][j][k]=theState + " " + bird + " "+ flower;
i++;
j++;
k++;
intro();*/
}
//exit clause
/*public String toString(String a[][])
{
String theEnd = " ";
//print array
for (int row = 0; row < states.length; row ++)
{
for(int column = 0; column < states[row].length; column ++)
{
for(int col = 0; col < states[row][column].length; col++)
{
theEnd = (states[row][column][col] + " ");
}
}
}
//exit program
return theEnd;
}*/
public void exit(String theEnd)
{
System.out.println(theEnd);
for (int i = 0; i < states.length;i++)
{
for(int j = 0; j < states[i].length; j ++)
{
System.out.println(states[i][j] + " ");
/* for(int col = 0; col < states[row][column].length; col++)
{
System.out.println(states[row][column] + " ");
}*/
}
}
//exit program
System.out.println("Thank you for using this program, Good Bye.");
System.exit(0);
}
}