我刚刚开始学习Java,目标是为Android制作游戏。我正在上一个在线课程,在那里我获得了一项任务,后来又进行了某种评估。
这是我到目前为止所做的:
public String[] pie;
public Scanner x;
public String[] name;
public String[] name1;
public String[] name2;
public int[] year;
public int[] numb;
public String[] language;
public boolean[] read;
public int[] rating;
public void openfile(){
try{x = new Scanner(new File("Raamatukogu.txt"));}
catch(Exception e){
System.out.println("no file was found");}}
public void readfil(){
while(x.hasNext()){
String rida = x.nextLine();
pie = rida.split("#");
for(int i = 0; i < pie.length ; i++){
我想要做的是在我的代码中添加类似的东西,但是自动化:
name[0] = pie[0]
name1[0] = pie[1]
name2[0] = pie[2]
year[0] = pie[3]
numb[0] = pie[4]
language[0] = pie[5]
read[0] = pie[6]
rating[0] = pie[7]
name[1] = pie[8]
name1[1] = pie[9]
name2[1] = pie[10]
year[1] = pie[11]
numb[1] = pie[12]
language[1] = pie[13]
read[1] = pie[14]
rating[1] = pie[15]
name[2] = pie[16]
name1[2] = pie[17]
...}
提前谢谢你!
答案 0 :(得分:4)
试试这个
int j=0;
for(int i=0;i<pie.length;i++){
name[j] = pie[0];
name1[j] = pie[1];
name2[j] = pie[2];
year[j] = pie[3];
numb[j] = pie[4];
language[j] = pie[5];
read[j] = pie[6];
rating[j] = pie[7];
j++;
}
答案 1 :(得分:0)
这是同一枚硬币的两面。假设pie.length
是8的倍数,因为您的代码似乎也假设为。
for (int i=0; i<pie.length/8; i++) {
int index = i * 8;
name[i] = pie[index];
name1[i] = pie[index+1];
name2[i] = pie[index+2];
year[i] = pie[index+3];
numb[i] = pie[index+4];
language[i] = pie[index+5];
read[i] = pie[index+6];
rating[i] = pie[index+7];
}
或
for (int i=0; i<pie.length; i=i+8) {
int index = i/8;
name[index] = pie[i];
name1[index] = pie[i+1];
name2[index] = pie[i+2];
year[index] = pie[i+3];
numb[index] = pie[i+4];
language[index] = pie[i+5];
read[index] = pie[i+6];
rating[index] = pie[i+7];
}