package diyizhou;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Fraction a = new Fraction(in.nextInt(), in.nextInt());
Fraction b = new Fraction(in.nextInt(),in.nextInt());
a.print();
b.print();
a.plus(b).print();
a.multiply(b).plus(new Fraction(5,6)).print();
a.print();
b.print();
in.close();
}
}
class Fraction{
int a,b,c,d,e,f;
double score;
Fraction(int a ,int b)
{
this.a=a;
this.b=b;
}
double todouble()
{
return score = a/b;
}
Fraction plus(Fraction r)
{
a=a*r.b+r.a*b;
b=b*r.b;
}
Fraction multiply(Fraction r)
{
a=a*r.a;
b=b*r.b;
}
void print()
{
if(b==a)
System.out.println("1");
else
System.out.println(a/gcd(a, b)+"/"+b/gcd(a, b));
}
int gcd(int a,int b)
{
int m,n;
m=a;
n=b;
while(true)
{
if((m = m%n)==0)
return n;
if((n = n%m)==0)
return m;
}
}
}
IDE提示我
plus()
和
multiply
缺少返回值。我不知道这种以类为返回类型的函数该怎样写。
麻烦大家帮帮我。
因为你定义了返回值,在函数内却没有返回!
plus()和multiply函数里加上return就好了,参考我以下的修改代码
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Fraction a = new Fraction(in.nextInt(), in.nextInt());
Fraction b = new Fraction(in.nextInt(),in.nextInt());
a.print();
b.print();
a.plus(b).print();
a.multiply(b).plus(new Fraction(5,6)).print();
a.print();
b.print();
in.close();
}
}
class Fraction{
int a,b,c,d,e,f;
double score;
Fraction(int a ,int b)
{
this.a=a;
this.b=b;
}
double todouble()
{
return score = a/b;
}
Fraction plus(Fraction r)
{
a=a*r.b+r.a*b;
b=b*r.b;
return r;
}
Fraction multiply(Fraction r)
{
a=a*r.a;
b=b*r.b;
return r;
}
void print()
{
if(b==a)
System.out.println("1");
else
System.out.println(a/gcd(a, b)+"/"+b/gcd(a, b));
}
int gcd(int a,int b)
{
int m,n;
m=a;
n=b;
while(true)
{
if((m = m%n)==0)
return n;
if((n = n%m)==0)
return m;
}
}
}
Fraction plus(Fraction r)
{
a=a*r.b+r.a*b;
b=b*r.b;
return r;
}
Fraction multiply(Fraction r)
{
a=a*r.a;
b=b*r.b;
return r;
}
只要返回值是Fraction类型的就可以了,不知道你纠结什么。