Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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 使用Scanner对象的代码中的逻辑存在问题_Java - Fatal编程技术网

Java 使用Scanner对象的代码中的逻辑存在问题

Java 使用Scanner对象的代码中的逻辑存在问题,java,Java,我对这里一些非常基本的东西感到沮丧。。扫描器只是没有做我想做的,我不知道为什么,如果有人能帮助我,我将非常感激 所以我正在尝试编写一个基本的清单,我想要一个扫描仪来检查是否输入了一个有效的生成器名称。字符串变量存储用户输入的单词,如果输入了无效的生成器名称,我只希望该字符串变量等于“ANY”。发生的事情是-字符串的输出正确(大写字母,如我所愿)以及“Any”,这是不应该的!,当输入无效内容时,不会打印任何内容(并且应打印“任何”) 我做了一个简单的SSCCE: import java.util.

我对这里一些非常基本的东西感到沮丧。。扫描器只是没有做我想做的,我不知道为什么,如果有人能帮助我,我将非常感激

所以我正在尝试编写一个基本的清单,我想要一个扫描仪来检查是否输入了一个有效的生成器名称。字符串变量存储用户输入的单词,如果输入了无效的生成器名称,我只希望该字符串变量等于“ANY”。发生的事情是-字符串的输出正确(大写字母,如我所愿)以及“Any”,这是不应该的!,当输入无效内容时,不会打印任何内容(并且应打印“任何”)

我做了一个简单的SSCCE:

import java.util.Scanner;

public class SSCCE {

    public static void main(String[] args)
    {
        System.out.println("Enter a builder name: ");
        Scanner scan = new Scanner(System.in);
        //scan.useDelimiter("\\z"); // count a blank entry (end of input)

        String entry_1 = scan.next();


        if (entry_1.equalsIgnoreCase("FENDER")
                        || entry_1.equalsIgnoreCase("MARTIN")
                        || entry_1.equalsIgnoreCase("GIBSON")
                        || entry_1.equalsIgnoreCase("COLLINGS")
                        || entry_1.equalsIgnoreCase("OLSON")
                        || entry_1.equalsIgnoreCase("RYAN") 
                        || entry_1.equalsIgnoreCase("PRS")) 
        {
            entry_1 = entry_1.toUpperCase();

            System.out.println(entry_1);
        }


    //  if (entry_1.equals(entry_1.toLowerCase())
    //          || entry_1.equalsIgnoreCase(entry_1)
    //          && (entry_1.equalsIgnoreCase("FENDER")
    //                  || entry_1.equalsIgnoreCase("MARTIN")
    //                  || entry_1.equalsIgnoreCase("GIBSON")
    //                  || entry_1.equalsIgnoreCase("COLLINGS")
    //                  || entry_1.equalsIgnoreCase("OLSON")
    //                  || entry_1.equalsIgnoreCase("RYAN") || entry_1
    //                      .equalsIgnoreCase("PRS"))) 
    //  {
    //      entry_1 = entry_1.toUpperCase();
    //  }

        if (!entry_1.equalsIgnoreCase("FENDER")
                || !entry_1.equalsIgnoreCase("MARTIN")
                || !entry_1.equalsIgnoreCase("GIBSON")
                || !entry_1.equalsIgnoreCase("COLLINGS")
                || !entry_1.equalsIgnoreCase("OLSON")
                || !entry_1.equalsIgnoreCase("RYAN")
                || !entry_1.equalsIgnoreCase("PRS")) {

            entry_1 = "ANY";

            System.out.println(entry_1);
        }   
    }

}
编辑: 感谢所有的回复,现在我只是对scan.useDelimiter(“\z”);
当没有输入任何内容时,它应该说“ANY”,它确实是这样做的,但现在它将始终说“ANY”,即使是正确的输入

您正在检查的第二个if语句!条目_1等应使用
&
而不是
|


对于
,“挡泥板”
将返回
true
!条目1.同等信号案例(“马丁”)
等等,在一个比较列表中,只有一个比较必须是
true
,才能使整个事情返回
true

