2015-11-08 79 views
0

我一直在試圖讓我的程序正常工作,雖然由於某種原因它一直沒有工作。我幾乎可以肯定,一切都運作完畢,直到它到達if循環來實際執行計算。出於某種奇怪的原因,當我做op.equals(plus)或者我想測試的操作時,它只是不承認它們是平等的。我嘗試了無數變化,看看他們是否會工作,但我仍然不成功。我真的很感激,如果任何人都可以看看我的代碼,並可能修正或修復它。我的Java分數計算器拒絕與我合作

public class FracCalc { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int repeat = 1; 
     while (repeat == 1) { 
      System.out.println("Input your equation"); 
      String equation = input.nextLine(); // Takes the user input and sets it equal to a variable (equation) 
      produceAnswer(equation); 
      if(equation.equals("quit")) { // If the user inputs 'quit' 
       System.out.println("You've quit out of the calculator."); 
       repeat = 2; // repeat is set to 2 so the program can no longer run and it terminates. 
      } 
     } 
    } 
    public static String produceAnswer(String input) { 

     Scanner fraction = new Scanner(input); 
     String num1 = fraction.next(); 
     String op = fraction.next(); 
     String num2 = fraction.next(); 
     // Creates an empty string for each necessary variable for values to be added to them later on. 
     String plus = "+"; 
     String multiply = "*"; 
     String division = "/"; 
     String subtract = "-"; 
     String whole = ""; 
     String numer = ""; 
     String denom = ""; 
     String whole2 = ""; 
     String numer2 = ""; 
     String denom2 = ""; 
     // Creates variables ansnum and ansden to be manipulated later on in the calculations. 
     int ansnum = 0; 
     int ansden = 0; 
     // Creates place and place2 to act as counters to go through each token of the original equation input. 
     int place = 0; 
     int place2 = 0; 
     String input1 = ""; // Sets an empty string variable for the return. 
     if(num1.contains("_")) { // If the first fraction contains an _ 
      while(num1.charAt(place) != '_') { // while the place (index) isn't equal to that _ 
       whole += num1.charAt(place); // Set everything before it, equal to whole. 
       place++; // Each time increase the place (index) by 1. 
      } 
      place++; // Increases the place by 1 to pass the _ and move onto the next part of the fraction 
      if(num1.contains("/")) { // If the first fraction contains a forward slash 
       while(num1.charAt(place) != '/') { // While the place does not equal its index 
        numer += num1.charAt(place); // Add every thing before it to a variable called numer 
        place++; // Each time increases the place (index) by 1. 
       } 
      place++; // Increases the place by 1 to pass the/and move onto the denominator. 
      } 
      while(place != num1.length()) { // While the place (index) is less than the length of fraction 1 
       denom += num1.charAt(place); // Add every thing before it equal to the denom variable. 
       place++; // Each time increases the place (index) by 1. 
      } 
      place++; 
      // Same process as above is repeated for fraction 2. 
      if(num2.contains("_")) { 
       while(num2.charAt(place2) != '_') { 
        whole2 += num2.charAt(place2); 
        place2++; 
       } 
       place2++; 
       if(num2.contains("/")) { 
        while(num2.charAt(place2) != '/') { 
         numer2 += num2.charAt(place2); 
         place2++; 
        } 
       place2++; 
       } 
      while(place2 != num2.length()) { 
       denom2 += num2.charAt(place2); 
       place2++; 
      } 
     } 
     // All those string variables with the values are converted into integers to be used in calculations. 
     int wholenum = Integer.parseInt(whole); 
     int wholenum2 = Integer.parseInt(whole2); 
     int numerator1 = Integer.parseInt(numer); 
     int denominator1 = Integer.parseInt(denom); 
     int numerator2 = Integer.parseInt(numer2); 
     int denominator2 = Integer.parseInt(denom2); 

     // Calculates the whole number into the fraction to get rid of any mixed numbers. 
     int wholenumerator1 = ((wholenum * denominator1) + numerator1); 
     int wholenumerator2 = ((wholenum2 * denominator2) + numerator2); 

     if(op.equals(plus)) { // If the operator is addition 
      ansnum = ((wholenumerator1 * denominator2) + (denominator1 * wholenumerator2)); // Cross multiply to get the numerator. 
      ansden = (denominator1 * denominator2); // Multiply both denominators to get a common denominator. 
      input1 = (ansnum + "/" + ansden); 
      System.out.println(input1); 
      } 
     else if(op.equals("multiply")) { // If the operator is multiplication 
      ansnum = (wholenumerator1 * wholenumerator2); // multiples both numerators. 
      ansden = (denominator1 * denominator2); // multiplies both denominators 
      input1 = (ansnum + "/" + ansden); 
      System.out.println(input1); 
      } 
     else if(op.equals("division")) { // If the operator is division 
      ansnum = (wholenumerator1 * denominator2); // multiplies numerator1 by the reciprocal of fraction 2 (denominator2) 
      ansden = (denominator1 * wholenumerator2); // multiplies denominator 1 by numerator2. 
      input1 = (ansnum + "/" + ansden); 
      System.out.println(input1); 
      } 
     else if(op.equals("subtract")) {  
      ansnum = ((wholenumerator1 * denominator2) - (wholenumerator2 * denominator1)); // multiplies numerator1 by denominator 2 and numerator 2 by denominator 1 
      ansden = (denominator1 * denominator2); // multiples both denominators to find a common denominator 
      input1 = (ansnum + "/" + ansden); 
      System.out.println(input1); 
     } 
    } 
     return input1; 
} 

}

