如何在Java中读取和比较字符串?

时间:2022-09-06 13:17:43

I have been trying to compare a string to another, or a string to a statement, but I can't find where the mistake is. I tried with the == but it didnt work, then i changed to .equals()

我一直在尝试将字符串与另一个字符串进行比较,或者将字符串与语句进行比较,但我无法找到错误的位置。我尝试了==但它没有工作,然后我改为.equals()

static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
  System.out.print("En este programa calcularemos el area de una figura.\n" + 
              "Ingresa a continuacion que figura calcularemos.");
    String triangulo = "triangulo";
    String figura = scan.nextLine();
    if (figura.equals(triangulo)){
        System.out.print("Ingrese base y altura del triangulo.\n");
        double base = scan.nextDouble();
        double altura = scan.nextDouble();
        System.out.print("La altura es " + (base*altura)/2);
    }
}

The idea here is to ask the user the name of the shape whose area will be calculated, and if it equals one of the different shapes I'm going to name, then it will use formula depending on the shape.

这里的想法是询问用户将计算其面积的形状的名称,如果它等于我要命名的不同形状之一,那么它将根据形状使用公式。

3 个解决方案

#1


Replace this line: String figura = scan.nextLine();with String figura = scan.next();and your program will work: see image below

替换此行:String figura = scan.nextLine(); with String figura = scan.next();您的程序将起作用:请参见下图

如何在Java中读取和比较字符串?

Also see here the difference between Scanner.next() and Scanner.nextLine()

另请参见Scanner.next()和Scanner.nextLine()之间的区别

#2


When I tried this program and entered triangulo as input it seems to work for me as it printed Ingrese base y altura del triangulo..Then enter the value for sides and it works. I think code is correct.

当我尝试这个程序并输入triangulo作为输入时,它似乎对我有用,因为它打印了Ingrese base y altura del triangulo ..然后输入两侧的值并且它有效。我认为代码是正确的。

#3


You have to differentiate between compering the references and values. Using "==" means you are comparing references not values. The link here will describes what are the java methods to compares variables in general and in which cases you have to use them.

您必须区分参考和值的比较。使用“==”表示您正在比较引用而不是值。这里的链接将描述一般比较变量的java方法,以及必须使用它们的情况。

http://www.leepoint.net/data/expressions/22compareobjects.html

I hope this will answer your question.

我希望这能回答你的问题。

#1


Replace this line: String figura = scan.nextLine();with String figura = scan.next();and your program will work: see image below

替换此行:String figura = scan.nextLine(); with String figura = scan.next();您的程序将起作用:请参见下图

如何在Java中读取和比较字符串?

Also see here the difference between Scanner.next() and Scanner.nextLine()

另请参见Scanner.next()和Scanner.nextLine()之间的区别

#2


When I tried this program and entered triangulo as input it seems to work for me as it printed Ingrese base y altura del triangulo..Then enter the value for sides and it works. I think code is correct.

当我尝试这个程序并输入triangulo作为输入时,它似乎对我有用,因为它打印了Ingrese base y altura del triangulo ..然后输入两侧的值并且它有效。我认为代码是正确的。

#3


You have to differentiate between compering the references and values. Using "==" means you are comparing references not values. The link here will describes what are the java methods to compares variables in general and in which cases you have to use them.

您必须区分参考和值的比较。使用“==”表示您正在比较引用而不是值。这里的链接将描述一般比较变量的java方法,以及必须使用它们的情况。

http://www.leepoint.net/data/expressions/22compareobjects.html

I hope this will answer your question.

我希望这能回答你的问题。