【问题标题】:Newbie to Java, wondering about if statements and strings that are read by ScannerJava 新手,想知道 Scanner 读取的 if 语句和字符串
【发布时间】:2015-10-13 23:41:46
【问题描述】:

所以,今天刚从Java 开始,在观看了关于这个人如何使用它来找到三角形面积的教程之后,我正在玩Scanner 对象。也许我做错了什么,但是,我决定搞砸,看看我是否可以在其中加入“if”语句。

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 语句只有在他们输入特定密码(“one”)时才能通过。但是,我收到一个错误,因为它说值“一”无法解析为变量。当我输入数字时,它工作正常。

我做错了什么,我该如何解决?

【问题讨论】:

  • 使用带有语法校正功能的 IDE 是学习一门新编程语言的好方法,它们通常会很快告诉你新手错误是有原因的。 Netbeans 和 Eclipse 是最受欢迎的,总有一天看看吧。
  • 我正在使用 Eclipse,这就是我什至知道在哪里找到错误的原因!不过,是否有任何其他设置可以为 Eclipse 打开以提供更多帮助?
  • Eclipse 做了很多很酷的东西,其中大部分你不会注意到或从未使用过。我自己还在学习。除了阅读文档之外,我不确定“学习​​”Eclipse 有多么容易。
  • 我已经更新了我的答案,希望它能让您了解为什么您发布的代码不能按您预期的方式工作。

标签: java java.util.scanner conditional-statements string-comparison


【解决方案1】:

首先,类总是大写的,所以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 编译我的java 代码是一个好习惯。与我使用的任何 ide 相比,它是无情的,并且会指出一些一般性错误,ide 可能会原谅/纠正你。

【讨论】:

  • 是的!我只是在玩,哈哈。永远不会以这种方式对待密码 - 永远不会。只是一些可以玩的东西,非常感谢您的帮助!另外,对于您的第二个选项,整个声明字符串在我的标题中是什么,我是否必须向它添加静态?因为,我只用你的代码试过了,但它没有用,但添加静态是有效的,所以,我只是想知道。
  • 对不起,我没有检查你是否在实例化你的类的对象。因为你不是 pw 变量如果没有设置为静态就不会实例化,所以你是对的。
  • 我可以理解你的代码要去哪里 - 但是,唉,我对构造函数和实例化的知识缺乏让我失去了。但是,我会保存这段代码,等我对 Java 有更深入的了解后再回来看它!再次感谢您的额外帮助,向我展示了它会变成什么样子。
  • 别害怕,Java 非常适合理解面向对象编程的概念。如果您查看网上许多教程中的一些,您将有一个陡峭的学习曲线
【解决方案2】:

改为

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】:
    String one = "one";
    if (password.equals(one))
        ......
    

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

    这些都是编码中非常小的问题,正如上面的 cmets 所建议的,使用 IDE 可以在编写代码的同时修复这些编码错误。

    【讨论】:

      猜你喜欢
      • 2010-11-30
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多