首页 > 解决方案 > Java - 比较 Sentinel 值的字符串的问题

问题描述

我的 while 循环和哨兵值没有注册有一个简单的问题。我得出的结论是,我的问题在于我的 while 循环,或者可能是用户输入的分配。我的程序的基本思想是我有一个带有字符串值的二维数组。我正在尝试根据用户输入访问那些。我询问用户想要什么状态,然后我的程序将返回状态、状态鸟和状态花。在主类中,我需要询问用户输入,使用 while 循环和我选择为“无”的标记值。我希望用户能够继续询问状态,直到他们在命令行中输入 none。我在我的代码中包含了注释,以帮助尝试和理解我正在尝试做的事情。我的问题是:为什么我的哨兵值不起作用?即使我不输入任何内容,循环也会永远继续。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class StateInfo{
  private int counter;
  private int i;
  private int summary[];
  public String output;
  private String userInput;

  // 50 states, each index with 3 entries (state, bird, flower)
  //String[][] stateInfo = new String[50][3];
  String stateInfo[][] = {
                            {"Alabama", "Camellia", "Yellowhammer"},
                            {"Alaska", "Alpine Forget-me-not", "Willow Ptarmigan"},
                            {"Arizona", "Saguaro Cactus Blossom", "Cactus Wren"},
                            {"Arkansas", "Apple Blossom", "Mockingbird"},
                            {"California", "California Poppy", "California Quail"},
                            {"Colorado", "Rocky Mountain Columbine", "Lark Bunting"},
                            {"Connecticut", "Mountain Laurel", "American Robin"},
                            {"Delaware", "Peach Blossom", "Blue Hen Chicken"},
                            {"Florida", "Orange Blossom", "Northern Mockingbird"},
                            {"Georgia", "Cherokee Rose", "Brown Thrasher"},
                            {"Hawaii", "Pua Aloalo", "Hawaiian Goose"},
                            {"Idaho", "Syringa", "  Mountain Bluebird"},
                            {"Illinois", "Violet", "Northern Cardinal"},
                            {"Indiana", "Peony", "Northern Cardinal"},
                            {"Iowa", "Wild Rose", "   Eastern Goldfinch"},
                            {"Kansas", "Wild Native Sunflower", "Western Meadowlark"},
                            {"Kentucky", "Goldenrod", "Northern Cardinal"},
                            {"Louisiana", "Louisiana Iris", "Brown Pelican"},
                            {"Maine", "White Pine Cone and Tassel", "Black-capped Chickadee"},
                            {"Maryland", "Black-Eyed Susan","Baltimore Oriole"},
                            {"Massachussets", "Mayflower", "Black-capped Chickadee"},
                            {"Michigan", "Dwarf Lake Iris", "American Robin"},
                            {"Minnesota", "Pink & White Lady Slipper", "Common Loon"},
                            {"Mississippi", "Coreopsis", "Northern Mockingbird"},
                            {"Missouri", "White Hawthorn Blossom", "Eastern Bluebird"},
                            {"Montana", "Bitterroot", "Western Meadowlark"},
                            {"Nebraska", "Goldenrod", "Western Meadowlark"},
                            {"Nevada", "Sagebrush", "Mountain Bluebird"},
                            {"New Hampshire", "Pink Lady's Slipper", "Purple Finch"},
                            {"New Jersey", "Violet", "Eastern Goldfinch"},
                            {"New Mexico", "Yucca", "Roadrunner"},
                            {"New York", "Rose", "Eastern Bluebird"},
                            {"North Carolina", "Dogwood","Northern Cardinal"},
                            {"North Dakota", "Wild Prairie Rose", "Western Meadowlark"},
                            {"Ohio", "White Trillium", "Northern Cardinal"},
                            {"Oklahoma", "Mistletoe", "Scissor-tailed Flycatcher"},
                            {"Oregon", "Oregon Grape", "Western Meadowlark"},
                            {"Pennsylvania", "Mountain Laurel", "Ruffed Grouse"},
                            {"Rhode Island", "Violet", "Rhode Island Red Chicken"},
                            {"South Carolina", "Yellow Jessamine", "Carolina Wren"},
                            {"South Dakota", "American Pasque", "Ring-necked Pheasant"},
                            {"Tennessee", "Iris", "Northern Mockingbird"},
                            {"Texas", "Bluebonnet", "Northern Mockingbird"},
                            {"Utah", "Sego Lily", "California Gull"},
                            {"Vermont", "Red Clover", "Hermit Thrush"},
                            {"Virginia", "American Dogwood", "Northern Cardinal"},
                            {"Washington", "Coast Rhododendron", "Willow Goldfinch"},
                            {"West Virginia", "Rhododendron", "Northern Cardinal"},
                            {"Wisconsin", "Wood Violet", "American Robin"},
                            {"Wyoming", "Indian Paintbrush", "Western Meadowlark"},
                           };

  public void findState() {
    for (i = 0; i < 50; i++){
        if (userInput == stateInfo[i][0]){
          output = "State: " + stateInfo [i][0] + "/n" + "Flower: " + stateInfo [i][1] + "/n" + "Bird: " + stateInfo [i][2];
        }
  }
}

public void setUserInput(String userInput) {
       this.userInput = userInput;
    }

public String getOutput() {
       return output;
    }
}

class Main extends StateInfo{
    public static void main(String[] args) throws IOException{
      StateInfo newState = new StateInfo();
      BufferedReader myReader = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter a State or None to exit:");
      //assigns user input to a variable for later use
      String input = myReader.readLine(); 

          //while loop to check the value of input and will not be entered if input = "none"
          while(input != "none"){
            //assigns the value of input to userInput from a newState instance
            newState.setUserInput(input);

            //trys to call the findState method. Find state method takes userInput (which should be = to input) and builds a string based on indexes from the 2d array
            newState.findState();

            //getter method for output. I want to get the new string that I built for output
            newState.getOutput();

            //ending first iteration of the while loop, begins another with a new assignment of input. If input = none, while loop exits and prints thank you to the screen
            System.out.println("Enter a State or None to exit:");
            input = myReader.readLine();
          }
          System.out.println("***** Thank you *****");
        }
}

标签: javamultidimensional-arraywhile-loopsentinel

解决方案


while(input != "none")比较字符串和input字符串的内存地址,"none"总是返回false。如果要检查input变量的值,请尝试while(!input.equals("none"))


推荐阅读