gpt4 book ai didi

java - 查明 for 循环中的数据是否满足特定条件

转载 作者:行者123 更新时间:2023-12-02 05:16:52 25 4
gpt4 key购买 nike

我想做的是弄清楚如何查看 for 循环中的数据是否满足特定条件。我想使用 if 语句来执行此操作(除非有更好的方法),但我遇到的麻烦是数据位于 for 循环内,并且代码不希望我使用该循环之外的值环形。这是代码:

    //Create an array called schoolEnrollment of length numberOfStudents         
Student[] myArray = new Student[numberOfStudents];


//Create enough objects of type Student to fill the array above
for (int i = 0; i < numberOfStudents; i++) {

//Uses the instance of RandomNames to assign first and last names to each student
//First names
String[] firstNames1 = {"John", "Roxanne", "Dean", "Mary", "Shawn", "Sara",
"Peter", "Susie", "Jerry", "Juliet"};

int idx = new Random().nextInt(firstNames1.length);
String random = (firstNames1[idx]);

String NextFirstName = random;

//Last names
String[] lastNames1 = {"Smith", "Brown", "Baker", "Doe", "Poe", "Allen",
"Black", "Frederick", "Hunter", "Spencer"};

int idx1 = new Random().nextInt(lastNames1.length);
String random1 = (lastNames1[idx1]);

String NextLastName = random1;


//Randomly assign house locations to each student
int houseLocation = (int) (0 + (Math.random() * (3 - 0)));
//Randomly assign distance from school to each student
int distanceFromSchool = (int) (0 + (Math.random() * (30 - 0)));;
myArray[i] = new Student(NextFirstName, NextLastName, houseLocation, distanceFromSchool);

}

for (int i = 0; i < numberOfStudents; i++) {
// display toString for each object
System.out.println(myArray[i].toString());
}

//Create 4 other arrays of length numberOfStudents of type Student with the names
//ridersOfNorthBus, ridersOfWestBus, ridersOfSouthBus, and ridersOfEastBus
Student[] ridersOfNorthBus = new Student[numberOfStudents];

Student[] ridersOfWestBus = new Student[numberOfStudents];

Student[] ridersOfSouthBus = new Student[numberOfStudents];

Student[] ridersOfEastBus = new Student[numberOfStudents];

所以,我想做的是获取 for 循环随机给出的 houseLocation(东、南、北或西)来看看谁会乘坐哪辆公共(public)汽车。简单地尝试使用 houseLocation 变量是行不通的,因为程序无法识别 for 循环之外的变量。此外,创建该变量的另一个随机实例是行不通的,因为它可能不会得出相同的值。我也无法将代码放入 for 循环中,因为这样我的数组就会多次打印出来,这不是我想要的。有什么办法可以做到这一点吗?谢谢。

另外,不确定是否需要它,但这是包含 houseLocation 方法的类:

public class Student {

//Create private instance variables with setters and getters
private String firstName;
private String lastName;
private int houseLocation = 0;
private int distanceFromSchool = 0;


//Set values for private instance variable house location
public void north() {
int North = 0;
if (houseLocation == North)
North = houseLocation;

}
public void west() {
int West = 1;
if (houseLocation == West)
West = houseLocation;
}
public void south() {
int South = 2;
if (houseLocation == South)
South = houseLocation;
}
public void east() {
int East = 3;
if (houseLocation == East)
East = houseLocation;
}

最佳答案

您不应该使用 houseLocation 变量进行比较,因为它的范围仅在 for 循环内。相反,您应该为 Student 类提供一个 getter 方法来获取特定实例的 houseLocation。例如。

myArray[i].getHouseLocation(); //something like this to access the houseLocation for each element (Student) of the array...

您已经在构造函数中为每个学生设置了房屋位置,只需创建或(使用您已有的 getter)来访问数据。

Student类中的Getter方法:

public int getHouseLocation()
{
return this.houseLocation;
}

关于java - 查明 for 循环中的数据是否满足特定条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26851697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com