Java 分数计算器

标签 java calculator jgrasp

我不知道为什么我的分数不会减少。我认为这是因为我没有调用它但我不知道在哪里调用它。 (抱歉间距太小了。他们在学校从不检查)

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 方法困住了。有人在类里面告诉我,我没有调用那个方法,但我不知道他们的意思。他们告诉我在加法和乘法中调用它,但你如何/在哪里这样做?

最佳答案

当您的同学告诉您,您没有调用 reduce 方法时,他们的意思是您从未使用 reduce 方法。

您的添加方法应该看起来像这样:

 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!

编辑:添加了将分数打印为带分数的代码。

关于Java 分数计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34329083/

相关文章:

java - 使用 time4j 获取该月的天数

java - 在 Java 中初始化复杂静态成员的最佳方法是什么?

c++ - 使用命令行参数 C++ 的简单计算器

java - 无法与 .class 文件交互

c++ - Jgrasp C++ 编译问题

java - 如何使用java设置文件的只读访问权限

java - keystore 中私钥和签名证书之间的链接

c++ - 基本 C++ 计算器

Java 简单计算器

java - 为什么我的 Java 代码在 Jgrasp 中的这一特定代码行中的 if else 语句给我带来了这么多麻烦?