在java中将按位运算符分配给变量
我尝试将位运算符赋给变量。我怎么做 乙二醇在java中将按位运算符分配给变量,java,Java,我尝试将位运算符赋给变量。我怎么做 乙二醇 但它不起作用。任何人都可以 您的代码甚至不应该编译,所以我不确定您真正想要实现什么。您是想计算您创建的字符串表达式,还是只想得到更经典的结果?x和y来自哪里。。。?代码中有一个重要部分缺失 无论如何,我会选择第二个选项: int eval; String rFunction=JOptionPane.showInputDialog(null,"Enter Your round function","Round function",JOptionPane
但它不起作用。任何人都可以 您的代码甚至不应该编译,所以我不确定您真正想要实现什么。您是想计算您创建的字符串表达式,还是只想得到更经典的结果?x和y来自哪里。。。?代码中有一个重要部分缺失 无论如何,我会选择第二个选项:
int eval;
String rFunction=JOptionPane.showInputDialog(null,"Enter Your round function","Round function",JOptionPane.INFORMATION_MESSAGE).toUpperCase();
if(rFunction.equals("AND")){
eval = x & y;
} else if(rFunction.equals("OR")){
eval = x | y;
} else if(rFunction.equals("XOR")){
eval = x ^ y;
} else {
//here you could also throw an exception
//or loop and request the user to renew their choice
System.out.print("Invalid choice");
return;
}
System.out.print(res);
您的代码甚至不应该编译,所以我不确定您真正想要实现什么。您是想计算您创建的字符串表达式,还是只想得到更经典的结果?x和y来自哪里。。。?代码中有一个重要部分缺失 无论如何,我会选择第二个选项:
int eval;
String rFunction=JOptionPane.showInputDialog(null,"Enter Your round function","Round function",JOptionPane.INFORMATION_MESSAGE).toUpperCase();
if(rFunction.equals("AND")){
eval = x & y;
} else if(rFunction.equals("OR")){
eval = x | y;
} else if(rFunction.equals("XOR")){
eval = x ^ y;
} else {
//here you could also throw an exception
//or loop and request the user to renew their choice
System.out.print("Invalid choice");
return;
}
System.out.print(res);
您可以制作自己的BitOperator:
public enum BitOperator {
AND {
@Override
public int calc(int x, int y) {
return x & y;
}
},
OR {
@Override
public int calc(int x, int y) {
return x | y;
}
},
XOR {
@Override
public int calc(int x, int y) {
return x ^ y;
}
};
public abstract int calc(int x,int y);
}
用法(添加一些异常处理):
您可以制作自己的BitOperator:
public enum BitOperator {
AND {
@Override
public int calc(int x, int y) {
return x & y;
}
},
OR {
@Override
public int calc(int x, int y) {
return x | y;
}
},
XOR {
@Override
public int calc(int x, int y) {
return x ^ y;
}
};
public abstract int calc(int x,int y);
}
用法(添加一些异常处理):
您实际上是在尝试执行“eval”。请看一看。此外,您还需要执行计算(而不是分配“bitOp”)。闻起来像枚举。@ElliottFrisch这是错误的副本。即使字符串比较正确,也不会有问题。它显示NumberFormatException这肯定不会显示
NumberFormatException
。您实际上是在尝试执行“eval”。请查看。此外,您还需要执行计算(而不是分配“bitOp”).闻起来像一个枚举。@ElliottFrisch这是错误的副本。即使字符串比较正确,也不会有问题。它显示NumberFormatException这肯定不会显示NumberFormatException
。