Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到构造函数Rational(字符串)_Java - Fatal编程技术网

Java 找不到构造函数Rational(字符串)

Java 找不到构造函数Rational(字符串),java,Java,我的主函数第5行出现错误。。。该行内容如下: System.out.println( "The fractional number is " + myRational.Rational(s) ); 发生的错误告诉我 The method Rational(String) is undefined for the type Assignment13.Rational. 如有任何帮助或建议,将不胜感激 import java.math.BigInteger; import java.util.S

我的主函数第5行出现错误。。。该行内容如下:

System.out.println( "The fractional number is " + myRational.Rational(s) );
发生的错误告诉我

The method Rational(String) is undefined for the type Assignment13.Rational.
如有任何帮助或建议,将不胜感激

import java.math.BigInteger;
import java.util.Scanner;

public class Assignment13 {

    public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
            System.out.println("Enter a decimal number: ");
            String s = input.next();
           Rational myRational = new Rational();
            System.out.println( "The fractional number is " + myRational.Rational(s) );

    }

 public class Rational extends Number implements Comparable<Rational> {


    // Data fields for numerator and denominator
    private BigInteger numerator = BigInteger.ZERO;
    private BigInteger denominator = BigInteger.ONE;

    /** Construct a rational with default properties */
    public Rational() {
      this(BigInteger.ZERO,BigInteger.ONE);
    }

    /** Construct a rational with specified numerator and denominator */
    public Rational(BigInteger numerator, BigInteger denominator) {
      BigInteger gcd = gcd(numerator, denominator);
      this.numerator= (denominator.compareTo(BigInteger.ZERO) > 0 ? BigInteger.ONE : 
         new BigInteger("-1")).multiply(numerator.divide(gcd));
      this.denominator = denominator.divide(gcd);
    }

    public Rational(String decimal) {

        int index = (decimal.contains(".")) ? decimal.indexOf('.') : decimal.indexOf('/');
        BigInteger d;
        BigInteger n;
        // if string is in decimal form
        if (decimal.contains(".")) {
            int power = decimal.substring(index + 1, decimal.length()).length();
            d = new BigInteger ("Math.pow(10,power)");
            n = new BigInteger(new StringBuilder(decimal).deleteCharAt(index).toString());
        } else {
            // if string contains '/'
            n = new BigInteger(decimal.substring(0, index));
            d = new BigInteger(decimal.substring (index + 1, decimal.length()));
        }

        BigInteger gcd = gcd(n, d);
        this.numerator = ((d.compareTo(BigInteger.ZERO) > 0) ? BigInteger.ONE : new BigInteger("-1")).multiply(n).divide(gcd);
        this.denominator = d.abs().divide(gcd);
        }

   /** Find GCD of two numbers */
   private  BigInteger gcd(BigInteger n, BigInteger d) {
       BigInteger n1 = n.abs();
       BigInteger n2 = d.abs();
       BigInteger gcd = BigInteger.valueOf(1);

     for (BigInteger k = BigInteger.valueOf(1); k.compareTo(n1) <= 0 && k.compareTo(n2) <= 0; k.add(BigInteger.ONE)) {
       if (n1.mod(k) == BigInteger.ZERO && n2.mod(k) == BigInteger.ZERO)
         gcd = k;
     }

     return gcd;
   }

   /** Return numerator */
   public BigInteger getNumerator() {
     return numerator;
   }

   /** Return denominator */
   public BigInteger getDenominator() {
     return denominator;
   }

   /** Add a rational number to this rational */

public Rational add(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).add(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
     return new Rational(n, d);
   }

   /** Subtract a rational number from this rational */

public Rational subtract(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
     return new Rational(n, d);
   }

   /** Multiply a rational number by this rational */


public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
     return new Rational(n, d);
   }

   /** Divide a rational number by this rational */


