Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中将按位运算符分配给变量_Java - Fatal编程技术网

在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