我有麻烦转换字符串(birthyear)成int(年龄)。我希望有人输入他们的出生年份,并让程序做一个简单的减法计算来计算他们的年龄。我是编程新手,所以我一直在寻找,大多数地方告诉我同样的事情。二元运算符的不良操作数类型“ - ”第一种类型:int;第二种类型:java.lang.String
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 + ".");
}
}
对不起,如果这是在错误的地方,这只是我的第二篇文章在这里。我只是点击“问一个问题”,然后按照指示>。 <
你只是丢弃()''用整数#parseInt函数的返回值。不要这样做。 – 2013-03-04 23:13:09
快速问题:您是如何将代码发布到所有缩进问题的?当我尝试使用“代码块”时,它完全没有缩进地发布它。 – Mav 2013-03-04 23:16:47
只需按下“Tab”,我就可以使用[Chrome扩展让我缩进代码块](https://chrome.google.com/webstore/detail/textarea-code-formatter/dgkbnngeehclnibbdblaimapibmmcdal),但您应该没有问题通过粘贴代码获得相同的结果,在编辑器中突出显示它,然后按下“代码”按钮。 – 2013-03-04 23:42:24