比较逻辑不起作用

时间:2016-03-20 06:55:09

标签: java android if-statement logic

我需要在旋转序列中显示9个名字。像图片一样:

rotation schedule

如您所见,名称在列表中向下一个位置,第9个名称在每次旋转时位于其顶部。

控制旋转的变量是整数iName。这是一种只读取用户输入(图像按钮触摸)并添加或减少iName值的方法。它从0开始(列表显示Adams到Ida),当用户给出命令时它假定为1(因此列表从Ida显示为Henry)。 实际上,用户可以及时返回并将iName从5变为4,因此列表从Easy-Denver变为Frank-Easy。

我有9个TextView执行此工作,并在循环内更新,如下所示:

int[] arrNames_Ids = {R.id.Name1, R.id.Name2, R.id.Name3, R.id.Name4, R.id.Name5, R.id.Name6, R.id.Name7, R.id.Name8, R.id.Name9};
String[] arrPeople = new String {"Adams","Boston","Chicago","Denver","Easy","Frank","George","Henry","Ida"}
String sAlternative_Name = "";

for (iCounter = 0; iCounter < 9; iCounter++){
    // iName is a global variable, stars at 0, but it changes upon user request:
    sName = arrPeople[iName];
    // this "if" statement is the problematic piece of code.......
    if (((iCounter == 1)||(iCounter == 3)||(iCounter == 5))&& (sName.equals("Adams")){
        sAlternative_Name = mtAlternative_Name();
        sName = sAlternative_Name;
    } else {
        if ((sAlternative_Name != "") && (sName.equals(sAlternative_Name))){
            sName = "Adams";
        }
    }
    // without the previous "if" the following part works properly, so the list rotates accordingly;
    tvNames = (TextView) getView().findViewById(arrNames_Ids[iCounter]);
    tvNames.setText(sName);
    iName = iName + 1;
    if (iName > 8){
        iName = 0;
    }
}

public String mtAlternative_Name(){
    int iCycle;
    String sName_Aux = "";
    // iCurrent_Cycle is a global variable, assumes values from 0 to 73, and it controls the order in which arrPeople is supposed to be shown:
    iCycle = iCurrent_Cycle % 18;
    switch (iCycle){
        case 1: sName_aux = "George"; break;
        case 3: sName_aux = "Easy"; break;
        case 5: sName_aux = "Chicago"; break;
        case 10: sName_aux = "Henry"; break;
        case 12: sName_aux = "Frank"; break;
        case 14: sName_aux = "Denver"; break;
    }
    return sName_Aux;
}

这个想法是为了防止“亚当斯”占据位置1,3和5。 每当他在那些位置时,其他人(sAlternative_Name)就会占据这个位置,而“亚当斯”则会进入该人应该采取的位置。 数组arrPeople不会改变,例如:

如果“Adams”应该位于第4位,“Easy”将取代该位置,“Adams”将进入第8位。 但是当列表再次旋转(向前移动)时,“Adams”占据位置5而“Easy”占据位置9。

我是Java的新手,也没有太多的编程经验,但我看不出这段代码有什么问题...

我使用%运算符将绿色名称设置为“Adams”位置。正如你所看到的,“波士顿”和“Ida”不在游戏中...但应该是.........这没关系,我会设法稍后再思考,但是现在我希望我能理解为什么我的代码不起作用......

非常感谢...

1 个答案:

答案 0 :(得分:0)

我使用的逻辑是创建了一个包含整个圆桌的二维数组(没有Adams从位置1,3或5移除)。然后,运行另一个嵌套循环,用“Chicago”替换每行中的“Adams”。最后,打印二维阵列。这给你输出。 (我把芝加哥作为替代名称而不是Easy,因为有时,Easy也可以在位置1,3或5处相对于Adam的位置,因此交换仍然会导致异常持续存在。

 String[] arrPeople={"Adams","Boston","Chicago","Denver","Easy","Frank","George","Henry","Ida"};
   String[][] roundtable=new String[9][9];
   int i,j,p;
   for(i=0;i<9;i++){
     p=i;
     for(j=0;j<9;j++){
      roundtable[i][j]=arrPeople[p++];
      if(p==9)
      p=0;
     }
   }

   for(i=0;i<9;i++){
     for(j=0;j<5;j+=2){
       if(roundtable[i][j].equals("Adams")){
        roundtable[i][j+2]="Adams";
        roundtable[i][j]="Chicago";
        break;
       }
     }
   }

   for(i=0;i<9;i++){
   for(j=0;j<9;j++)
   System.out.print(roundtable[i][j]+"\t");
   System.out.println("\n");
   }
相关问题