我的while循环有一个简单的问题,并且哨兵值未注册。我得出的结论是,我的问题出在我的while循环或用户输入的分配上。我程序的基本思想是,我有一个带有String值的2D数组。我正在尝试根据用户输入访问这些内容。我问用户想要什么状态,然后我的程序将返回状态,状态鸟和状态花。在主类中,我需要询问用户输入,使用while循环和我选择为“无”的哨兵值。我希望用户能够继续请求状态,直到他们在命令行中均未输入任何状态为止。我在代码中添加了注释,以帮助尝试理解我的意图。我的问题是:为什么我的哨兵值不起作用?即使我什么也没输入,循环永远持续下去。为什么即使给程序合法的输入(例如“ Texas”),我的代码也不会返回输出值?
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 *****");
}
}
答案 0 :(得分:1)
while(input != "none")
比较input
和字符串"none"
的内存地址,并且始终返回false。如果要检查input
变量的值,请尝试while(!input.equals("none"))