当我使用 nextInt() 它会跳到下一行
when I use nextInt() it skip on the next line
我正在尝试 java 中的基本对象,但我在尝试时遇到了这个问题
我有狗class
public class Dogs {
private String Name;
private int Age;
private String Color;
private String Owner;
public Dogs() {
this.Name = "Rex";
this.Age = 5;
this.Color = "black";
this.Owner = "John";
}
public Dogs(String name, int age, String color, String owner) {
this.Name = name;
this.Age = age;
this.Color = color;
this.Owner = owner;
}
//all the getters and setters
和主要 class
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Dogs my_dog = new Dogs();
System.out.println("What is the dog's name? ");
my_dog.setName(input.next());
System.out.println("What is the dog's age? ");
my_dog.setAge(input.nextInt());
System.out.println("What is the dog's color? ");
my_dog.setColor(input.nextLine());
System.out.println("What is the owner's name? ");
my_dog.setOwner(input.nextLine());
}
}
当我 运行 它时,它可以很好地打印两个问题,但它会跳过下一个...
这是结果:
What is the dog's name?
pil
What is the dog's age?
7
What is the dog's color?
What is the owner's name?
我该如何解决?
使用 input.next() 而不是 input.nextLine() 将解决此问题。
nextLine() 方法 returns 输入狗的年龄后按下 ENTER 按钮时 nextInt() 跳过的行。
public static void main(String... args) {
Scanner input = new Scanner(System.in);
Dogs my_dog = new Dogs();
System.out.println("What is the dog's name? ");
my_dog.setName(input.next());
System.out.println("What is the dog's age? ");
my_dog.setAge(input.nextInt());
System.out.println("What is the dog's color? ");
my_dog.setColor(input.next());
System.out.println("What is the owner's name? ");
my_dog.setOwner(input.next());
}
请在读取年龄即整数值后加上input.nextLine()。
详情refer this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Dogs my_dog = new Dogs();
System.out.println("What is the dog's name? ");
my_dog.setName(input.next());
System.out.println("What is the dog's age? ");
my_dog.setAge(input.nextInt());
// add this line
input.nextLine()
System.out.println("What is the dog's color? ");
my_dog.setColor(input.nextLine());
System.out.println("What is the owner's name? ");
my_dog.setOwner(input.nextLine());
}
}
我正在尝试 java 中的基本对象,但我在尝试时遇到了这个问题
我有狗class
public class Dogs {
private String Name;
private int Age;
private String Color;
private String Owner;
public Dogs() {
this.Name = "Rex";
this.Age = 5;
this.Color = "black";
this.Owner = "John";
}
public Dogs(String name, int age, String color, String owner) {
this.Name = name;
this.Age = age;
this.Color = color;
this.Owner = owner;
}
//all the getters and setters
和主要 class
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Dogs my_dog = new Dogs();
System.out.println("What is the dog's name? ");
my_dog.setName(input.next());
System.out.println("What is the dog's age? ");
my_dog.setAge(input.nextInt());
System.out.println("What is the dog's color? ");
my_dog.setColor(input.nextLine());
System.out.println("What is the owner's name? ");
my_dog.setOwner(input.nextLine());
}
}
当我 运行 它时,它可以很好地打印两个问题,但它会跳过下一个...
这是结果:
What is the dog's name?
pil
What is the dog's age?
7
What is the dog's color?
What is the owner's name?
我该如何解决?
使用 input.next() 而不是 input.nextLine() 将解决此问题。
nextLine() 方法 returns 输入狗的年龄后按下 ENTER 按钮时 nextInt() 跳过的行。
public static void main(String... args) {
Scanner input = new Scanner(System.in);
Dogs my_dog = new Dogs();
System.out.println("What is the dog's name? ");
my_dog.setName(input.next());
System.out.println("What is the dog's age? ");
my_dog.setAge(input.nextInt());
System.out.println("What is the dog's color? ");
my_dog.setColor(input.next());
System.out.println("What is the owner's name? ");
my_dog.setOwner(input.next());
}
请在读取年龄即整数值后加上input.nextLine()。
详情refer this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Dogs my_dog = new Dogs();
System.out.println("What is the dog's name? ");
my_dog.setName(input.next());
System.out.println("What is the dog's age? ");
my_dog.setAge(input.nextInt());
// add this line
input.nextLine()
System.out.println("What is the dog's color? ");
my_dog.setColor(input.nextLine());
System.out.println("What is the owner's name? ");
my_dog.setOwner(input.nextLine());
}
}