我不知道为什么我的分数不会降低。我想这是因为我没有打电话,但我不知道在哪里打电话。 (对不起,间距太差了。他们从来没有在学校检查过)
import java.util.*;
public class FracCalc_Egg {
public static String f1;
public static String op;
public static String f2;
public static int w1;
public static int w2;
public static int n1;
public static int n2;
public static int d1;
public static int d2;
public static void main(String[] args) {
System.out.println("Welcome to the Fraction calculator!");
Scanner console = new Scanner(System.in);
System.out.print("Enter an expression (or \"quit\"): ");
//get the first fraction, or quit
f1 = console.next();
//test fraction1 to see if the user types "quit"
if(f1.equalsIgnoreCase("quit")){
System.out.println("Goodbye!");
}
while(!f1.equalsIgnoreCase("quit")){
op = console.next();
f2 = console.next();
processFractions(f1, op, f2);
System.out.print("Enter an expression (or \"quit\"): ");
f1 = console.next();
if(f1.equalsIgnoreCase("quit")){
System.out.println("Goodbye!");
}
}//end while loop
//while loop continues the calc until the user types "quit"
}//end of main
public static void processFractions(String f1, String op, String f2){
//get int variables from fractions
//testing fraction 1 to get int values
if(f1.contains("_")){ //testing for mixed number
w1=Integer.parseInt(f1.substring(0,f1.indexOf("_")));
n1=Integer.parseInt(f1.substring(f1.indexOf("_")+1,f1.indexOf("/")));
d1=Integer.parseInt(f1.substring(f1.indexOf("/")+1));
n1=(w1*d1)+n1; //making mixed number improper
} else if(f1.contains("/")) { //testing for fraction
n1=Integer.parseInt(f1.substring(0,f1.indexOf("/")));
d1=Integer.parseInt(f1.substring(f1.indexOf("/")+1));
} else {//testing for whole number
w1=Integer.parseInt(f1.substring(0));
n1=w1;
d1=1;
}//end if, else if, else method
//testing fraction 2 to get int values
if(f2.contains("_")){ //mixed fraction
w2=Integer.parseInt(f2.substring(0,f2.indexOf("_")));
n2=Integer.parseInt(f2.substring(f2.indexOf("_")+1,f2.indexOf("/")));
d2=Integer.parseInt(f2.substring(f2.indexOf("/")+1));
n2=w2*d2+n2;
} else if(f2.contains("/")) { //fraction
n2=Integer.parseInt(f2.substring(0,f2.indexOf("/")));
d2=Integer.parseInt(f2.substring(f2.indexOf("/")+1));
} else { //whole number
w2=Integer.parseInt(f2.substring(0));
n2=w2;
d2=1;
}//end if, else if, else method
dotheMath(n1, n2, d1, d2, op);
}//end processFraction method
//dotheMath detmerines the operator
public static void dotheMath(int n1, int n2, int d1, int d2, String op) {
if(op.equals("+")){
System.out.println(add(n1, n2, d1, d2));
} else if(op.equals("-")) {
n2=-1*n2;
System.out.println(add(n1, n2, d1, d2));
} else if(op.equals("*")) {
System.out.println(multiply(n1, n2, d1, d2));
} else {
int x = n2;
int y = d2;
d2=x;
n2=y;
System.out.println(multiply(n1, n2, d1, d2));
} //end the if, else if, else statement
}//end dotheMath method
public static String add(int n1, int n2, int d1, int d2) {
int newn = (n1*d2) + (n2*d1);
int newd = d1*d2;
int divisor = reduce(newn,newd);
newn/=divisor;
newd/=divisor;
String answer = newn+"/"+newd;
return answer;
}//end add method
public static String multiply(int n1, int n2, int d1, int d2) {
int newn = n1*n2;
int newd = d1*d2;
int divisor = reduce(newn,newd);
newn/=divisor;
newd/=divisor;
String answer = newn+"/"+newd;
return answer;
}//end multiply method
public static int lcd(int n1,int d1, int n2, int d2){
int dividend=(d1*n2)+(n1*d2);
int divisor = d1*d2;
int rem = dividend % divisor;
while (rem != 0){
dividend = divisor;
divisor = rem;
rem = dividend % divisor;
}
return divisor;
} //end lcd
public static int reduce (int newn, int newd) { //
int newn_abs = Math.abs (newn);
int newd_abs = Math.abs (newd); //
int min_num = Math.min (newn_abs, newd_abs);
int divisor = 1;
for (int i = 1; i <= min_num; i++) {
if (newn%i == 0 && newd%i == 0){
divisor = 1;
}//end if
}//end for
return divisor;
}//end reduce
}//end of class
例 -
欢迎来到分数计算器!
输入表达式(或“退出”):1/4 + 1_1 / 2
14/8
预期产量为1_3 / 4
而且我被困在reduce方法中。有人在课堂上告诉我,我没有打电话给那个方法,但我不知道他们的意思。他们告诉我用add and multiply方法调用它,但是你怎么做/在哪里做?
答案 0 :(得分:1)
当你的队友告诉你,你没有调用还原方法时,他们的意思是,你永远不会使用 reduce方法。
你的add-method应该看起来像这样:
public static String add(int n1, int n2, int d1, int d2) {
int newn = (n1*d2) + (n2*d1);
int newd = d1*d2;
int divisor = reduce(newn, newd);
newn/=divisor;
newd/=divisor;
int integerComponent=0;
while(newn >= newd) {
integerComponent++;
newn-=newd;
}
String answer ="";
if(integerComponent>0) {
answer += integerComponent +"_";
}
if(newn!=0) {
answer += newn+"/"+newd;
}
return answer;
}
并且乘法方法应如下所示:
public static String multiply(int n1, int n2, int d1, int d2) {
int newn = n1*n2;
int newd = d1*d2;
int divisor = reduce(newn, newd);
newn/=divisor;
newd/=divisor;
int integerComponent=0;
while(newn >= newd) {
integerComponent++;
newn-=newd;
}
String answer ="";
if(integerComponent>0) {
answer += integerComponent +"_";
}
if(newn!=0) {
answer += newn+"/"+newd;
}
return answer;
}
请记住,您还必须更改您的reduce方法,因为它现在总是返回1!
修改:添加了将分数打印为混合分数的代码。
答案 1 :(得分:0)
在我得到答案之前,我只有一个请求,请修复你的缩进。通过在您的问题中提供新格式化,易于编码,您更有可能获得答案,并且更有可能获得高质量的答案。
现在,我的回答。这是一个学校项目,你应该通过它来学习。在那方面,我不会给你完整的答案,但我会尽力指导你。所以,有几件事,首先,你的朋友是对的,其次,你的减少功能是不正确的。您要在multiply
函数和String answer = newn+"/"+newd;
函数中的两个位置生成函数:
String answer = reduce(newn, newd);
在此步骤中您需要做的是在
中调用reduce函数$injector
现在,您需要更改reduce函数以正确减少分数并处理不正确的分数。