2013-10-07 41 views
0

我需要創建添加方法來添加由用戶輸入的英尺和英寸。用戶可以輸入英尺或英寸的任何整數,然後將其取回,但我需要添加值。我不完全確定我該如何解決這個問題。 任何幫助將是偉大的!創建添加方法來添加英尺和英寸java

下面是我的代碼:

import java.util.Scanner; 
public class MixWithUnit_JM { 
Fraction_JM a; 
int first; 
int second; 

public String displayMixWithUnit_JM() { 
    String str=""; 
    if (first == 0) str= first%second+"/"+second+"in"; 
    else if (first%second == 0) str= ""+first/second+"ft"; 
    else if (first < second) str=first+"/"+second+"in"; 
    else str= first/second+"ft" +" "+ first%second+"/"+second+"in"; 
    return str; 
}//display 


public MixWithUnit_JM(String str) { 
    int pos = str.indexOf("ft"); 
    int pos2 = str.indexOf("in"); 
    String a1 = ""; 
    String a2 =""; 
    String b1 = ""; 
    String b2 =""; 
    String b3 =""; 

    if(pos == -1){ 
    a2 = str.substring(0,pos2 +2).trim(); 
    b2 = str.substring(0,pos2); 
    b3 = b2; 
    } 
    else if(pos2 == -1){ 
    a1 = str.substring(0,pos +2); 
    b1 = str.substring(0, pos); 
    b3 = b1; 
    } 
    else{ 
    a1 = str.substring(0,pos +2); 
    b1 = str.substring(0, pos); 
    a2 = str.substring(pos +2).trim(); 
    b2 = str.substring(pos+2, str.length()-2); 
    b3 = b1 + b2; 
    } 

    int[] iA= parse (b3); 
    int top=iA[0]*iA[2]+iA[1]; 
    int bot= iA[2]; 
    a = new Fraction_JM (top,bot); 
    int gcd = a.gcd(top,bot); 
    first=top/gcd; 
    second =bot/gcd; 
}//Mix 




public static String get(){ 
    Scanner scan = new Scanner (System.in); 
    String userInput = scan.nextLine(); 
    userInput =userInput.trim(); 
    return (userInput); 

} //get 


public static int[] parse (String userInput){ 
    int iNum = 0; 
    int iTop = 0; 
    int iBot = 1; 

    userInput = userInput.trim(); 
    int pos = userInput.indexOf(" "); 
    int pos2 = userInput.indexOf("/"); 

    if (pos == -1 && pos2 != -1){ 
    pos= userInput.indexOf("/"); 
    String sTop=userInput.substring(0,pos); 
    iTop = Integer.parseInt(sTop);//second integer 
    String sBot=userInput.substring(pos+1); 
    iBot = Integer.parseInt(sBot);//third integer 
    //case 2 

    } 
    else if (pos == -1 && pos2== -1) { 
    iNum = Integer.parseInt(userInput);//first integer 
    //case 1 
    } 
    else{ 
    String sNum=userInput.substring(0,pos); 
    iNum = Integer.parseInt(sNum);//first integer 

    String sNum2=userInput.substring(pos+1); 
    pos= sNum2.indexOf("/"); 
    String sTop=sNum2.substring(0,pos); 
    iTop = Integer.parseInt(sTop);//second integer 

    String sBot=sNum2.substring(pos+1); 
    iBot = Integer.parseInt(sBot);//third integer 
    } 
    int[] sA = {iNum,iTop,iBot}; 
    return (sA);   
} //parse 


public static void main(String[] args) { 
    System.out.print("Please enter a mixed-format number :"); 
    String userInput = MixWithUnit_JM.get(); 
    System.out.println("Input is: "+userInput); 
    MixWithUnit_JM s = new MixWithUnit_JM(userInput); 
    s.displayMixWithUnit_JM(); 

    System.out.print("Please enter a mixed-format number :"); 
    userInput = MixWithUnit_JM.get(); 
    System.out.println("Input is: "+userInput); 
    MixWithUnit_JM s2 = new MixWithUnit_JM(userInput); 
    s2.displayMixWithUnit_JM(); 


}//main 
}//class 

程序運行時,所有我需要的是一個add方法。

回答

相關問題