简体   繁体   English

当 charAt 和 String.valueOf 的值相同时,为什么它们的比较不返回 true?

[英]Why isn't the comparison of charAt and String.valueOf returning true when they're the same value?

Here's my program:这是我的程序:

import java.util.Scanner;
public class Lottery
{
public static void main(String[] args)
{
    //declare and initialized variables and objects
    Scanner input = new Scanner(System.in);

    String lotteryNum = "";
    String userGuess = "";

    //Generate a 3-digit "lottery" number composed of random numbers
    int num1 = (int)(Math.random()*10);
    int num2 = (int)(Math.random()*10);
    int num3 = (int)(Math.random()*10);
    //Simulate a lottery by drawing one number at a time and 
    //concatenating it to the string
    String c1 = String.valueOf(num1);
    String c2 = String.valueOf(num2);
    String c3 = String.valueOf(num3);
    lotteryNum = (c1+c2+c3);
    //Identify the repeated steps and use a for loop structure
    //Input: Ask user to guess 3 digit number
    System.out.println("WINNER:"+lotteryNum+" Please enter your three numbers (e.g. 123): ");
    userGuess = input.next();
    System.out.println(c1.equals(userGuess.charAt(0)));
    System.out.println(userGuess.charAt(0) +"   " + userGuess.charAt(1) + "     " + userGuess.charAt(2) + "\n" + c1 + "\t"+c2+"\t"+c3);;
    if (c1.equals(userGuess.charAt(0)) && c2.equals(userGuess.charAt(1)))
        System.out.println("Winner: "+ lotteryNum+"\nCongratulations, the front pair matched.");
    else if (c2.equals(userGuess.charAt(1)) && c3.equals(userGuess.charAt(2)))
        System.out.println("Winner: "+ lotteryNum+"\nCongratulations, the end pair matched.");
    else if (c1.equals(userGuess.charAt(0)) && c2.equals(userGuess.charAt(1)) && c3.equals(userGuess.charAt(2)))
        System.out.println("Winner: "+ lotteryNum+"\nCongratulations, both pairs matched.");
    else 
                System.out.println("Winner: "+ lotteryNum+"\nSorry, no matches. You only had \n one chance out of 100 to win anyay.");

} //end main
}//end class Lottery

For some reason, the lines with logic such as:出于某种原因,带有逻辑的行,例如:

System.out.println(c1.equals(userGuess.charAt(0)));

Specifically return false , even if the user enters the same value, ie String.valueOf(num1) is the same value as userGuess.charAt(0) , and I even considered both of them as strings, but it still returns false.具体返回false ,即使用户输入相同的值,即String.valueOf(num1)userGuess.charAt(0)值相同,我什至将它们都视为字符串,但它仍然返回 false。 Why is that?这是为什么?

Because a String is not a char .因为String不是char

if (c1.equals(String.valueOf(userGuess.charAt(0))) 
        && c2.equals(String.valueOf(userGuess.charAt(1))))

or the shorter或者更短的

if (c1.charAt(0) == userGuess.charAt(0) && c2.charAt(0) == userGuess.charAt(1))

userGuess.charAt(0) returns char whereas userGuess.charAt(0)返回char

c1 is String

can use String.valueOf as suggested.可以按照建议使用String.valueOf

You can also use , Character.toString(char);您也可以使用 , Character.toString(char);

System.out.println(c1.equals(Character.toString(userGuess.charAt(0))));

We need to convert either String to char or char to String and then compare我们需要将String to char转换String to charchar to String转换为String to char然后比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM