找到你要的答案

Q:How to read and compare strings in Java? [closed]

Q:如何阅读和比较字符串在java?[关闭]

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()

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.

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()

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);
    }
}

这里的想法是问用户的形状的面积将计算,如果它等于一个不同的形状,我要命名,然后将使用公式取决于形状。

answer1: 回答1:

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

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

替换此线:字符串的形象=扫描。nextline();字符串的形象=扫描。next();和你的程序将工作:见下图

也在这里看到之间的差异和扫描仪。next() nextline()扫描仪。

answer2: 回答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基地Y阿尔图拉德尔TRIANGULO ..然后输入方面的价值和它的工作。我认为代码是正确的。

answer3: 回答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.

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

I hope this will answer your question.

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.

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

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

java  string  compare  statements