我希望程序使用许多字符串读取用户输入

时间:2022-09-08 23:47:47

I want the user input to be read and from there the if statements will take over but it is not reading the String. I will be adding more station in different zones. help!

我想要读取用户输入,并从那里if语句将接管,但它不读取字符串。我将在不同的区域增加更多的站点。帮帮我!

import java.util.*;

public class centralline {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("What station do you need to know the zone of? ");
        String answer = input.nextLine();
        String zone1 = "Liverpool Street" + "Oxford Street" + "Bank";
        String zone2 = "Mile End" + "Stratford";

        {
            if (zone1.equals(answer)) {
                System.out.println(answer + " is in Zone 1");

            } else if (zone2.equals(answer)) {
                System.out.println(answer + " is in Zone 2");
            } else
                System.out
                        .println("is"
                                + answer
                                + "a Loodon underground station? Maybe check your spelling. ");
        }
    }
}

2 个解决方案

#1


2  

Either store your zones in String Arrays and check to see if the answer is in the Array or use .contains() not .equals().

将区域存储在String Arrays中并检查答案是否在Array中,或者使用.contains()而不是.equals()。

if (zone1.contains(answer))

...

...

 } else if (zone2.contains(answer)) {

Or:

要么:

String[] zone1 = new String[3];
zone1[0] = "Liverpool Street";
zone1[1] = "Oxford Street";
zone1[2] = "Bank";

for(String a : zone1)
{
    if (a.equals(answer))
    {
         System.out.println( answer+" is in Zone 1");
         break;
    }
}

Just repeat the steps for other zones...Create a boolean variable to keep track of whether answer was found in one of the zones.

只需重复其他区域的步骤...创建一个布尔变量,以跟踪是否在其中一个区域中找到了答案。

#2


1  

You could use methods if you dont want to use arrays:

如果您不想使用数组,可以使用方法:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("What station do you need to know the zone of? ");
    String answer = input.nextLine();

        if (isZone1(answer)) {
            System.out.println(answer + " is in Zone 1");

        } else if (isZone2(answer)) {
            System.out.println(answer + " is in Zone 2");
        } else{
            System.out
                    .println("is "
                            + answer
                            + "a Loodon underground station? Maybe check your spelling. ");

        }
}

public static boolean isZone1(String zone)
{
    if(zone.equals("Liverpool Street") || zone.equals("Oxford Street") || zone.equals("Bank"))
    {
        return true;
    }
    return false;
}
public static boolean isZone2(String zone)
{
    if(zone.equals("Mile End") || zone.equals("Stratford"))
    {
        return true;
    }
    return false;
}

or you can just move the logic inside your program, if you dont want to use arrays/methods:

或者你可以只是移动程序中的逻辑,如果你不想使用数组/方法:

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("What station do you need to know the zone of? ");
    String answer = input.nextLine();

        if(answer.equals("Liverpool Street") || answer.equals("Oxford Street") || answer.equals("Bank")) {
            System.out.println(answer + " is in Zone 1");

        } else if(answer.equals("Mile End") || answer.equals("Stratford")){
            System.out.println(answer + " is in Zone 2");
        } else{
            System.out
                    .println("is "
                            + answer
                            + "a Loodon underground station? Maybe check your spelling. ");

        }
}

#1


2  

Either store your zones in String Arrays and check to see if the answer is in the Array or use .contains() not .equals().

将区域存储在String Arrays中并检查答案是否在Array中,或者使用.contains()而不是.equals()。

if (zone1.contains(answer))

...

...

 } else if (zone2.contains(answer)) {

Or:

要么:

String[] zone1 = new String[3];
zone1[0] = "Liverpool Street";
zone1[1] = "Oxford Street";
zone1[2] = "Bank";

for(String a : zone1)
{
    if (a.equals(answer))
    {
         System.out.println( answer+" is in Zone 1");
         break;
    }
}

Just repeat the steps for other zones...Create a boolean variable to keep track of whether answer was found in one of the zones.

只需重复其他区域的步骤...创建一个布尔变量,以跟踪是否在其中一个区域中找到了答案。

#2


1  

You could use methods if you dont want to use arrays:

如果您不想使用数组,可以使用方法:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("What station do you need to know the zone of? ");
    String answer = input.nextLine();

        if (isZone1(answer)) {
            System.out.println(answer + " is in Zone 1");

        } else if (isZone2(answer)) {
            System.out.println(answer + " is in Zone 2");
        } else{
            System.out
                    .println("is "
                            + answer
                            + "a Loodon underground station? Maybe check your spelling. ");

        }
}

public static boolean isZone1(String zone)
{
    if(zone.equals("Liverpool Street") || zone.equals("Oxford Street") || zone.equals("Bank"))
    {
        return true;
    }
    return false;
}
public static boolean isZone2(String zone)
{
    if(zone.equals("Mile End") || zone.equals("Stratford"))
    {
        return true;
    }
    return false;
}

or you can just move the logic inside your program, if you dont want to use arrays/methods:

或者你可以只是移动程序中的逻辑,如果你不想使用数组/方法:

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("What station do you need to know the zone of? ");
    String answer = input.nextLine();

        if(answer.equals("Liverpool Street") || answer.equals("Oxford Street") || answer.equals("Bank")) {
            System.out.println(answer + " is in Zone 1");

        } else if(answer.equals("Mile End") || answer.equals("Stratford")){
            System.out.println(answer + " is in Zone 2");
        } else{
            System.out
                    .println("is "
                            + answer
                            + "a Loodon underground station? Maybe check your spelling. ");

        }
}