PlayerData类
public class PlayerData{
int id; // id means Player's order
boolean isTeacher;
public PlayerData() {
}
public void setID(int id) {
this.id = id;
}
public void setTeacher() {
isTeacher = true;
}
}
单班
public class Singleton {
public int time;
public int total; // number of total players
public int teacher; // number of teachers
public int student; // number of students
public int count = 1;
public String keyWord; //
ArrayList<PlayerData> playerArrayList = new ArrayList<PlayerData>();
// method init creates Objects of players
public void init() {
for (int i = 1; i <= total; i++) {
PlayerData player = new PlayerData();
player.setID(i); // set id
playerArrayList.add(player); // add player Object to ArrayList
}
}
public void JobSelection() {
int a[] = new int[teacher]; // Make array to the number I will select
Random r = new Random(); // Random Object
PlayerData player = new PlayerData();
for (int i = 0; i < teacher; i++) // for loop to select Teachers
{
a[i] = r.nextInt(total) + 1; //select a number from 1~total and Save in a[0]~a[total]
for (int j = 0; j < i; j++) //for loop to avoid overlap
{
if (a[i] == a[j]) {
i--;
}
}
if (a[i] == player.id) {
playerArrayList.get(player.id - 1).isTeacher = true;
}
}
}
角色选择类
public class RoleSelection extends AppCompatActivity {
Singleton s1 = Singleton.getInstance();
PlayerData player = new PlayerData();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_role_selection07_03);
TextView t1 = (TextView) findViewById(R.id.textJobConfirm);
if (s1.playerArrayList.get(s1.player.id-1).isTeacher == true) {
t1.setText(" You job is Teacher ");
} else {
t1.setText(" Your job is Student ");
我正在制作一款应用程序游戏,其工作仅包括教师和学生。 上面的代码显示了我的应用。
我的logcat或应用程序中没有任何错误。 但是,我只收到“您的工作是学生”。我猜JobSelection方法效果不好。 所以我试图修复这些代码
for (int i = 0; i < teacher; i++) // for loop to select Teachers
{
a[i] = r.nextInt(total) + 1; //select a number from 1~total and Save in a[0]~a[total]
for (int j = 0; j < i; j++) //for loop to avoid overlap
{
if (a[i] == a[j]) {
i--;
}
}
if (a[i] == player.id) {
playerArrayList.get(player.id - 1).isTeacher = true;
}
}
但是我无法解决这些问题...
如果有人回答我如何分配工作并根据他们的工作看到不同的TextView,我将不胜感激。谢谢
答案 0 :(得分:0)
我通过以下代码解决了这个问题:
for (int i = 0; i < teacher; i++)
{
a[i] = r.nextInt(total) + 1;
for (int j = 0; j < i; j++)
{
if (a[i] == a[j]) {
i--;
}
}
if (playerArrayList.get(a[i] - 1).id == a[i]) {
playerArrayList.get(a[i] - 1).setTeacher();
}
}
}