+3

你試過調試代碼 – bakki

+0

是的,我做過。從技術上講,它沒有錯。它只是不會通過if循環運行,所以它跳過它們,只是返回input1,因爲input1的值只在if循環中更新,它仍然是「」。 – Idontlikejava

+0

爲什麼你在使用''時,你應該使用「」。 –

回答

0

在這裏你去,

import java.util.Scanner; 

public class FracCalc { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int repeat = 1; 
     while (repeat == 1) { 
      System.out.println("Input your equation"); 
      String equation = input.nextLine(); // Takes the user input and sets it equal to a variable (equation) 
      if (equation.equals("quit")) { // If the user inputs 'quit' 
       System.out.println("You've quit out of the calculator."); 
       repeat = 2; // repeat is set to 2 so the program can no longer run and it terminates. 
      } else { 
       System.out.println("Your answer is: " + produceAnswer(equation));//EDIT 
      } 
     } 
    } 

    public static String produceAnswer(String input) { 

     Scanner fraction = new Scanner(input); 
     String num1 = fraction.next().trim(); 
     String op = fraction.next().trim(); 
     String num2 = fraction.next().trim(); 
     // Creates an empty string for each necessary variable for values to be added to them later on. 
     String plus = "+"; 
     String multiply = "*"; 
     String division = "/"; 
     String subtract = "-"; 
     String whole = ""; 
     String numer = ""; 
     String denom = ""; 
     String whole2 = ""; 
     String numer2 = ""; 
     String denom2 = ""; 
     // Creates variables ansnum and ansden to be manipulated later on in the calculations. 
     int ansnum = 0; 
     int ansden = 0; 
     // Creates place and place2 to act as counters to go through each token of the original equation input. 
     int place = 0; 
     int place2 = 0; 
     String input1 = ""; // Sets an empty string variable for the return. 
     if (num1.contains("_")) { // If the first fraction contains an _ 
      while (num1.charAt(place) != '_') { // while the place (index) isn't equal to that _ 
       whole += num1.charAt(place); // Set everything before it, equal to whole. 
       place++; // Each time increase the place (index) by 1. 
      } 
      place++; // Increases the place by 1 to pass the _ and move onto the next part of the fraction 
     }//EDIT 
     if (num1.contains("/")) { // If the first fraction contains a forward slash 
      while (num1.charAt(place) != '/') { // While the place does not equal its index 
       numer += num1.charAt(place); // Add every thing before it to a variable called numer 
       place++; // Each time increases the place (index) by 1. 
      } 
      place++; // Increases the place by 1 to pass the/and move onto the denominator. 
     }//EDIT 
     while (place != num1.length()) { // While the place (index) is less than the length of fraction 1 
      denom += num1.charAt(place); // Add every thing before it equal to the denom variable. 
      place++; // Each time increases the place (index) by 1. 
     } 
     place++; 
     // Same process as above is repeated for fraction 2. 
     if (num2.contains("_")) { 
      while (num2.charAt(place2) != '_') { 
       whole2 += num2.charAt(place2); 
       place2++; 
      } 
      place2++; 
     }//EDIT 
     if (num2.contains("/")) { 
      while (num2.charAt(place2) != '/') { 
       numer2 += num2.charAt(place2); 
       place2++; 
      } 
      place2++; 
     }//EDIT 
     while (place2 != num2.length()) { 
      denom2 += num2.charAt(place2); 
      place2++; 
     } 

     // All those string variables with the values are converted into integers to be used in calculations. 
     int wholenum = (whole.equals("")) ? 0 : Integer.parseInt(whole);//EDIT 
     int wholenum2 = (whole2.equals("")) ? 0 : Integer.parseInt(whole2);//EDIT 
     int numerator1 = Integer.parseInt(numer); 
     int denominator1 = Integer.parseInt(denom); 
     int numerator2 = Integer.parseInt(numer2); 
     int denominator2 = Integer.parseInt(denom2); 

     // Calculates the whole number into the fraction to get rid of any mixed numbers. 
     int wholenumerator1 = ((wholenum * denominator1) + numerator1); 
     int wholenumerator2 = ((wholenum2 * denominator2) + numerator2); 

     if (op.equals(plus)) { // If the operator is addition 
      ansnum = ((wholenumerator1 * denominator2) + (denominator1 * wholenumerator2)); // Cross multiply to get the numerator. 
      ansden = (denominator1 * denominator2); // Multiply both denominators to get a common denominator. 
     } else if (op.equals(multiply)) { // If the operator is multiplication 
      ansnum = (wholenumerator1 * wholenumerator2); // multiples both numerators. 
      ansden = (denominator1 * denominator2); // multiplies both denominators 
     } else if (op.equals(division)) { // If the operator is division 
      ansnum = (wholenumerator1 * denominator2); // multiplies numerator1 by the reciprocal of fraction 2 (denominator2) 
      ansden = (denominator1 * wholenumerator2); // multiplies denominator 1 by numerator2. 
     } else if (op.equals(subtract)) { 
      ansnum = ((wholenumerator1 * denominator2) - (wholenumerator2 * denominator1)); // multiplies numerator1 by denominator 2 and numerator 2 by denominator 1 
      ansden = (denominator1 * denominator2); // multiples both denominators to find a common denominator 
     } 
     input1 = (ansnum + "/" + ansden);//EDIT 
     return input1; 
    } 
} 

我已標記的變化爲 「//編輯」。

如果這可行,請將其標記爲解決方案。

+0

完美的工作,非常感謝你作出更正。我的下一步是用另一個整數或分數計算整數。我感謝您的幫助! – Idontlikejava

1

提示:如果您使用的是Eclipse,強調一切按Ctrl-i正確縮進代碼。你會看到你的錯誤。

此外,不要重複== 1作爲條件,然後切換repeat = 2,只需使用帶有break語句的無限while循環。

+0

我使用控件i更正了縮進,並且還將末端大括號放在了正確的位置,但程序仍然只在用戶輸入混合數字時才起作用。我似乎無法弄清楚爲什麼用戶輸入分數時不起作用。 – Idontlikejava