您正在检查的第二个if语句!条目_1等应使用
&
而不是
|


对于
,“挡泥板”
将返回
true
!条目1.同等信号案例(“马丁”)
等等,在一个比较列表中,只有一个比较必须是
true
,才能使整个事情返回
true

您正在检查的第二个if语句!条目_1等应使用
&
而不是
|


对于
,“挡泥板”
将返回
true
!条目1.同等信号案例(“马丁”)
等等,在一个比较列表中,只有一个比较必须是
true
,才能使整个事情返回
true

您正在检查的第二个if语句!条目_1等应使用
&
而不是
|


对于
,“挡泥板”
将返回
true
!条目1.同等信号案例(“马丁”)
等,并且在一个
|
比较列表中,只有其中一个必须为
才能使整个事件返回

如果条件为else,只需更改整个秒:

if (entry_1.equalsIgnoreCase("FENDER")
                            || entry_1.equalsIgnoreCase("MARTIN")
                            || entry_1.equalsIgnoreCase("GIBSON")
                            || entry_1.equalsIgnoreCase("COLLINGS")
                            || entry_1.equalsIgnoreCase("OLSON")
                            || entry_1.equalsIgnoreCase("RYAN") 
                            || entry_1.equalsIgnoreCase("PRS")) 
            {
                entry_1 = entry_1.toUpperCase();

                System.out.println(entry_1);
            }

            else {

                entry_1 = "ANY";

                System.out.println(entry_1);
            } 

只需使用else更改整个第二个if条件:

if (entry_1.equalsIgnoreCase("FENDER")
                            || entry_1.equalsIgnoreCase("MARTIN")
                            || entry_1.equalsIgnoreCase("GIBSON")
                            || entry_1.equalsIgnoreCase("COLLINGS")
                            || entry_1.equalsIgnoreCase("OLSON")
                            || entry_1.equalsIgnoreCase("RYAN") 
                            || entry_1.equalsIgnoreCase("PRS")) 
            {
                entry_1 = entry_1.toUpperCase();

                System.out.println(entry_1);
            }

            else {

                entry_1 = "ANY";

                System.out.println(entry_1);
            } 

只需使用else更改整个第二个if条件:

if (entry_1.equalsIgnoreCase("FENDER")
                            || entry_1.equalsIgnoreCase("MARTIN")
                            || entry_1.equalsIgnoreCase("GIBSON")
                            || entry_1.equalsIgnoreCase("COLLINGS")
                            || entry_1.equalsIgnoreCase("OLSON")
                            || entry_1.equalsIgnoreCase("RYAN") 
                            || entry_1.equalsIgnoreCase("PRS")) 
            {
                entry_1 = entry_1.toUpperCase();

                System.out.println(entry_1);
            }

            else {

                entry_1 = "ANY";

                System.out.println(entry_1);
            } 

只需使用else更改整个第二个if条件:

if (entry_1.equalsIgnoreCase("FENDER")
                            || entry_1.equalsIgnoreCase("MARTIN")
                            || entry_1.equalsIgnoreCase("GIBSON")
                            || entry_1.equalsIgnoreCase("COLLINGS")
                            || entry_1.equalsIgnoreCase("OLSON")
                            || entry_1.equalsIgnoreCase("RYAN") 
                            || entry_1.equalsIgnoreCase("PRS")) 
            {
                entry_1 = entry_1.toUpperCase();

                System.out.println(entry_1);
            }

            else {

                entry_1 = "ANY";

                System.out.println(entry_1);
            } 

在第二个
if
中,保证
条目1
不是一种情况,也不是另一种情况或。。。逻辑是错误的。您需要带有
&&
的“and”:

if (!entry_1.equalsIgnoreCase("FENDER")
        && !entry_1.equalsIgnoreCase("MARTIN")
        && !entry_1.equalsIgnoreCase("GIBSON")
        && !entry_1.equalsIgnoreCase("COLLINGS")
        && !entry_1.equalsIgnoreCase("OLSON")
        && !entry_1.equalsIgnoreCase("RYAN")
        && !entry_1.equalsIgnoreCase("PRS")) {

    entry_1 = "ANY";

    System.out.println(entry_1);
}   
但是,将其作为
else
条件附加到第一个
if
条件会更简单:

