我是爱尔兰当地科技公司的第一年学生。我们正如你猜测做的那样,目前我们正在处理扫描仪。我在底部添加了我的代码并且有点困惑。
当我运行该程序时,它工作正常,直到我进入"输入国家"和#34;进入课程"并且无论我将扫描仪更改为f.nextLine还是f.next,这两行都会一起打印。
任何人都可以帮助我解决这个问题,因为我错过了一些东西而且我无法弄清楚它是什么。
非常感谢
马修
public static void main(String[] args)
{
String name; //declaring string name
String town; //declaring string town
double dist; //declaring double distance from AIT the person lives
int age; //declaring their age
double height; //declaring their height
double weight; //declaring their weight
String country; //declaring their country that they are from
String course; //declaring their course that they are taking
int years; //declaring the amount of years they are in the course
String sNum; //declaring there email address string
Scanner f = new Scanner(System.in);
//Scanner s = new Scanner(System.in);
// below is the prompt for all the information and there keyboard input with scanner
System.out.print("Enter your name here & press enter: ");
name = f.nextLine();
System.out.print("Enter the name of the town or city you live in & press enter: ");
town = f.nextLine();
System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
dist = f.nextDouble();
System.out.print("Enter your age & press enter: ");
age = f.nextInt();
System.out.print("Enter your height & press enter: ");
height = f.nextDouble();
System.out.print("Enter your weight & press enter: ");
weight = f.nextDouble();
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.nextLine();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.nextLine();
System.out.print("Enter the number of years the course is & press enter: ");
years = f.nextInt();
System.out.print("Enter your student number & press enter: ");
sNum = f.next();
//all the outputs of the information collected
System.out.println("Your name: "+name+" Town: "+town);
System.out.println(town+" is "+dist+" away from AIT.");
System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
System.out.println("Your studying "+course+" for "+years+" years at AIT.");
System.out.println("Your student number is "+sNum+" & your student email address is "+sNum+"@student.ait.ie");
System.out.println("Please review the information.");
}
答案 0 :(得分:0)
nextDouble()扫描下一个标记并将其解析为double。这就是你面临这个问题的原因。你可以简单地将nextLine()移到nextDouble()之上。它是解决问题的临时解决方案。
尝试以下代码。我刚编辑了你的代码。
public static void main(String[] args)
{
String name; //declaring string name
String town; //declaring string town
double dist; //declaring double distance from AIT the person lives
int age; //declaring their age
double height; //declaring their height
double weight; //declaring their weight
String country; //declaring their country that they are from
String course; //declaring their course that they are taking
int years; //declaring the amount of years they are in the course
String sNum; //declaring there email address string
Scanner f = new Scanner(System.in);
//Scanner s = new Scanner(System.in);
// below is the prompt for all the information and there keyboard input with scanner
System.out.print("Enter your name here & press enter: ");
name = f.nextLine();
System.out.print("Enter the name of the town or city you live in & press enter: ");
town = f.nextLine();
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.nextLine();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.nextLine();
System.out.print("Enter the number of years the course is & press enter: ");
years = f.nextInt();
System.out.print("Enter your student number & press enter: ");
sNum = f.next();
System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
dist = f.nextDouble();
System.out.print("Enter your age & press enter: ");
age = f.nextInt();
System.out.print("Enter your height & press enter: ");
height = f.nextDouble();
System.out.print("Enter your weight & press enter: ");
weight = f.nextDouble();
//all the outputs of the information collected
System.out.println("Your name: "+name+" Town: "+town);
System.out.println(town+" is "+dist+" away from AIT.");
System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
System.out.println("Your studying "+course+" for "+years+" years at AIT.");
System.out.println("Your student number is "+sNum+" & your student email address is "+sNum+"@student.ait.ie");
System.out.println("Please review the information.");
}
}
答案 1 :(得分:0)
如果您使用
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.next();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.next();
它会起作用。您也可以为其他nextLine
执行此操作。出于某种原因,您将学生编号读作字符串而不是数字,但由于您将所有输入视为字符串,因此您只需使用next
即可。
修改强>
不要忘记致电f.close()
关闭扫描仪。