-4
私は分数計算に取り組んでいます。私は減量部分にはまっていますが、答えを印刷するためにこれをどのように作成するのか分かりません。また、私のコードに何か間違っている場合は、私に知らせてください。この電卓は2/7と6/7のような入力と+のような演算子を取るべきで、これは1_1/7を出力するはずです。事前に助けてくれてありがとう。分数計算ヘルプJAVA
import java.util.*;
public class calculator {
String operator;
String int1;
String int2;
public static int whole1;
public static int whole2;
public static int num1;
public static int num2;
public static int den1;
public static int den2;
public static void main (String []args) {
System.out.println("Hello, I'm a fractional calculator. FracCalc for short.");
Scanner everything = new Scanner(System.in);
System.out.println("Enter your first number type:");
String int1 = everything.nextLine();
System.out.println("Enter your second number:");
String int2 = everything.nextLine();
System.out.println("Enter operation (+, *, -, /):");
String operator = everything.nextLine();
}
public static void fractionFormat (String int1, String int2, String operator) {
if (int1.contains("_")) { //first number
whole1 = Integer.parseInt(int1.substring(0, int1.indexOf("_")));
num1 = Integer.parseInt(int1.substring(int1.indexOf("_") + 1, int1.indexOf("/")));
den1 = Integer.parseInt(int1.substring(int1.indexOf("/") + 1));
num1 = (whole1*den1)+num1;
}
else if (int1.contains("/")) {
num1 = Integer.parseInt(int1.substring(0, int1.indexOf("/")));
den1 = Integer.parseInt(int1.substring(int1.indexOf("/") + 1));
}
else {
whole1 = Integer.parseInt(int1.substring(0));
whole1 = num1;
den1 = 1;
}
if (int2.contains("_")) { //second number
whole2 = Integer.parseInt(int2.substring(0, int2.indexOf("_")));
num2 = Integer.parseInt(int2.substring(int2.indexOf("_") + 1, int2.indexOf("/")));
den2 = Integer.parseInt(int2.substring(int2.indexOf("/") + 1));
num2 = (whole2*den2)+num2;
}
else if (int2.contains("/")) {
num2 = Integer.parseInt(int2.substring(0, int2.indexOf("/")));
den2 = Integer.parseInt(int2.substring(int2.indexOf("/") + 1));
}
else {
whole2 = Integer.parseInt(int2.substring(0));
whole2 = num2;
den2 = 1;
}
math(operator, num1, den1, num2, den2);
}
public static void math(String operator, int num1, int den1, int num2, int den2) {
if (operator.equals("+")) {
System.out.println(addmath(num1, num2, den1, den2)); }
else if (operator.equals("-")) {
num2 = -1*num2;
System.out.println(addmath(num1, num2, den1, den2)); }
else if (operator.equals("*")) {
System.out.println(multmath(num1, num2, den1, den2)); }
else {
System.out.println(multmath(num1, num2, den1, den2)); }
}
public static String addmath(int num1 , int num2 , int den1 , int den2) {
int newnum = (num1*den2) + (num2*den1);
int newden = den1*den2;
String answer = reduce(newnum, newden);;
return answer; }
public static String multmath(int num1 , int num2 , int den1 , int den2) {
int newnum = num1 * num2;
int newden = den1 * den2;
String answer = reduce(newnum, newden);
return answer; }
メインのメソッドの呼び出しはどこですか? –