【问题标题】:Parsing string to accept other inputs解析字符串以接受其他输入
【发布时间】:2013-10-06 02:51:53
【问题描述】:

一般信息。

混合数是以下三种形式之一:

1).一个整数,比如12。

2).Fraction = int/int 例如¾

3. 形式 1 和 2 的混合:1 ¾ 在这种情况下,多个空格之一充当整数和分数之间的分隔符。

问题: 1.将第一种和第二种形式作为特殊输入,并增强方法解析以包括这两种情况。

* **我已经完成了混合数的第三种形式我对如何解析字符串以在 Mix 中的混合数旁边包含整数和分数感到困惑.java 。输出应为 gcd 格式。以下三个程序一起运行。请帮助我。谢谢。

这是我的混合数字代码:

import java.util.Scanner;
class Mix extends Fraction{
  public Mix(int n, int m) {super(n,m); }

  public String displayMix() {
     String str="";
     if (first < second) str=first+"/"+second;
     else str= first/second +" "+ first%second+"/"+second;
     return str;


  }//display


  public Mix(String str) {
     int[] iA= parse (str);
     int top=iA[0]*iA[2]+iA[1];
     int bot= iA[2];
     int gcd = gcd(top,bot);
     first=top/gcd;
     second =bot/gcd;
  }//Mix

  public static Mix add (Mix s, Mix s2){
     int gtop=s.first * s2.second
        + s2.first * s.second;
     int gbottom= s.second * s2.second;
     return (new Mix(gtop,gbottom));
  }//add

  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 pos = userInput.indexOf(" ");  

    String sNum=userInput.substring(0,pos);
     int iNum = Integer.parseInt(sNum);//first integer

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

     String sBot=sNum2.substring(pos+1);
     int 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 mixed-format number :");
     String userInput = Mix.get();
     System.out.println("Input is: "+userInput);

     Mix s = new Mix(userInput);
     s.displayMix();

     System.out.print("Please enter mixed-format number :");

     userInput = Mix.get();
     System.out.println("Input is: "+userInput);
     Mix s2 = new Mix(userInput);
     s2.displayMix();

     Mix h= Mix.add(s,s2);
     System.out.print(h.displayMix());

   }//main
  }//class

这是分数代码:

    public class Fraction extends Pair {
   //attributes: NONE

  public Fraction() { first=0; second=1;}

  public Fraction(int n, int m) {
     super(n,m);
     int g=gcd(n,m);
     first = first/g;
     second=second/g;
    }//Fraction

  public String display2() {
    String str = first+"/"+second;
      return str;

  }//display

  public static Fraction add (Fraction f1, Fraction f2){
     int gtop=f1.first * f2.second
        + f2.first * f1.second;
     int gbottom= f1.second * f2.second;
     return (new Fraction(gtop,gbottom));
  }

  public static int gcd (int n, int m){
     while ( n!=m) {
        if (n>m) n=n-m;
        else m=m-n;
     }//while
     return (n);
  }//gcd

//test the class

  public static void main(String[] args) {
  //pseudo-code is here
     Fraction f= new Fraction();
     f.display();
     System.out.print(f.display2());
  }//main

 } //class

终于来了一对:

  import java.util.Arrays;
 public class Pair {
    int first;
    int second;     

 public Pair (){first=0; second=0;}

 public Pair(int n, int m) {
     first=n;
     second=m;
  }//Pair



  public int[] display() {
  //pseudo_code is here
     int[] c = {first, second};
return c;
  }//display

  public static void main(String[] args) {
  //pseudo-code is here
     Pair f= new Pair();
     f.display();

System.out.println(Arrays.toString(f.display()));
   }//main
 } //class

【问题讨论】:

    标签: java string parsing


    【解决方案1】:

    您必须使用str.split("/") 之类的方法创建自己的方法来做到这一点,我不知道还有其他方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2012-07-13
      • 2018-05-13
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多