if
ステートメントを別の方法に変換するのが難しいです。私はそれを4つの別々のメソッドに変換しようとしています:add、sub、mul、およびdiv、CMDに記述する必要がありますが、何とかできません。私は助けと感謝が必要です。Java:ifステートメントからメソッドへの変換
public static void main(String[] args){
int first = Integer.parseInt(args[1]);
int second = Integer.parseInt(args[2]);
int third = Integer.parseInt(args[3]);
int forth = Integer.parseInt(args[4]);
if(args[0].equals("add")){
if (second == 0 || forth == 0){
System.out.println("Undividable by zero");
} else if (second == forth){
System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first + third) + "/" + forth);
} else {
System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first * forth + second * third) + "/" + (second * forth));
}
} else if (args[0].equals("sub")){
if (second == 0 || forth == 0){
System.out.println("Undividable by zero");
} else if (second == forth){
System.out.println(first + "/" + second + " - " + third + "/" + forth + " = " + (first + third) + "/" + forth);
} else {
System.out.println(first + "/" + second + " - " + third + "/" + forth + " = " + (first * forth - second * third) + "/" + (second * forth));
}
} else if (args[0].equals("mul")){
if (second == 0 || forth == 0){
System.out.println("Undividable by zero");
} else {
System.out.println(first + "/" + second + " * " + third + "/" + forth + " = " + (first * third) + "/" + (second * forth));
}
} else if (args[0].equals("div")){
if (second == 0 || forth == 0){
System.out.println("Undividable by zero");
} else {
System.out.println(first + "/" + second + " : " + third + "/" + forth + " = " + (first * forth) + "/" + (second * third));
}
} else {
System.out.println("Recheck the code");
}
}
なぜ4つのメソッドを作成し、 'args [0]'の値に応じて正しいメソッドを呼び出せませんか? – tilper
@tilperそれはまさに彼がやろうとしていることです。 –
これは私の宿題であり、add、sub、mul、divなどの演算子はメソッド –