while死循環(huán)能改成for循環(huán)么
public void testModify() {
//提示輸入要修改的學(xué)生Id
System.out.println("請輸入要修改的學(xué)生Id");
Scanner input =new Scanner(System.in );
while(true) {
//取得從鍵盤上輸入的學(xué)生Id
String stuId=input.next();
//從students中查找該學(xué)生Id對象的學(xué)生對象
Student student=students.get(stuId);
if(student==null) {
System.out.println("該Id不存在!請重新輸入!");
}else {
//提示當(dāng)前對應(yīng)的學(xué)生對象的姓名
System.out.println("當(dāng)前該學(xué)生的Id,所對應(yīng)的學(xué)生為:"+student.name);
//提示輸入新的學(xué)生姓名,來修改已有的映射
System.out.println("請輸入新的學(xué)生姓名:");
String name =input.next();
Student newStudent=new Student(stuId,name);
students.put(stuId, newStudent);
System.out.println("修改成功");
break;
}
}
}
2018-08-21
emm樓上不要誤人子弟,任何循環(huán)都可以改為for循環(huán)。。。。for(;ture;)即可
2018-08-17
不能,因?yàn)椴恢滥銜斿e多少次ID,所以只能使用while達(dá)成一個無限的循環(huán)
for需要判斷次數(shù),不符合需求,并非最優(yōu)選項(xiàng)
2018-08-15
樓上在說什么?完全可以改為 for(;;) ,因?yàn)閮煞N條件都有跳出條件,這種情況下和 while(true) 完全等價。
2018-08-15
for循環(huán)是確定循環(huán)。理解一下
2018-08-14
while的作用就是在輸入的id在集合中沒有找到的時候,可以再次輸入id,如果改成for的話,就有個上限,如果有最多輸入n次id的條件的話,那么這是可以用for也是一種辦法。