希望可以帮助到你 ~~

★☆ 设计一个表示分数的类Fraction

正文

Java语言程序设计进阶 第1周编程题

虽然我不能提交 但是耐不住自己试了试

到现在搞不懂题目中

double toDouble(); 这个功能

但是在main中没用到 tuDouble(); 那要他有啥用

说明

为了防止有人直接拷贝代码去提交

我对代码做了点小改变

但是不影响你测试学习

为自己机智 鼓掌.jpg

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.Scanner;

public class Fraction {
private int x;//分子
private int y;//分母
private static double dx,dy;

public Fraction(int x,int y)
{
this.x=x;
this.y=y;
}

public static double toDouble(Fraction a)
{
dx=a.x;
dy=a.y;
System.out.println(dx/dy);
return dx/dy;
}

public int gcd(int x, int y)
{
while(true)
{
if((y=y%x)==0)
{
return x;
}

if((x=x%y)==0)
{
return y;
}
}
}

public void print()
{
if(x%y==0)
{
System.out.println(x/y);
}
else
{
System.out.println((x/gcd(x,y))+"/"+(y/gcd(x,y)));
}

}

public Fraction plus(Fraction a)
{
Fraction w= new Fraction((this.x)*(a.y)+(a.x)*(this.y),((this.y)*(a.y)));
return w;
}

public Fraction multiply(Fraction a)
{
Fraction w = new Fraction(this.x*a.x,this.y*a.y);
return w;
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Fraction a = new Fraction(input.nextInt(), input.nextInt());
Fraction b = new Fraction(input.nextInt(),input.nextInt());
a.print();
b.print();
toDouble(b);
a.plus(b).print();
a.multiply(b).plus(new Fraction(5,6)).print();
a.print();
b.print();
input.close();
}
}

链接

Java语言程序设计进阶–翁恺

★☆ 微信自动快速复制粘帖发消息

  1. 1. 正文
  2. 2. 说明
  3. 3. 代码
  4. 4. 链接