我是Java的新手,我正在练习执行Java应用程序,该应用程序循环询问用户有关此人已出售的某些项目的一些输入。但是我试图根据我的菜单选项使我的if语句条件。显示Else语句中遵循的错误消息。但是我缺少看不见的东西,所以我希望你们的一个人能真正看到它,让我知道我缺少的东西。
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
SalesCal a = new SalesCal();
int select = 1; // menu selection
int solds; // sales person input
a.displayMessage();
while(select != 0){
System.out.print("Plese enter t1 for item1($239.99),ntt2 for item2($129.75),ntt3 for item3($99.95),ntt4 for item4($350.89),ntt0 to cancel: ");
select = input.nextInt(); //getting user input
System.out.print("n");
if((select <= 4) && (select <= 0) && (select < 0)){
System.out.printf("You selected: item%dn", select);
System.out.printf("Plese enter the quantity sold for item%d: ", select);
solds = input.nextInt(); //getting user input
System.out.print("n");
a.GrossCal(select, solds);
} else {
System.err.print("Wrong selectiong, please try again...nn");
}
}
a.displayResults();
}
}
谢谢,我将非常感谢您的帮助...
if((select <= 4) && (select >= 0))
,此语句应照顾-ve输入
我认为您想要的if
语句逻辑是:
if (select > 0 && select <= 4){
System.out.printf("You selected: item%dn", select);
System.out.printf("Plese enter the quantity sold for item%d: ", select);
solds = input.nextInt(); //getting user input
System.out.print("n");
a.GrossCal(select, solds);
} else if (select != 0) {
System.err.print("Wrong selectiong, please try again...nn");
}
当用户输入0。
if((select <= 4) && (select <= 0) && (select < 0))
A B C
与
完全相同if(select < 0)
D
考虑一下:
- 如果
select
为5:A,B,C和D全部失败(false
结果)。这两个IFS返回false
。 - 如果是4:B,C和D失败。这两个IFS返回
false
。 - 如果是1:B,C和D失败。这两个IFS返回
false
。 - 如果是0:C和D失败。两者都返回
false
。 - 如果是-1:a,b,c和d全部成功(返回
true
)。这两个IFS返回true
。
但是,如果您要小于零或大于四个被认为是不好的,这就是我认为的意思,那么您想要
if(select < 0 || select > 4)
如果零和四个也不好,则使用
if(select <= 0 || select => 4)
或
if(!(select > 0 && select < 4))
看起来您的最后一个车把。
确定哪个是否属于哪个是最内在的方法。最接近的是与if相关的其他。您可以从那里向外工作。
:D
,或者您可以使用Switch语句并在您自己的逻辑中输入:
switch(select) {
case 0: System.out.println("select = 0");
break;
case 1: System.out.println("select = 1");
break;
case 2: System.out.println("select = 2");
break;
case 3: System.out.println("select = 3");
break;
case 4: System.out.println("select = 4");
break;
default: System.out.println("Please enter a number from zero to four");
}
希望这会有所帮助。