我正在研究一些例子并且坚持这个问题:
您将开发一个程序,根据用户的开始和结束范围以及表的类型打印出乘法或加法表。 该程序将使用2D数组来计算表格。 该程序将使用System.out.printf()方法来允许格式化输出。 该程序将具有以下名称和签名的两种方法: public void createTable(int begin,int finish,TableType tableType) public void printTable() 将提供另一种检查传入main的参数的方法(称为argumentCheck())。如果传入的参数有效并且将设置数据成员,则此方法将返回true:start,end和tableType。 没有构造函数,也没有任何其他方法(包括重载方法)。 该类将包含表的数据成员(句柄)(float类型的2D数组),表的开始和结束值的start和end(int),tableType(TableType)包含{MULT的枚举类型,ADD}。
public class ArithmeticTable {
private TableType tableType;
static int start = 1;
static int end = 10;
public enum TableType {
MULT {
int result(int x, int y) {return x * y;}},
ADD {
int result(int x, int y) {return x + y;}}
}
public boolean argumentCheck(String[] args){
if(args.length!=3){
System.err.println("Usage: Main <type> <start> <stop>");
System.err.println("\tWhere <type> is one of +, \"*\"");
System.err.println("\tand <start> is between 1 and 100");
System.err.println("\tand <stop> is between 1 and 100");
System.err.println("\tand start < stop");
return false;
}
if(args[0].charAt(0) == '+')
tableType = TableType.ADD;
else
tableType = TableType.MULT;
int sta;
int sto;
try{
sta = Integer.parseInt(args[1]);
sto = Integer.parseInt(args[2]);
}
catch(NumberFormatException ex){
System.err.println("Usage: Main <type> <start> <stop>");
System.err.println("\tWhere <type> is one of +, -, \"*\", /");
System.err.println("\tand <start> is between 1 and 100");
System.err.println("\tand <stop> is between 1 and 100");
System.err.println("\tand start < stop");
return false;
}
if((sta < 1 || sta > 100)||((sto < 1 || sto > 100))){
System.err.println("Usage: Main <type> <start> <stop>");
System.err.println("\tWhere <type> is one of +, -, \"*\", /");
System.err.println("\tand <start> is between 1 and 100");
System.err.println("\tand <stop> is between 1 and 100");
System.err.println("\tand start < stop");
return false;
}
if(sta >= sto){
System.err.println("Usage: Main <type> <start> <stop>");
System.err.println("\tWhere <type> is one of +, -, \"*\", /");
System.err.println("\tand <start> is between 1 and 100");
System.err.println("\tand <stop> is between 1 and 100");
System.err.println("\tand start < stop");
return false;
}
start = sta;
end = sto;
return true;
}
public void createTable(int begin, int finish, TableType tableType)
{
int i, result;
for(i = begin; i <= finish; i++){
switch(tableType){
case MULT:
result = begin * finish;
break;
case ADD:
result = begin + finish;
break;
default:
result = begin * finish;
break;
}
}
}
public void printTable()
{
}
public static void main(String[] args){
ArithmeticTable table = new ArithmeticTable();
if (table.argumentCheck(args)){
table.createTable(table.start, table.end, table.tableType);
table.printTable();
}
}
}
我正在尝试使枚举具有基本操作数方法并将符号(&#34; *&#34;和&#34; +&#34;)放到表格中。有什么建议吗?
答案 0 :(得分:0)
这是你的意思吗?
public enum TableType {
MULT,
ADD;
public static TableType get(char operand) {
return (operand == '+') ? ADD : ((operand == '*') ? MULT : null);
}
public int result(int x, int y) {
return (this == ADD) ? (x + y) : (x * y);
}
}
...
TableType example = TableType.get('*');
System.out.println(example.result(4, 4)); // 16
答案 1 :(得分:0)
使用Java 8函数可能更容易。例如:
BiFunction<Integer, Integer, Integer> add = (Integer a, Integer b) -> a + b; // or Integer::sum
BiFunction<Integer, Integer, Integer> multiply = (Integer a, Integer b) -> a * b;
System.out.println("1 + 2 = " + add.apply(1, 2));
System.out.println("2 * 3 = " + multiply.apply(2, 3));