代码之家  ›  专栏  ›  技术社区  ›  Southrop

Java新手,想知道Scanner是否读取语句和字符串

  •  1
  • Southrop  · 技术社区  · 10 年前

    所以,刚开始 Java 今天我在玩 Scanner 在观看了一个关于该男子如何使用它来查找三角形区域的教程之后。也许我做错了什么,但是,我决定搞砸,看看是否可以在其中加入一个“如果”语句。

    import java.util.Scanner;
    public class test {
    
        static Scanner sc = new Scanner(System.in);
        /*
         * in = input from a keyboard or other device.
         * System is a class.
         */
        public static void main(String[] args){
            {
                String password;
                //this is our variable - password
    
                System.out.print("Enter your password:");
                password = sc.next();
                //we have gotten the user to input the words, now to work on displaying a final message
                //using if
    
                if (password.equals(one))
                    System.out.println("You have logged in successfully");
            }
    
        }
    
    }
    

    这是我的代码,如果它看起来很糟糕,我很抱歉-我还在努力学习。

    我的问题是,我希望我的if语句只在他们输入特定密码(“一”)时才通过。然而,我得到了一个错误,因为它表示值“1”不能解析为变量。当我输入数字时,它工作正常。

    我做错了什么?我该如何纠正?

    3 回复  |  直到 10 年前
        1
  •  3
  •   The F    10 年前

    首先,一个类总是大写的,所以 public class test 应该是 public class Test .

    您需要与字符串进行显式比较。像 one 在任何地方都没有定义,您的代码将无法工作。

    替换:

    if (password.equals(one))
    

    具有

    if (password.equals("one"))
    

    你可以走了。

    相反,您也可以在Class的头中声明密码:

    String pw = "one";
    

    然后检查

    if (password.equals(pw))
    

    但请记住这是 从不 这样处理密码是安全的,在 任何 编程语言。密码应该被散列并存储在数据库中。然而,由于您仍在学习,我可能会认为第二种方法(声明要比较的字符串)是“更干净”的方法。

    编辑 -代码如何使用简单变量声明

    import java.util.Scanner;
    public class Test {
    
        String pw = "one";
        String pw2;
    
        /* this is the constructor for your class 
        you can override existing variables or 
        set a variable that is declared in the header of your class
        inside the constructor of your class. It will run whenever you
        create a new object for that class */
    
        public Test(String pw2Param) {
             this.pw2 = pw2Param;
        }
    
        public static void main(String[] args){
            {    
                Scanner sc = new Scanner(System.in);
    
                // instantiate an object that will hold the information you need 
                Test obj = new Test("two");
                System.out.println(obj.pw);
                // "one"
                System.out.println(obj.pw2);
                // "two"
    
                System.out.println("Enter your password:");
                String password = sc.next();
    
                if (password.equals(obj.pw)) {
                    System.out.println("You have typed 'one' and logged in successfully");
                } else if(password.equals(obj.pw2)) {
                    System.out.println("You have typed 'two' and logged in successfully");
                } else {
                    System.out.println("I didn't get that");
                }
            }
        }
    }
    

    我希望这有意义:)

    我也是一个初学者,但我发现使用 javac 在我的控制台中。与我使用过的任何ide相比,它都是无情的,它会向您指出一些一般错误,ide可能会为您原谅/纠正这些错误。

        2
  •  2
  •   SatyaTNV    10 年前

    相反

    if (password.equals(one))//here one is treated as variable and you are not declared at all in your class.
    

    使用

    if (password.equals("one"))//as string, the value should in double qoutes
    

    如果你写得像

       String one="one";
    

    在您的程序中 if 然后陈述

      if (password.equals(one))//correct
    
        3
  •  0
  •   manoj    10 年前
    String one = "one";
    if (password.equals(one))
        ......
    

    这将解决你的问题,正如你现在宣布的那样,这样它就会得到解决。

    正如上面的评论所指出的,这些都是编码中非常小的问题,使用IDE可以在编写代码的同时修复此类编码错误。