这是myProgrammingLab任务,我不确定我在哪里出错。输出似乎混淆了变量,我不知道为什么。我已经包含了预期的输出和我的代码输出。
import java.util.Scanner;
public class WordGame
{
public static void main(String[] args)
{
String name;
int age;
String city;
String college;
String profession;
String animal;
String pet;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your name: ");
name = keyboard.nextLine();
System.out.print("Enter your age: ");
age = keyboard.nextDouble();
System.out.print("Enter the name of a city: ");
city = keyboard.nextLine();
System.out.print("Enter the name of a college: ");
college = keyboard.nextLine();
System.out.print("Enter profession: ");
profession = keyboard.nextLine();
System.out.print("Enter a type of animal: ");
animal = keyboard.nextLine();
System.out.print("Enter a pet name: ");
pet = keyboard.nextLine();
System.out.println("There once was a person named " + name + " who lived in " + city + "." + "At the age of\n" + age + ", " + name + " went to college at " + college + ". " + name + " graduated and went to\nwork as a " + profession + ". Then, " + name + " adopted a(n)" + animal + " named " + pet + ". \nThey both lived happily ever after!");
}
}
Expected Output:
Enter·your·name:·Enter·your·age:·Enter·the·name·of·a·city:·Enter·the·name·of·a·college:·Enter·profession:·Enter·a·type·of·animal:·Enter·a·pet·name:·There·once·was·a·person·named·Al·Smith·who·lived·in·Brooklyn.·At·the·age·of↵
43,·Al·Smith·went·to·college·at·School·of·Hard·Knocks.·Al·Smith·graduated·and·went·to·↵
work·as·a·Politician.·Then,·Al·Smith·adopted·a(n)·Pitbull·named·Toodles↵
They·both·lived·happily·ever·after!↵
Actual Output:
Enter·your·name:·Enter·your·age:·Enter·the·name·of·a·city:·Enter·the·name·of·a·college:·Enter·profession:·Enter·a·type·of·animal:·Enter·a·pet·name:·There·once·was·a·person·named·Al·Smith·who·lived·in·.At·the·age·of↵
43,·Al·Smith·went·to·college·at·Brooklyn.·Al·Smith·graduated·and·went·to↵
work·as·a·School·of·Hard·Knocks.·Then,·Al·Smith·adopted·a(n)Politician·named·Pitbull.·↵
They·both·lived·happily·ever·after!↵
答案 0 :(得分:1)
您的代码正在跳过阅读城市字符串。
适合年龄使用
age= Integer.parseInt(keyboard.nextLine());