Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java新手,想知道扫描程序读取的if语句和字符串_Java_Java.util.scanner_Conditional Statements_String Comparison - Fatal编程技术网

Java新手,想知道扫描程序读取的if语句和字符串

Java新手,想知道扫描程序读取的if语句和字符串,java,java.util.scanner,conditional-statements,string-comparison,Java,Java.util.scanner,Conditional Statements,String Comparison,所以,今天我刚从Java开始,在看了一个关于这个家伙如何用它来找到三角形区域的教程之后,我正在玩Scanner对象。也许我做错了什么,但是,我决定胡闹一下,看看是否可以在其中加入一个“如果”语句 import java.util.Scanner; public class test { static Scanner sc = new Scanner(System.in); /* * in = input from a keyboard or other device.

所以,今天我刚从
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语句只有在它们输入特定密码(“一”)时才能通过。但是,我得到了一个错误,因为它表示值“one”不能解析为变量。当我输入数字时,它工作正常

我做错了什么?我如何修复它?

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

如果你写得像

   String one="one";
if
语句之前的程序中,然后

  if (password.equals(one))//correct
反而

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

如果你写得像

   String one="one";
if
语句之前的程序中,然后

  if (password.equals(one))//correct

首先,类总是大写的,所以
公共类测试
应该是
公共类测试

您需要明确地与字符串进行比较。由于
one
没有定义,您的代码将无法工作

替换:

if (password.equals(one))

你可以走了

相反,您也可以在类的标题中声明密码:

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可以原谅/纠正的一般错误。

首先,类总是大写的,所以
公共类测试
应该是
公共类测试

String one = "one";
if (password.equals(one))
    ......
您需要明确地与字符串进行比较。由于
one
没有定义,您的代码将无法工作

替换:

if (password.equals(one))

你可以走了

相反,您也可以在类的标题中声明密码:

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可以原谅/纠正的一般错误

String one = "one";
if (password.equals(one))
    ......
这将解决您的问题,因为您正在声明一个,所以它将得到解决

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

这将解决您的问题,因为您正在声明一个,所以它将得到解决


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

使用带语法更正的IDE是学习新编程语言的好方法,它们通常会快速告诉您新手错误的原因。Netbeans和Eclipse是最受欢迎的工具,有一天可以查看它们。我正在使用Eclipse,这就是为什么我知道在哪里可以找到错误!不过,Eclipse是否还有其他更有用的设置可以打开呢?Eclipse做了很多很酷的事情,其中大多数你都不会注意到,也不会使用。我自己还在学。除了阅读文档之外,我不确定“学习”Eclipse有多容易。我已经更新了我的答案,希望它能让您了解为什么您发布的代码不能按预期的方式工作。使用带有语法更正的IDE是学习新编程语言的一个好方法,通常他们会很快告诉你新手的错误,并说明原因。Netbeans和Eclipse是最受欢迎的工具,有一天可以查看它们。我正在使用Eclipse,这就是为什么我知道在哪里可以找到错误!不过,Eclipse是否还有其他更有用的设置可以打开呢?Eclipse做了很多很酷的事情,其中大多数你都不会注意到,也不会使用。我自己还在学。除了阅读文档之外,我不确定“学习”Eclipse有多容易。我已经更新了我的答案,希望它能让您了解为什么您发布的代码不能按预期的方式工作。是的!我只是随便玩玩,哈哈。永远不会这样对待密码-永远不会。只是一件可以玩玩的东西,非常感谢你这么久以来的帮助!另外,对于第二个选项,它声明了我的头中的字符串是什么,我是否必须向它添加static?因为,我只是用你的代码尝试了一下,但它不起作用,但向它添加静态代码起作用了,所以,我只是想知道。对不起,我没有检查你是否正在实例化类的对象。因为你不是,如果pw变量不是静态的,它就不会实例化,所以你是对的。我可以理解你的代码将要去哪里——但是,唉,我对构造函数和实例化的知识缺乏,这让我不知所措。但是,我会保存这段代码,当我进一步使用Java时再回来!再次感谢你的帮助,让我知道这是什么