我试图编写一个控制器来检查输入,以便从学生那里得到正确的名字。列表,不知何故,即使我给出正确的名称,循环仍然继续。 我确定我错过了一些非常明显的东西。
以下是代码:
sudo su
pip install plyfile
编辑: 问题出在"学生"列出这里的类和列表如何:
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
while (!students.contains(str)){
System.out.println("Try again");
str = sc.nextLine();
}
我想检查此类中的名称数据成员
答案 0 :(得分:2)
您无法在String
列表中搜索Student
。您需要编写自己的contains()
方法,可能是这样的:
public boolean contains(List<Student> list, String s) {
for(Student student : list)
if(student.getName().equals(s)) return true;
return false;
}
然后你可以这样做:
Scanner sc = new Scanner(System.in);
while (!contains(students, sc.nextLine()))
System.out.println("Try again");
这当然是假设你有name
的吸气剂。
另一个选项是让Student
实施Comparable
,以便您可以使用各种Collections
方法,并且可以将Student
个对象相互比较。一个例子是:
public class Student implements Comparable<Student> {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int compareTo(Student s2) {
return name.compareTo(s2.getName());
}
}
然后您可以执行以下操作:
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("Chris"));
students.add(new Student("John"));
students.add(new Student("Frank"));
students.add(new Student("Devon"));
Student me = new Student("Chris");
students.contains(me); // true
由于您现在已经实施了Comparable
,因此您还可以使用Student
按name
对Collections.sort(students)
对象进行排序。
有关在https://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
中使用Comparable
的更多示例
答案 1 :(得分:1)
用户阅读引文:
import java.util.ArrayList;
import java.util.Scanner;
public class Sandbox {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Boolean found = false;
String input;
ArrayList<Student> students = new ArrayList<Student>();
students.add(new Student("Jim"));
students.add(new Student("Kim"));
students.add(new Student("Bill"));
students.add(new Student("Betty"));
System.out.println("Enter the name of a student to check");
input = keyboard.nextLine();
if(students.get(0).getName().equals(input)) {
System.out.println("You found " + students.get(0).getName());
found = true;
keyboard.close();
}
while(!found) {
System.out.println("Try again");
input = keyboard.nextLine();
for(int i = 1; i < students.size(); i++) {
if(students.get(i).getName().equals(input)) {
System.out.println("You found " + students.get(i).getName());
found = true;
keyboard.close();
break;
}
}
}
}
学生班
public class Student {
String name;
public Student () {
}
public Student (String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
答案 2 :(得分:0)
假设有一个Student对象的getter,您可以将.contains()
个对象列表映射到学生姓名列表(String),然后在该列表上执行Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
// Create a List<String> of student names
List<String> names = students
.stream()
.map(student -> student.getName())
.collect(Collectors.toList());
while (!names.getName().contains(str)){
System.out.println("Try again");
str = sc.nextLine();
}
调用。所以试试这个:
\bamount\b.*?(\d+(?:\.\d*)?|\.\d+)
答案 3 :(得分:0)
扫描仪sc =新扫描仪(System.in);
String str = null;
boolean isFound = false;
而(!isFound)
{
str = sc.nextLine();
for(Student stdObj:students)
如果(stdobj.name.equals(STR))
{
isFound = TRUE;
打破;
}
如果(!isFound)
System.out.println(&#34;再试一次&#34;);
}