我在将 String(birthyear) 转换为 int(age) 时遇到了麻烦。我希望有人输入他们的出生年份,并让程序进行简单的减法计算以计算出他们的年龄。我是编程新手,所以我一直在四处寻找,大多数地方都告诉我同样的事情。
Integer.parseInt(birthyear);
但是,这样做之后,当我尝试进行数学运算时...
int age = year-birthyear;
我得到标题中的错误。
public class WordGameScanner
{
public static void main(String[] argus)
{
String name;
String home;
String birthyear;
String course;
String reason;
String feedback;
int year = 2013;
Scanner input = new Scanner(System.in);
System.out.print("What is your name: ");
name = input.nextLine();
System.out.print("Where are you from: ");
home = input.nextLine();
System.out.print("What year were you born: ");
birthyear = input.nextLine();
Integer.parseInt(birthyear);
System.out.print("What are you studying: ");
course = input.nextLine();
System.out.print("Why are you studying " + course + ": ");
reason = input.nextLine();
System.out.print("How is " + course + " coming along so far: ");
feedback = input.nextLine();
int age = year-birthyear;
System.out.println("There once was a person named " + name +
" who came all the way from " + home +
" to study for the " + course +
" degree at --------------.\n\n" + name +
" was born in " + birthyear + " and so will turn " + age +
" this year.");
System.out.println(name + " decided to study the unit ------------, because \"" +
reason + "\". So far, ----------- is turning out to be " +
feedback + ".");
}
}
如果发错地方了,我很抱歉,这只是我在这里的第二篇文章。我只是点击“提问”并按照指示进行操作 >.<