创建添加脚和英寸java的添加方法

时间:2013-10-07 06:30:22

标签: java methods add units-of-measurement

我需要创建一个add方法来添加用户输入的英尺和英寸。用户可以输入任何以英尺或英寸为单位的整数,然后将其取回,但我需要添加值。我不确定如何处理这个问题。  任何帮助都会很棒!

以下是我的代码:

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

程序运行,我只需要一个添加方法。

1 个答案:

答案 0 :(得分:0)

您应该使用System.in.read()来读取标准输入。您可以在以下帖子中找到更多信息:What does System.in.read actually return?