2013-03-04 122 views
0

我有麻烦转换字符串(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 + "."); 
    } 
} 

对不起,如果这是在错误的地方,这只是我的第二篇文章在这里。我只是点击“问一个问题”,然后按照指示>。 <

+1

你只是丢弃()''用整数#parseInt函数的返回值。不要这样做。 – 2013-03-04 23:13:09

+0

快速问题:您是如何将代码发布到所有缩进问题的?当我尝试使用“代码块”时,它完全没有缩进地发布它。 – Mav 2013-03-04 23:16:47

+0

只需按下“Tab”,我就可以使用[Chrome扩展让我缩进代码块](https://chrome.google.com/webstore/detail/textarea-code-formatter/dgkbnngeehclnibbdblaimapibmmcdal),但您应该没有问题通过粘贴代码获得相同的结果,在编辑器中突出显示它,然后按下“代码”按钮。 – 2013-03-04 23:42:24

回答

7
int age = year-Integer.parseInt(birthyear); 

调用parseInt不重新定义变量String birthYear作为int,它只是返回一个int值,可以在另一变量中存储(如int birthYearInt = Integer.parseInt(birthYear);)或通过表达式如上述使用。


您可能还想花一点时间思考输入。

您的用户可能只输入最后两位数字(“83”,而不是“1983”),那么你可以这样做:

int birthYearInt = Integer.parseInt(birthYear); 
if (birthYear.length() == 2) { 
    // One way to adjust 2-digit year to 1900. 
    // Problem: There might not be more users born in 1900 than born in 2000. 
    birthYearInt = birthYearInt + 1900; 
} 
int age = year = birthYearInt; 

或者,你可以使用java.text.NumberFormat妥善处理逗号输入。 NumberFormat是处理来自人类的数字的好方法,因为它处理人们将数字格式与计算机格式不同的方式。


另一个问题是,这里使用了中国时代的编号系统,其中在新年每个人的年龄增加一(中国农历的,而不是公历)。这不是他们年龄的计算方式,尽管世界各地。例如,在美国和欧洲的大部分地区,您的年龄在您出生的周年纪念日都会增加。

2
Integer.parseInt(birthyear); 

不会覆盖birthyear值,不它的类型从字符串更改为int

,所以你要做的

int intbirthyear = Integer.parseInt(birthyear); 

然后

int age = year-intbirthyear; 
+0

啊!我忘了给它一个变量! – Mav 2013-03-04 23:15:17

0

Integer.parseInt不会改变birthyear从一个String到一个int,它只是返回该字符串表示的int。您需要将此返回的值分配给另一个变量并在减法中使用。

0

即使在执行Integer.parseInt(birthyear);后没有分配任何东西, birthyear将是一个字符串。

要么使用 int age = year-Integer.parseInt(birthyear);

OR

int ibirthyear = Integer.parseInt(birthyear); 
int age = year - ibirthyear; 
相关问题