if (entry_1.equalsIgnoreCase("FENDER")
                    || entry_1.equalsIgnoreCase("MARTIN")
                    || entry_1.equalsIgnoreCase("GIBSON")
                    || entry_1.equalsIgnoreCase("COLLINGS")
                    || entry_1.equalsIgnoreCase("OLSON")
                    || entry_1.equalsIgnoreCase("RYAN") 
                    || entry_1.equalsIgnoreCase("PRS")) 
{
    entry_1 = entry_1.toUpperCase();

    System.out.println(entry_1);
}
else
{
    entry_1 = "ANY";

    System.out.println(entry_1);
}

在第二个
if
中,保证
条目1
不是一种情况,也不是另一种情况或。。。逻辑是错误的。您需要带有
&&
的“and”:

if (!entry_1.equalsIgnoreCase("FENDER")
        && !entry_1.equalsIgnoreCase("MARTIN")
        && !entry_1.equalsIgnoreCase("GIBSON")
        && !entry_1.equalsIgnoreCase("COLLINGS")
        && !entry_1.equalsIgnoreCase("OLSON")
        && !entry_1.equalsIgnoreCase("RYAN")
        && !entry_1.equalsIgnoreCase("PRS")) {

    entry_1 = "ANY";

    System.out.println(entry_1);
}   
但是,将其作为
else
条件附加到第一个
if
条件会更简单:

if (entry_1.equalsIgnoreCase("FENDER")
                    || entry_1.equalsIgnoreCase("MARTIN")
                    || entry_1.equalsIgnoreCase("GIBSON")
                    || entry_1.equalsIgnoreCase("COLLINGS")
                    || entry_1.equalsIgnoreCase("OLSON")
                    || entry_1.equalsIgnoreCase("RYAN") 
                    || entry_1.equalsIgnoreCase("PRS")) 
{
    entry_1 = entry_1.toUpperCase();

    System.out.println(entry_1);
}
else
{
    entry_1 = "ANY";

    System.out.println(entry_1);
}

在第二个
if
中,保证
条目1
不是一种情况,也不是另一种情况或。。。逻辑是错误的。您需要带有
&&
的“and”:

if (!entry_1.equalsIgnoreCase("FENDER")
        && !entry_1.equalsIgnoreCase("MARTIN")
        && !entry_1.equalsIgnoreCase("GIBSON")
        && !entry_1.equalsIgnoreCase("COLLINGS")
        && !entry_1.equalsIgnoreCase("OLSON")
        && !entry_1.equalsIgnoreCase("RYAN")
        && !entry_1.equalsIgnoreCase("PRS")) {

    entry_1 = "ANY";

    System.out.println(entry_1);
}   
但是,将其作为
else
条件附加到第一个
if
条件会更简单:

if (entry_1.equalsIgnoreCase("FENDER")
                    || entry_1.equalsIgnoreCase("MARTIN")
                    || entry_1.equalsIgnoreCase("GIBSON")
                    || entry_1.equalsIgnoreCase("COLLINGS")
                    || entry_1.equalsIgnoreCase("OLSON")
                    || entry_1.equalsIgnoreCase("RYAN") 
                    || entry_1.equalsIgnoreCase("PRS")) 
{
    entry_1 = entry_1.toUpperCase();

    System.out.println(entry_1);
}
else
{
    entry_1 = "ANY";

    System.out.println(entry_1);
}

在第二个
if
中,保证
条目1
不是一种情况,也不是另一种情况或。。。逻辑是错误的。您需要带有
&&
的“and”:

if (!entry_1.equalsIgnoreCase("FENDER")
        && !entry_1.equalsIgnoreCase("MARTIN")
        && !entry_1.equalsIgnoreCase("GIBSON")
        && !entry_1.equalsIgnoreCase("COLLINGS")
        && !entry_1.equalsIgnoreCase("OLSON")
        && !entry_1.equalsIgnoreCase("RYAN")
        && !entry_1.equalsIgnoreCase("PRS")) {

    entry_1 = "ANY";

    System.out.println(entry_1);
}   
但是,将其作为
else
条件附加到第一个
if
条件会更简单:

