好友
阅读权限20
听众
最后登录1970-1-1
|
刚开始学习,遇到点问题,求助:
代码执行的时候进入while后,没有执行1和2,直接执行了3
第二次循环才执行1和2
这是什么原因啊
.
如果换成String a4 = sc.next();
当密码设置成111 111的时候,next就没办法读取了啊。。。。。求解
package class_test;
import java.util.Scanner;
public class WorkerTest {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Worker s1 = new Worker();
System.out.println("请输入姓名:");
String a1 = sc.nextLine();
s1.setName (a1);
System.out.println("请输入年龄:");
int a2 = sc.nextInt();
s1.setAge(a2);
System.out.println("请输入工资:");
double a3 = sc.nextInt();
s1.setSalary(a3);
System.out.println("请输入访问密码:");
while(true){
String a4 = sc.next();
if ("111111".equals(a4)){
System.out.println(s1.getName());
System.out.println(s1.getAge());
System.out.println(s1.getSalary());
System.out.println("-----打印完成-----");
break;
}else{
System.out.println("密码错误,请重新输入:");
}
}
}
}
class Worker{
private String name;
private int age;
private double salary;
public Worker(){}
public Worker(String name,int age,double salary){
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
}
}
|
-
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|