gpt4 book ai didi

Java - 打印对象的内容

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:08 25 4
gpt4 key购买 nike

我正在尝试打印我的对象 Fraction() 的内容,但它总是给我很多错误,并让我回到打印语句开始的地方。我创建了一个 toString() 方法,但它似乎也不起作用。这是我的类(class)和测试员类(class):

 package fraction;

public class Fraction
{
/**
* The numerator and denominator.
*/
private int top;
private int bottom;

/**
* Creates the fraction 0/1
*/
public Fraction()
{
top = 0;
bottom = 1;
}

/**
* Creates the fraction n/1
* @param n an integer
*/
public Fraction(int n)
{
top = n;
bottom = 1;
}

/**
* Creates a fraction with the specified numerator and denominator
* @param n the numerator
* @param d the denominator
*/
public Fraction(int n, int d)
{
top = n;
bottom = d;

if (bottom == 0) {
throw new IllegalArgumentException();
}
}

/**
* Computes the sum of this fraction and the specified fraction
* @param f a fraction
* @return the fraction obtained by adding this fraction from the specified fraction
*/
public Fraction add(Fraction f)
{
return new Fraction(this.numerator() * f.denominator() + f.numerator() * this.denominator());
}

/**
* Compares this fraction and the specified fraction
* @param f a fraction
* @return 0 when this fraction is equal to the specified fraction; 1 when this fraction is greater than the specified
* fraction; -1 otherwise
*/
public int compareTo(Fraction f)
{
if (top > f.numerator())
return 1;
else if (top < f.numerator())
return -1;
else if (bottom > f.denominator())
return 1;
else if (bottom < f.denominator())
return -1;
else
return 0;
}

/**
* Gives the denominator of this fraction
* @return the denominator of this fraction
*/
public int denominator()
{
return bottom;
}

/**
* Gives the quotient of this fraction and the specified fraction
* @param f a fraction
* @return the fraction obtained by dividing this fraction by the specified fraction
* @throws IllegalArgumentException when the numerator equals 0
*/
public Fraction divide(Fraction f)
{
if (f.numerator() == 0) {
throw new IllegalArgumentException();
}
else
{
return new Fraction(this.numerator() * f.numerator(), this.denominator() * f.denominator());
}
}

/**
* Determines whether this fraction is equal to the specified fraction
* @param f a fraction
* @return true when this fraction is equal to the specified fraction; otherwise, false
*/
public boolean equals(Fraction f)
{
return top == f.numerator() && bottom == f.denominator();
}

/**
* Computes the greatest common divisor of the specified numbers
* @param num1 an integer
* @param num2 an integer
* @return the greatest common divisor of the specified parameters
*/
private int gcd(int num1, int num2)
{
if (num2 == 0) {
return num1;
}
else
{
return gcd(num2, num1%num2);
}
}

/**
* Calculates the product of this fraction and the specified fraction
* @param f a fraction
* @return the product of this fraction and the specified fraction
*/
public Fraction multiply(Fraction f)
{
return new Fraction(this.numerator() * f.numerator(), this.denominator() * f.denominator());
}

/**
* Simplifies this fraction by expressing its numerator and denominator in standard form:
* the denominator is positive and the numerator and denominator are relative primes
*/
public void normalize()
{
int gcd = gcd(top,bottom);
top /= gcd;
bottom /= gcd;
}

/**
* Gives the numerator of this fraction
* @return the numerator of this fraction
*/
public int numerator()
{
return top;
}

/**
* Computes the reciprocal of this fraction
* @return the reciprocal of this fraction
* @throws IllegalArgumentException when the numerator is equal to 0
*/
public Fraction reciprocal()
{
if (top == 0) {
throw new IllegalArgumentException();
}

int temp;
temp = numerator();
top = denominator();
bottom = temp;
return new Fraction(top, bottom);
}

/**
* Modifies this fraction
* @param n the new numerator of this fraction
* @param d the new denominator of this fraction
* @throws IllegalArgumentException when the denominator equals 0
*/
public void setFraction(int n, int d)
{
if (d == 0) {
throw new IllegalArgumentException();
}

top = n;
bottom = d;
}

/**
* Computes the difference of this fraction and the specified fraction
* @param f a fraction
* @return the fraction obtained by subtracting this fraction from the specified fraction
*/
public Fraction subtract(Fraction f)
{
return new Fraction(this.numerator() * f.denominator() - f.numerator() * this.denominator(), this.denominator() * f.denominator());
}

/**
* Gives a string representing this fraction in in-line notation, numerator/denominator
* @return a string representing this fraction in in-line notation, numerator/denominator
*/
@Override public String toString()
{
return String.format("%d/%d",top,bottom);
}
}

package fraction;

import java.util.Scanner;

public class FractionTester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.print("Enter the numerator and denominator of f1 > ");
int n1 = in.nextInt(), d1 = in.nextInt();
System.out.print("Enter the numerator and denominator of f2 > ");
int n2 = in.nextInt(), d2 = in.nextInt();
System.out.print("Enter the numerator and denominator of f3 > ");
int n3 = in.nextInt(), d3 = in.nextInt();
System.out.print("Enter the numerator and denominator of f4 > ");
int n4 = in.nextInt(), d4 = in.nextInt();
System.out.println();

Fraction f1 = new Fraction(n1, d1);
Fraction f2 = new Fraction(n2, d2);
Fraction f3 = new Fraction(n3, d3);
Fraction f4 = new Fraction(n4, d4);

System.out.printf("f1 = %d", f1);
System.out.printf("f2 = %d", f2);
System.out.printf("f3 = %d", f3);
System.out.printf("f4 = %d", f4);

if (f1.compareTo(f2) == 0) {
System.out.println("f1 = f2");
}
else if (f1.compareTo(f2) < 0) {
System.out.println("f1 < f2");
}
else {
System.out.println("f1 > f2");
}

if (f3.compareTo(f4) == 0) {
System.out.println("f1 = f2");
}
else if (f3.compareTo(f4) < 0) {
System.out.println("f1 < f2");
}
else {
System.out.println("f1 > f2");
}
System.out.println();


}
}

我正在尝试打印对象 f1、f2、f3 和 f4 的内容。它说我的错误出现在第一个打印语句 (System.out.printf("f1 = %d", f1);) 上。

最佳答案

您的分数不是数字 (%d),而是一个 Object,应该调用 toString() 方法。将 %d 更改为 %s 它将起作用,即

System.out.printf("f1 = %s", f1);

关于Java - 打印对象的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27092608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com