public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
     return new Rational(n, d);
   }

   @Override
   public String toString() {
     if (denominator.compareTo(BigInteger.ONE) == 0)
       return numerator + "";
     else
       return numerator + "/" + denominator;
   }

   @Override // Override the equals method in the Object class
   public boolean equals(Object other) {
    if (((this.subtract((Rational)(other))).getNumerator()).compareTo(BigInteger.ZERO) == 0)
       return true;
     else
       return false;
   }

   @Override // Implement the abstract intValue method in Number
   public int intValue() {
     return (int)doubleValue();
   }

   @Override // Implement the abstract floatValue method in Number
   public float floatValue() {
     return (float)doubleValue();
   }

   public BigInteger bigIntegerValue() {
       return numerator.multiply(new BigInteger("1").divide(denominator));
   }

   @Override // Implement the doubleValue method in Number
   public double doubleValue() {
       return numerator.divide(denominator).doubleValue();
  }

  @Override // Implement the abstract longValue method in Number
  public long longValue() {
    return (long)doubleValue();
  }

  @Override // Implement the compareTo method in Comparable
  public int compareTo(Rational o) {
    if (this.subtract(o).getNumerator().compareTo(BigInteger.ZERO) > 0)
      return 1;
    else if (this.subtract(o).getNumerator().compareTo(BigInteger.ZERO) < 0)
      return -1;
    else
      return 0;
  }
}
    }
import java.math.biginger;
导入java.util.Scanner;
公共课堂作业13{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入十进制数:”);
字符串s=input.next();
Rational myRational=新的Rational();
System.out.println(“分数是”+myRational.Rational(s));
}
公共类Rational扩展了一些可比较的实现{
//分子和分母的数据字段
私有BigInteger分子=BigInteger.0;
私有BigInteger分母=BigInteger.ONE;
/**构造具有默认属性的rational*/
公共理性{
这个(biginger.ZERO,biginger.ONE);
}
/**用指定的分子和分母构造有理数*/
公共有理数(大整数分子,大整数分母){
BigInteger gcd=gcd(分子、分母);
this.numerator=(分母.compareTo(BigInteger.ZERO)>0?BigInteger.ONE:
新的大整数(“-1”)。乘法(分子除法(gcd));
此分母=分母除法(gcd);
}
公共Rational(字符串十进制){
int index=(decimal.contains(“.”)decimal.indexOf('.'):decimal.indexOf('/');
大整数d;
大整数n;
//如果字符串是十进制形式
if(十进制包含(“.”){
整数幂=decimal.substring(索引+1,decimal.length()).length();
d=新的大整数(“数学功率(10,幂)”;
n=新的BigInteger(新的StringBuilder(十进制).deleteCharAt(索引).toString());
}否则{
//如果字符串包含“/”
n=新的BigInteger(十进制子字符串(0,索引));
d=新的BigInteger(decimal.substring(index+1,decimal.length());
}
大整数gcd=gcd(n,d);
this.numerator=((d.compareTo(biginger.ZERO)>0)?biginger.ONE:新的biginger(“-1”)。乘(n)。除(gcd);
该分母=d.abs().divide(gcd);
}
/**查找两个数字的GCD*/
专用BigInteger gcd(BigInteger n,BigInteger d){
BigInteger n1=n.abs();
大整数n2=d.abs();
BigInteger gcd=BigInteger.valueOf(1);

对于(biginger k=biginger.valueOf(1);k.compareTo(n1)您将
字符串s
传递给类
Assignment13.Rational
的构造函数,并从一个实例
myRational
调用它,该实例是有效的。没有理由创建一个新对象,也没有理由为同一事物使用另一个对象

自变量声明后,
myRational
已经是
Rational
。如果要从
字符串s
打印新创建的对象,请执行以下操作:

System.out.println("The fractional number is " + new Assignment13().new Rational(s));
或者最好将类设置为静态,然后通过以下方式访问:

  • newrational(s))
    在同一类中
  • newassignment13.Rational(s)
    来自所有类,因为它们是
    public
在这两种情况下,您都可以省略该行:

Rational myRational = new Rational();        // TODO: remove

我建议您在默认情况下覆盖打印到控制台时使用的方法。

您想用
myRational.Rational做什么?
?您不能(afaik)在现有实例上调用构造函数。如果您试图调用构造函数,它应该是
new Rational(s)
。构造函数不是方法。语法是
new Rational
,只有当您将嵌套类
设置为静态时,它才会起作用。