Java:菜单接受输入然后退出程序

时间:2015-12-05 23:04:55

标签: java methods menu

所以,问题是我的程序通过收集两个整数进入理性课程的过程很顺利,但是一旦进入菜单,它就会在做出选择后退出。我不知道为什么也没有人帮忙。

编辑:经过大幅度的摆弄,我设法至少让系统给我一些输出:也就是说,不管我输入的数字是什么,都给了我无效的输出。我想也许我应该回到开关/案例?

以下是代码:

public class Rational
{

  private int numerator;     // instance variable for the numerator
  private int denominator;   // instance variable for the denominator

  public Rational()          // Constructor
  {
    numerator = 0;
    denominator = 1;
  }

  public Rational(int numer, int denom) // Overloaded Constructor
  {
    numerator = numer;
    denominator = denom;
    normalize();
  }

  public void setNumer(int numer) // Setter for the Numerator value
  {
    numerator = numer;
  }

  public void setDenom(int denom) // Setter for the Denominator value
  {
    denominator = denom;
  }

  public void normalize()
  {
    if(denominator < 0)
    {
      denominator = -denominator;
      numerator = -numerator;
    }
  }

  public int getNumer()    // Numerator Getter
  {
    return numerator;
  }

  public int getDenom()    // Denominator Getter
  {
    return denominator;
  }

  public void add(Rational second)      // Addition method
  {
    int a = this.getNumer();
    int b = this.getDenom();
    int c = second.getNumer();
    int d = second.getDenom();
    numerator = ((a * d) + (b * c));
    denominator = (b * d);
    this.normalize();
  }

  public void subtract(Rational second) // Subtraction Method
  {
    int a = this.getNumer();
    int b = this.getDenom();
    int c = second.getNumer();
    int d = second.getDenom();
    numerator = ((a * d) - (b * c));
    denominator = (b * d);
    this.normalize();    
  }

  public void multiply(Rational second) // Multiplication Method
  {
    int a = this.getNumer();
    int b = this.getDenom();
    int c = second.getNumer();
    int d = second.getDenom();
    numerator = (a * c);
    denominator = (b * d);
    this.normalize();    
  }

  public void divide(Rational second)  // Division Method
  {
    int a = this.getNumer();
    int b = this.getDenom();
    int c = second.getNumer();
    int d = second.getDenom();
    numerator = (a * d);
    denominator = (b * c);
    this.normalize();
  }

  public boolean equals(Object second) // Equals method
  {
    if(second == null)
    {
      return false;
    }
    if(!(second instanceof Rational))
    {
      return false;
    }
    Rational other = (Rational)second;
    return this.getNumer() * other.getDenom() == other.getNumer() * this.getDenom();
  }

  public String toString()  // toString method
  {
    if(numerator == 0)
    {
      return "Result = 0";
    }
    if(denominator == 1)
    {
      return Integer.toString(numerator);
    }
    return Integer.toString(numerator) + "/" + Integer.toString(denominator);
  }

}

这是我所做的Rational课程:

{{1}}

0 个答案:

没有答案