第四天 break和continue

作者在 2017-07-11 00:10:54 发布以下内容
package example;//break coninue
//主题 : 简易保险柜密码系统
import java.util.Scanner;
public class continue_break {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(true){ //while(1)会出错 reason:java中强制要求while()里面的值为boolean类型的 而c/c++没有此要求
System.out.println(";please input the password:");
int password=s.nextInt();
if(password!=123456){
continue;
}else{ 
break;
}
}// TODO Auto-generated method stub
System.out.println("the password is right");
}


}//continue : 跳过这次循环中剩余的语句,继续下一次循环
//break : 结束循环

java 笔记 | 阅读 576 次
文章评论,共1条
无fuck可说
2017-08-09 22:55
1
while(true){
            System.out.println("请输入密码:");
            String str = s.next();
            if(str=="123456")
                break;
            else 
                continue;
        }System.out.println("your card is right!");
        错的 因为:== 只能比较内存地址,比较字符串应该用equals语法 a.equals(b)
    if(str.equals("123456"))
游客请输入验证码
文章归档