二元运算符“-”的错误操作数类型第一种类型:int;第二种类型:java.lang.String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15212925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:52:29  来源:igfitidea点击:

bad operand types for binary operator "-" first type: int; second type: java.lang.String

javastringoperatorsint

提问by Mav

I'm having troubles converting a String(birthyear) into an int(age). I want someone to type in their year of birth, and have the program do a simple subtraction calculation to work out their age. I'm new to programming, so I've been searching around, and most places tell me the same thing.

我在将 String(birthyear) 转换为 int(age) 时遇到了麻烦。我希望有人输入他们的出生年份,然后让程序做一个简单的减法计算来计算他们的年龄。我是编程新手,所以我一直在四处寻找,大多数地方都告诉我同样的事情。

Integer.parseInt(birthyear);

However, having done that, when I try to do the math...

然而,这样做之后,当我尝试做数学......

int age = year-birthyear;

I get the error in the title.

我收到标题中的错误。

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 + ".");
    }
}

I'm sorry if this is in the wrong place, this is only my second post here. I just clicked "ask a question" and followed the directions >.<

如果这是在错误的地方,我很抱歉,这只是我在这里的第二篇文章。我只是点击了“提问”并按照说明操作 >.<

回答by Mike Samuel

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

Calling parseIntdoesn't redefine the variable String birthYearas an int, it just returns an intvalue that you can store in another variable (like int birthYearInt = Integer.parseInt(birthYear);) or use in an expression as above.

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



You may also want to take a minute to think about inputs.

您可能还需要花一点时间考虑输入。

Your users may enter just the last two digits ("83" instead of "1983"), so you could do:

您的用户可能只输入最后两位数字(“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;

Alternatively, you could use java.text.NumberFormatto properly handle commas in inputs. NumberFormatis a good way to deal with numbers that come from humans since it handles the ways people format numbers differently from computers.

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



Another problem is that this uses the Chinese age numbering system where everyone's age increments by one on the New Year (of the Chinese Lunar calendar, not the Gregorian calendar). This is not they way ages are calculated worldwide though. In the US and most of Europe, for example, your age increments on the anniversary of your birth.

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

回答by BackSlash

Integer.parseInt(birthyear);

does not overwrite birthyear value and does not change it's type from String to int

不会覆盖出生年份值,也不会将其类型从 String 更改为 int

so you have to do

所以你必须做

int intbirthyear = Integer.parseInt(birthyear);

and then

接着

int age = year-intbirthyear;

回答by Ian Roberts

Integer.parseIntdoesn't change birthyearfrom a String to an int, it simply returns the int that the string represents. You need to assign this returned value to another variable and use that in the subtraction.

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

回答by Ravindra Gullapalli

even after executing Integer.parseInt(birthyear);without assigning to anything, birthyearwill be a string.

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

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

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

OR

或者

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