Java 如何修复Android中的For循环和随机问题?
玩家数据类Java 如何修复Android中的For循环和随机问题?,java,android,for-loop,Java,Android,For Loop,玩家数据类 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 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 ");
单身班
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 ");
我正在制作一个应用程序游戏,它的工作只有老师和学生。
上面的代码显示了我的应用程序
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;
}
}
for(int i=0;i
但我没能解决这些问题
如果有人回答我如何分配作业,并根据作业查看不同的文本视图,我将不胜感激。谢谢我通过以下代码解决了问题:
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();
}
}
}
for(int i=0;i