所以我只是在玩弄我学到的一些东西,制作一些毫无意义的计算器。在有人说之前,我知道这可以更简单地完成!
尽管在 while 语句中我收到此错误(二元运算符“||”的操作数类型错误,第一种类型:int,第二种类型:boolean”)
它也会引发问题
int AA = A + B;
int AB = A * B;
int AC = A / B;
说找不到符号。
class cases{ //1-4 sums with cases, delete after//
public static void main(String args[]){
System.out.println("Welcome to a pointless calculator.");
int A = Integer.parseInt(args[0]);
int B = Integer.parseInt(args[1]);
switch (A){
case 1:
System.out.print("You entered 1");
break;
case 2:
System.out.print("You entered 2");
break;
case 3:
System.out.print("You entered 3");
break;
case 4:
System.out.print("You entered 4");
}
while ( A || B > 4){
System.out.print("please enter numbers 1-4");
break;
}
switch (B) {
case 1:
System.out.println(" and 1");
break;
case 2:
System.out.println(" and 2");
break;
case 3:
System.out.println(" and 3");
break;
case 4:
System.out.println(" and 4");
}
}
{
int AA = A + B;
int AB = A * B;
int AC = A / B;
System.out.print("the answers added = ");
System.out.println(AA);
System.out.print("the answers multipled = ");
System.out.println(AB);
System.out.print("the answers divided = ");
System.out.println(AC);
}
}
最佳答案
问题出在这里:
while ( A || B > 4){
这个表达式可以这样分解:
while (
A
||
B > 4
){
A
的类型为 int
,但您将其视为 boolean
。在 Java 中你不能这样做。您可能的意思是:
while ( A > 4 || B > 4){
<小时/>
代码中很可能还有其他问题。例如,您的代码末尾有一个与任何内容都没有关联的 block 。我认为在 Java 中最终会成为实例初始化程序 block ,但坦率地说,我认为您需要退一步并完成一些教程。
关于java - "first type int, second type boolean"java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20461129/