Программа не даёт возможности считать пользовательский ввод
package out.production.javaworks;
import java.util.Scanner;
public class Person {
String name;
int age;
String gender;
public void printText(String str) {
System.out.println(str);
}
public Person() {
}
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Person project = new Person();
Person people1 = new Person("", 0, "");
System.out.println("Введите Ваше имя:");
people1.name = scanner.nextLine();
System.out.println("Введите Ваш возраст:");
people1.age = scanner.nextInt();
System.out.println("Введите Ваш пол (гендер):");
people1.gender = scanner.nextLine();
System.out.println("Привет, " + people1.name + "!");
}
}
После строки "Введите Ваш пол (гендер):" не успеваю ввести данные и выполняются следующие инструкции (вывод текста) С чем это связано?
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
Это связано с тем, что nextInt не считывает конец строки.
...
System.out.println("Введите Ваш возраст:");
people1.age = scanner.nextInt();
scanner.nextLine();
System.out.println("Введите Ваш пол (гендер):");
people1.gender = scanner.nextLine();
...