我知道这个标题听起来像很多人喜欢它,但我花了一个多小时试图用谷歌和堆栈溢出来解决这个问题。
我正在尝试为用户创建一个菜单供选择,但是当我输入一个选项时,它没有注册为相等。
http://example.com/shine?n=1&i=0
我试过了:
.post('http://example.com/shine?n=' + '*' + '&i=' + '*')
System.out.println("What would you like to do?\n1. Add\n2. End session");
String Selection = null;
Console reader;
try{
reader = System.console();
if(reader != null){
do{
Selection = reader.readLine("Please enter corresponding number: ");
if(((Integer.parseInt(Selection)) != 1) || ((Integer.parseInt(Selection)) != 2)){
System.out.println("Please enter a valid option");
}
}while(((Integer.parseInt(Selection)) != 1) || ((Integer.parseInt(Selection)) != 2));
}
}catch(Exception ex) {ex.printStackTrace();}
Selection.equals("1")
Selection.equals("Add")
我无法弄清楚出了什么问题。我想如果String to String比较不起作用,int会起作用,但它没有:(当我编译并运行程序时,它只是停留在循环中,除了在最后一个版本中使用“parseInt”我可以抛出离开循环的例外。
当我尝试用“=”替换“!=”时,它给了我这个错误:
"Add".equals(Selection)
为什么我会收到此错误,但是当我有“!=”时,为什么if和do / while语句甚至会在我输入的任何内容时执行。
编辑: 哎呀,我完全间隔=对==。一旦我解决了这个问题,它就像我期望的那样运行:它循环,我不想要。但如果我使用!=,它也会循环。怎么来???
答案 0 :(得分:-1)
你的逻辑错了。
试
!(Integer.parseInt(Selection) == 1 || Integer.parseInt(Selection) == 2)
或
Integer.parseInt(Selection) != 1 && Integer.parseInt(Selection) != 2