if (entry_1.equalsIgnoreCase("FENDER")
                    || entry_1.equalsIgnoreCase("MARTIN")
                    || entry_1.equalsIgnoreCase("GIBSON")
                    || entry_1.equalsIgnoreCase("COLLINGS")
                    || entry_1.equalsIgnoreCase("OLSON")
                    || entry_1.equalsIgnoreCase("RYAN") 
                    || entry_1.equalsIgnoreCase("PRS")) 
{
    entry_1 = entry_1.toUpperCase();

    System.out.println(entry_1);
}
else
{
    entry_1 = "ANY";

    System.out.println(entry_1);
}

您需要将第二个if语句从使用
|
更改为
&&

if (!entry_1.equalsIgnoreCase("FENDER")
                    && !entry_1.equalsIgnoreCase("MARTIN")
                    && !entry_1.equalsIgnoreCase("GIBSON")
                    && !entry_1.equalsIgnoreCase("COLLINGS")
                    && !entry_1.equalsIgnoreCase("OLSON")
                    && !entry_1.equalsIgnoreCase("RYAN")
                    && !entry_1.equalsIgnoreCase("PRS")) {

如果使用
|
则只有一个continues必须为true,If语句本身才为true,并使
ANY
显示为输出。使用
&
时,在执行if语句并显示
任何

之前,所有语句都必须求值为true,您需要将第二个if语句从使用
|
更改为
&

if (!entry_1.equalsIgnoreCase("FENDER")
                    && !entry_1.equalsIgnoreCase("MARTIN")
                    && !entry_1.equalsIgnoreCase("GIBSON")
                    && !entry_1.equalsIgnoreCase("COLLINGS")
                    && !entry_1.equalsIgnoreCase("OLSON")
                    && !entry_1.equalsIgnoreCase("RYAN")
                    && !entry_1.equalsIgnoreCase("PRS")) {

如果使用
|
则只有一个continues必须为true,If语句本身才为true,并使
ANY
显示为输出。使用
&
时,在执行if语句并显示
任何

之前,所有语句都必须求值为true,您需要将第二个if语句从使用
|
更改为
&

if (!entry_1.equalsIgnoreCase("FENDER")
                    && !entry_1.equalsIgnoreCase("MARTIN")
                    && !entry_1.equalsIgnoreCase("GIBSON")
                    && !entry_1.equalsIgnoreCase("COLLINGS")
                    && !entry_1.equalsIgnoreCase("OLSON")
                    && !entry_1.equalsIgnoreCase("RYAN")
                    && !entry_1.equalsIgnoreCase("PRS")) {

如果使用
|
则只有一个continues必须为true,If语句本身才为true,并使
ANY
显示为输出。使用
&
时,在执行if语句并显示
任何

之前,所有语句都必须求值为true,您需要将第二个if语句从使用
|
更改为
&

if (!entry_1.equalsIgnoreCase("FENDER")
                    && !entry_1.equalsIgnoreCase("MARTIN")
                    && !entry_1.equalsIgnoreCase("GIBSON")
                    && !entry_1.equalsIgnoreCase("COLLINGS")
                    && !entry_1.equalsIgnoreCase("OLSON")
                    && !entry_1.equalsIgnoreCase("RYAN")
                    && !entry_1.equalsIgnoreCase("PRS")) {
如果使用
|
则只有一个continues必须为true,If语句本身才为true,并使
ANY
显示为输出。使用
&&
时,在执行if语句并在第二个if()use&¬|中显示
ANY
之前,所有语句都必须求值为true。就这些;)

在第二个if()中使用&¬ | |。就这些;)

在第二个if()中使用&¬ | |。就这些;)

在第二个if()中使用&¬ | |。就这些;)

使用trim()删除输入的空白。也许会有帮助。加上编程建议,当您可以用一个if-else循环替换它们时,千万不要使用多个if。它减少了CPU的开销