java - 如何在我的程序中显示“以英寸为单位”

标签 java

感谢您尝试帮助我。

我正在开发的程序应该询问人们以英尺和英寸为单位的输入

2' 2 2/2" <---(此格式)

但是,我无法想出一种支持此类用户输入的方法。

该程序由三个类组成

  • 分数
  • 配对
  • 混合

Java:

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+ "'" + first%second +"/"+second+"\"";
    else 
    str=first+"'" +(first+second)+"/"+second+"\"";

    return str; 
  } //display

  public static String get () {
    Scanner scan = new Scanner(System.in);
    System.out.print("Please enter a mixed-format number:");
    String userInput = scan.nextLine();
    userInput = userInput.trim();
    System.out.println("Input is: "+ userInput);
    return (userInput);
  } //get


  public static void main(String [] args) {
    String userInput;
    int[]iA = {0,0,0,1};

    userInput = Mix.get();
    iA=parse(userInput);
    Mix f = new Mix ( iA[0] , (iA[1]*iA[2])+(iA[3]) );
    System.out.println("Number = " + f.displayMix());

    userInput = Mix.get();
    iA=parse(userInput);
    Mix g = new Mix ( iA[0] , iA[1] * iA[3] + iA[2]);
    System.out.println("Number = " + g.displayMix());

    Mix h = Mix.add (f,g);
    System.out.println("Sum ="+ h.displayMix());
  } // main


  public static Mix add (Mix f, Mix g) {
    int gtop=       f.first+g.first;

    int gbottom=    f.second+g.second;
    return(new Mix ( gtop, gbottom));
  } //add


  public static int[] parse (String userInput) {
    int [] sA = {0,0,0,1};
    int pos = userInput.indexOf("'");

    String sNum0 = userInput.substring(0,pos);
    pos = userInput.indexOf("'");
    sA[0]= Integer.parseInt(sNum0);

    String sNum = userInput.substring(0,pos);
    pos = sNum.indexOf("  ");
    sA[1]= Integer.parseInt(sNum);

    String sNum2 = userInput.substring(pos + 1);
    pos = sNum2.indexOf("/");

    String sTop= sNum2.substring (pos +3);
    pos = sNum2.indexOf("\"");
    sA[2] = Integer.parseInt(sTop);

    String sBot = sNum2.substring(pos +1);

    sA[3] = Integer.parseInt(sBot);

    // 2' 2 2/2
    return (sA);
  }
} //parse

输入后我已经成功得到结果

2' 2 2/2"1

我不知道我做错了什么,但有些东西不对劲,因为我需要“后面有一个数字才能得到结果。

我很确定解析方法有问题,我只是不知道它在哪里。

这是分数类

public class Fraction extends Pair {
  //attributes: NONE

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


  public void display2() {
    System.out.print(first);
    System.out.print("/");
    System.out.println(second);
  } //display


  public 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
} //class

这是 Pair 类

public class Pair {
  int first;
  int second;

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


  public void display() {
    //pseudo_code is here
    System.out.print("First integer="+first);
    System.out.println();
    System.out.println("Second integer="+second);
  } //display


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

最佳答案

I am pretty sure that something is wrong with the parsing method, i just dont know where it is.

当然。

出于调试目的,最好在解析的情况下使用不同的值。对于输入字符串 2' 2 2/2",如果尝试打印输出,可能会收到误报。向 parse 方法提供新的输入字符串 "1' 2 3/4"5" 会生成 int[] sA1 1 5 5 数组。现在来查找问题。

int pos = userInput.indexOf("'");

String sNum0 = userInput.substring(0, pos);
pos = userInput.indexOf("'"); //why getting a position you already have?
sA[0] = Integer.parseInt(sNum0);

该代码片段用于检索第一个数字。从我提供的评论中可以明显看出一个奇怪的地方。接下来问题开始,

 String sNum = userInput.substring(0, pos);
 pos = sNum.indexOf("  ");
 sA[1] = Integer.parseInt(sNum);

您再次从输入中获得相同的 2,然后尝试在 1 个字符长度的字符串中查找双空格。您的 pos 变为 -1,并且您复制的数据分配 sA[0]sA[1] 具有相同的值。

String sNum2 = userInput.substring(pos + 1);
pos = sNum2.indexOf("/");

接下来,由于您的 pos 现在是 -1,您可以对整个 userInput 进行子串,从而得到 sNum2 和 userInput 本质上是 .equals() 彼此。 pos 现在等于 6。

String sTop = sNum2.substring(pos + 3);
pos = sNum2.indexOf("\"");
sA[2] = Integer.parseInt(sTop);

pos + 3 是您必须输入额外输入字符的原因。

我不会详细介绍如何以及在何处解决问题,因为有多种解决方案。但是,如果您被允许使用 .split() 方法,我强烈建议您利用它。

 String[] tokens = "1' 2 3/4\"".split(" ");

关于java - 如何在我的程序中显示“以英寸为单位”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19204467/

相关文章:

java - EAR 在 jboss-as-7.1.1.Final 中自动取消部署

java - 阶梯式组合框刷新

java - 如何在基于 MySQL 的应用程序中有效地存储用户的多个不同计数器值?

java - Spring boot JPA @Query 与 like 和 in

java - 在Eclipse中运行mapreduce程序时出错

java - 混合 C++/Qt/Java 应用程序

java - ImageIO 无法写入 JPEG 文件

java - 如何使 JTextField 在按钮单击后出现,并且只接受与另一个字符串相同长度的字符 - Java GUI

java - IsFlush boolean 值

java - 在 IntelliJ 中使用数据源、JNDI API