如何将这个简单计算器程序中的switch/case语句转换为跳转表?
import java.lang.*;
import java.util.*;
public class Calculator
{
private int solution;
private static int x, y, ops;
private char operators;
public Calculator()
{
solution = 0;
}
public int addition(int x, int y)
{
return x + y;
}
public int subtraction(int x, int y)
{
return x - y;
}
public int multiplication(int x, int y)
{
return x * y;
}
public int division(int x, int y)
{
solution = x / y;
return solution;
}
public void calc(int ops){
Scanner operands = new Scanner(System.in);
System.out.println("operand 1: ");
x = operands.nextInt();
System.out.println("operand 2: ");
y = operands.nextInt();
System.out.println("Solution: ");
switch(ops)
{
case(1):
System.out.println(addition(x, y));
break;
case(2):
System.out.println(subtraction(x, y));
break;
case(3):
System.out.println(multiplication(x, y));
break;
case(4):
System.out.println(division(x, y));
break;
}
}
public static void main (String[] args)
{
System.out.println("What operation? ('+', '-', '*', '/')");
System.out.println(" Enter 1 for Addition");
System.out.println(" Enter 2 for Subtraction");
System.out.println(" Enter 3 for Multiplication");
System.out.println(" Enter 4 for Division");
Scanner operation = new Scanner(System.in);
ops = operation.nextInt();
Calculator calc = new Calculator();
calc.calc(ops);
}
}
老实说,我不知道跳转表到底是什么(在网上找不到任何解释),所以我不知道它与switch/case语句有什么不同。
附注:这段代码只处理整数,所以如果你除以5/3,它会得到1。我怎么才能很容易地把它改成浮点数/双精度数。
发布于 2013-10-10 00:15:34
如前所述,跳转表是指向函数的偏移量/指针数组。与C/C++不同,Java实际上没有函数指针(Function Pointers in Java)。
但是您可以假装,并以面向对象的方式完成它。使用一个方法(f)定义一个基类(Funky)。派生多个子代,每个函数操作(+、-、*、/等)一个子代,为每个子代创建一个单独的对象(毕竟它只是一个接口),并将那个子代存储到一个类型数组(Funky)中。
在表中查找操作,并根据参数调用该方法
示例:
定义一个基类(或者一个让你更快乐的接口?)。请注意,如果扩展类,则可以使用基类方法作为默认方法(生成错误消息或抛出异常)。
public class X
//or, public interface X
{
//method
Z fun(Z z1, Z z2)
{
//nothing to see here
}
}
class X1 extends X //or, implements X
{
public Z fun(Z z1, Z z2)
{
//variant1 stuff here
}
}
...
public class Xn extends X //or, implements X
{
public Z fun(Z z1, Z z2)
{
//variantn stuff here
}
}
哦,您还需要实例,并将它们加载到一个数组中(跳转)。
有一些特定的技术对于特定的语言来说是惯用的,而跳转表更多的是系统的东西,而不是Java的东西,而不是真正的Java惯用语言。
发布于 2013-10-10 00:05:10
嗯,我不知道什么是跳转表,但是如果你想控制另一种类型的数字,你可以改变参数,例如你的方法:
public int addition(int x, int y)
{
return x + y;
}
如果你想加倍-->
public int addition(Double x, Double y)
但我强烈建议你每隔一个类就使用Number扩展的类型Number。
例如:
public static String numeroToLetra(Number num)
{
Integer numero = Integer.valueOf(num.intValue()); //int value
Double numero = Double.valueOf(num.doubleValue());........
}//so you can pass whatever type of number.
发布于 2018-10-21 06:33:12
这是一个老问题,但我认为它仍然有价值来说明自Java 8以来您可以做些什么。基本上,您创建了一个接口,其唯一目的是为操作数组提供类型,然后使用方法引用来填充操作数组。之后,您可以使用索引来选择适当的操作。我对OP的代码做了最小的修改,以便比较最简单:
import java.util.Scanner;
public class Calculator
{
//
// Create an interface to use as Type for
// operations array.
//
private interface BinaryOperation {
int performOperation(int a, int b);
}
//
// Array has one unused element to make coding easier
// and use operation as a direct index.
// You can replace with 4 element array easily.
//
BinaryOperation[] operations = new BinaryOperation[5];
private int solution;
private static int x, y, ops;
private char operators;
public Calculator()
{
solution = 0;
//
// Initialize jump table using method references.
//
operations[1] = this::addition;
operations[2] = this::subtraction;
operations[3] = this::multiplication;
operations[4] = this::division;
}
public int addition(int x, int y)
{
return x + y;
}
public int subtraction(int x, int y)
{
return x - y;
}
public int multiplication(int x, int y)
{
return x * y;
}
public int division(int x, int y)
{
solution = x / y;
return solution;
}
public void calc(int ops){
Scanner operands = new Scanner(System.in);
System.out.println("operand 1: ");
x = operands.nextInt();
System.out.println("operand 2: ");
y = operands.nextInt();
System.out.println("Solution: ");
//
// Call binary operation through jump table
//
System.out.println(operations[ops].performOperation(x, y));
}
public static void main (String[] args)
{
System.out.println("What operation? ('+', '-', '*', '/')");
System.out.println(" Enter 1 for Addition");
System.out.println(" Enter 2 for Subtraction");
System.out.println(" Enter 3 for Multiplication");
System.out.println(" Enter 4 for Division");
Scanner operation = new Scanner(System.in);
ops = operation.nextInt();
Calculator calc = new Calculator();
calc.calc(ops);
}
}
https://stackoverflow.com/questions/19276940
复制相似问题