int age = sc.nextInt();没有输入时,怎么提示重新输入
时间: 2024-05-03 16:18:23 浏览: 24
你可以使用一个循环来实现这个功能,当用户没有输入时,就一直提示重新输入,直到用户输入了一个有效的值为止。具体代码如下:
```
int age = 0;
Scanner sc = new Scanner(System.in);
while(true){
System.out.print("请输入年龄:");
if(sc.hasNextInt()){
age = sc.nextInt();
break;
}else{
System.out.println("请输入有效的数字!");
sc.next();
}
}
System.out.println("你的年龄是:" + age);
```
在这个代码中,我们使用一个 while 循环来不断提示用户输入年龄。当用户输入的是一个整数时,我们就将其赋值给 age 变量,并且使用 break 语句退出循环。如果用户输入的不是一个整数,我们就提示用户重新输入,并且使用 sc.next() 来清空输入缓冲区。
相关问题
public static void add(ArrayList<stu> arr){ stu s=new stu(); Scanner sc=new Scanner(System.in); System.out.println("名字"); String name=sc.nextLine(); System.out.println("年龄"); String age=sc.nextLine(); System.out.println("学号"); String id=sc.nextLine(); System.out.println("地址"); String adddrese=sc.nextLine(); s.setAdresss(adddrese); s.setAge(age); s.setId(id); s.setName(name); arr.add(s); System.out.println("添加成功"); } public static void dele(ArrayList<stu> arr){ Scanner sc=new Scanner(System.in); int i=sc.nextInt(); i+=1; arr.remove(i); System.out.println("删除成功"); } public static void search(ArrayList<stu> arr){ } public static void move(ArrayList<stu> arr){ Scanner sc=new Scanner(System.in); int i=sc.nextInt(); stu s=new stu(); System.out.println("名字"); String name=sc.nextLine(); System.out.println("年龄"); String age=sc.nextLine(); System.out.println("学号"); String id=sc.nextLine(); System.out.println("地址"); String adddrese=sc.nextLine(); s.setAdresss(adddrese); s.setAge(age); s.setId(id); s.setName(name); arr.set(i,s); System.out.println("修改成功"); } public static void show(ArrayList<stu> arr){ System.out.println("学生\t姓名\t年龄\t居住地"); for(int i=0;i<arr.size();i++){ stu s1=arr.get(i); System.out.println(s1.getName()+"\t"+s1.getAge()+"\t"+s1.getId()+"\t"+s1.getAdresss()); } } }这段代码中方法show中的for循环为什么无法输出?
这段代码中方法show中的for循环无法输出的原因是在方法move中,你使用了Scanner类的nextInt()方法来读取整数,然后直接使用nextLine()方法读取字符串。这会导致在读取整数后,按下Enter键时会被nextInt()方法留在缓冲区中,并被作为nextLine()方法的输入。因此,下一个nextLine()方法会直接读取到空字符串,导致无法正确输入学生的姓名。
为了解决这个问题,你可以在读取整数后使用一个额外的nextLine()方法来清空缓冲区中的换行符。在move方法中的代码修改如下:
```java
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
sc.nextLine(); // 添加这一行来清空缓冲区
stu s=new stu();
// 其他代码不变
```
这样就能够正确地读取学生的姓名,并且show方法中的for循环可以输出正确的结果了。
该代码错误在哪?import java.util.ArrayList; import java.util.Arrays; import java.util.Objects; import java.util.Scanner; class PersonOverride{ private String name; private int age; private boolean gender; public PersonOverride(String name, int age, boolean gender) { this.name = name; this.age = age; this.gender = gender; } public PersonOverride() { this("default",1,true); } @Override public String toString() { return name + '-' + age + "-" + gender ; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PersonOverride that = (PersonOverride) o; return age == that.age && gender == that.gender && Objects.equals(name, that.name); } @Override public int hashCode() { return Objects.hash(name, age, gender); } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n1=sc.nextInt(); PersonOverride[] persons1 = new PersonOverride[n1]; int n2 = sc.nextInt(); ArrayList<PersonOverride> p = new ArrayList<>(); for (int i = 0; i < n2; i++) { PersonOverride per = new PersonOverride(sc.next(),sc.nextInt(),sc.nextBoolean()); p.add(per); } for (int i = 0; i < p.size()-1; i++) { if(p.get(i).equals(p.get(i+1))){ p.remove(i); i--; } } PersonOverride[] persons2 = new PersonOverride[p.size()]; for (int i = 0; i < persons2.length; i++) { persons2[i]=p.get(i); } for (int i = 0; i < persons1.length; i++) { System.out.println(persons1[i].toString()); } for (int i = 0; i < persons2.length; i++) { System.out.println(persons2[i].toString()); } System.out.println(persons2.length); System.out.println(Arrays.toString(PersonOverride.class.getConstructors())); } }
该代码错误在于最后一段输出 `persons1` 数组时,该数组并没有进行初始化,因此会出现 `NullPointerException` 异常。具体来说,你在代码中使用了如下语句:
```java
for (int i = 0; i < persons1.length; i++) {
System.out.println(persons1[i].toString());
}
```
这段代码中 `persons1` 数组没有被初始化,因此其中的元素都是 `null`。在执行 `persons1[i].toString()` 时,由于 `persons1[i]` 为 `null`,因此会抛出 `NullPointerException` 异常。
解决该问题的方法有两种:一种是将 `persons1` 数组初始化,例如:
```java
PersonOverride[] persons1 = new PersonOverride[n1];
for (int i = 0; i < n1; i++) {
persons1[i] = new PersonOverride();
}
```
另一种是直接删掉这段输出 `persons1` 数组的代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)