0

我需要创建一个 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

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

4

1 回答 1

0

您应该使用System.in.read()从标准输入中读取。您可以在这篇文章中找到更多信息:System.in.read 实际返回什么?

于 2013-10-07T07:07:15.100 回答