我的 while 循环和哨兵值没有注册有一个简单的问题。我得出的结论是我的问题在于我的 while 循环,或者可能是用户输入的分配。我的程序的基本思想是我有一个带有字符串值的二维数组。我正在尝试根据用户输入访问那些。我询问用户想要什么状态,然后我的程序将返回状态、州鸟和州花。在主类中,我需要请求用户输入、使用 while 循环和我选择为“none”的标记值。我希望用户能够继续询问状态,直到他们在命令行中输入 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"},
1 回答
MM们
TA贡献1886条经验 获得超2个赞
while(input != "none")
比较input
字符串的"none"
和的内存地址,并且总是返回false。如果要检查input
变量的值,请尝试while(!input.equals("none"))
添加回答
举报
0/150
提交
取消