Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确退出此while循环_Java_Loops - Fatal编程技术网

Java 如何正确退出此while循环

Java 如何正确退出此while循环,java,loops,Java,Loops,我想打印是否找到rollno但它只在找到rollno时打印,我应该如何存在循环并获取正确的布尔值来打印找到的值和未找到的值。有人能告诉我我做错了什么吗?现在,它只在找到值时打印,不打印;如果arraylist中不存在值,则存在 public class StudentDB{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); //Creating

我想打印是否找到
rollno
但它只在找到
rollno
时打印,我应该如何存在循环并获取正确的布尔值来打印找到的值和未找到的值。有人能告诉我我做错了什么吗?现在,它只在找到值时打印,不打印;如果arraylist中不存在值,则存在

public class StudentDB{

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //Creating user defined class objects  
        Student s1=new Student(1,"AAA",13);  
        Student s2=new Student(2,"BBB",14);  
        Student s3=new Student(3,"CCC",15); 

        ArrayList<Student> al=new ArrayList<Student>();
        al.add(s1);
        al.add(s2);  
        al.add(s3);  

        Iterator itr=al.iterator();  

        //traverse elements of ArrayList object  
       /* while(itr.hasNext()){  
            Student st=(Student)itr.next();  
            if(st.rollno == 2){
            System.out.println(st.rollno+" "+st.name+" "+st.age);  
            }
            else{
                continue;
            }
        }  */
        //Scanner scan = new Scanner(System.in);
        System.out.println("ENter your id: ");
        int id = scan.nextInt();
        
        
        boolean result = false;
        while(!result) {
            while(itr.hasNext()) {  
               Student st=(Student)itr.next();  
               if(st.rollno == id){
               result = true;
               break;
               }
               else{
                   result = false;
               } 
        }       
         
    }
    if(result == true){
      System.out.println("Roll no found!");
      }else{
      System.out.println("Roll no not found!");
      }
      }
}
class Student{  
    int rollno;  
    String name;  
    int age;  
    Student(int rollno,String name,int age){  
        this.rollno=rollno;  
        this.name=name;  
        this.age=age;  
    }  
}
公共班级学生数据库{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
//创建用户定义的类对象
学生s1=新生(1,“AAA”,13);
学生s2=新生(2,“BBB”,14);
学生s3=新生(3,“CCC”,15);
ArrayList al=新的ArrayList();
al.添加(s1);
al.添加(s2);
al.add(s3);
迭代器itr=al.Iterator();
//遍历ArrayList对象的元素
/*而(itr.hasNext()){
学生st=(学生)itr.next();
如果(st.rollno==2){
System.out.println(st.rollno+“”+st.name+“”+st.age);
}
否则{
继续;
}
}  */
//扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入您的id:”);
int id=scan.nextInt();
布尔结果=假;
而(!结果){
而(itr.hasNext()){
学生st=(学生)itr.next();
if(st.rollno==id){
结果=真;
打破
}
否则{
结果=假;
} 
}       
}
如果(结果==真){
System.out.println(“未找到卷!”);
}否则{
System.out.println(“未找到卷号!”);
}
}
}
班级学生{
int-rollno;
字符串名;
智力年龄;
学生(int-rollno,字符串名,int-age){
this.rollno=rollno;
this.name=name;
这个。年龄=年龄;
}  
}

如果找不到值,它将进入无限循环,这就是值无法打印的原因。不需要外部while循环。

while(!result)
如果
result
从未设置为true,则不会结束。不需要外部while循环。