计算分数
2017-03-20
1 import java.util.*; 2 class Scanf{ 3 Scanner sc = new Scanner(System.in); 4 int fenzi = sc.nextInt(); 5 int fenmu = sc.nextInt(); 6 } 7 class Fraction 8 { 9 private int fenzi; 10 private int fenmu; 11 public void setFenzi(int afenzi) 12 { 13 this.fenzi = afenzi; 14 } 15 public void setFenmu(int afenmu) 16 { 17 this.fenmu = afenmu; 18 } 19 public int getFenzi() 20 { 21 return fenzi; 22 } 23 public int getFenmu() 24 { 25 return fenmu; 26 } 27 //此处为减法运算! 28 public void subFraction(Fraction f){ 29 int a = this.fenzi * f.fenmu - this.fenmu * f.fenzi; 30 int b = this.fenmu * f.fenmu; 31 Fraction af = new Fraction(); 32 af.setFenzi(a); 33 af.setFenmu(b); 34 af.showInfor(); 35 } 36 //此处为加法运算! 37 public void addFraction(Fraction f) 38 { 39 int a = this.fenzi * f.fenmu + this.fenmu * f.fenzi; 40 int b = this.fenmu * f.fenmu; 41 Fraction af = new Fraction(); 42 af.setFenzi(a); 43 af.setFenmu(b); 44 af.showInfor(); 45 } 46 //此处为乘法运算 47 public void multFraction(Fraction f){ 48 int a = this.fenzi*f.fenzi; 49 int b = this.fenmu*f.fenmu; 50 Fraction af = new Fraction(); 51 af.setFenzi(a); 52 af.setFenmu(b); 53 af.showInfor(); 54 } 55 //此处为除法运算! 56 public void divFractio(Fraction f){ 57 int a = this.fenzi*f.fenmu; 58 int b = this.fenmu*f.fenzi; 59 Fraction af = new Fraction(); 60 af.setFenzi(a); 61 af.setFenmu(b); 62 af.showInfor(); 63 } 64 ///************************************** 65 public static void res(){ 66 //// 67 } 68 public void showInfor() 69 { 70 int a = this.fenzi; 71 int b = this.fenmu; 72 int small = a>b?b:a; 73 int max = 1; 74 for(int i =1;i<=b;i++){ 75 if(a%i == 0 && b%i==0){ 76 max = i; 77 } 78 } 79 if(b/max == 0){ 80 System.out.print("除数不能为0"); 81 }else{ 82 System.out.println("运算结果为: "+(a/max)+"/"+b/max); 83 } 84 } 85 } 86 class _res{ 87 88 } 89 class TestDemao6{ 90 91 public static void main(String[] args) 92 { 93 //实例化对象! 94 Fraction f1 = new Fraction(); 95 Fraction f2 = new Fraction(); 96 Scanner sc = new Scanner(System.in); 97 //提示输入算发符号。 98 for(int i =1;i<=4;i++){ 99 System.out.println("进行运算请输入:"+i+"对应的分别为加减乘除"); 100 } 101 //输入运算方法。 102 int bbbb = sc.nextInt(); 103 //对输入的内容进行判断 104 if(bbbb == 1){ 105 System.out.println("请输入第一个分数的分子和分母:"); 106 Scanf s1 = new Scanf(); 107 System.out.println("请输入第二个分数的分子和分母:"); 108 Scanf s2 = new Scanf(); 109 f1.setFenzi(s1.fenzi); 110 f1.setFenmu(s1.fenmu); 111 f2.setFenzi(s2.fenzi); 112 f2.setFenmu(s2.fenmu); 113 f1.addFraction(f2); 114 }else if(bbbb == 2){ 115 System.out.println("请输入第一个分数的分子和分母:"); 116 Scanf s1 = new Scanf(); 117 System.out.println("请输入第二个分数的分子和分母:"); 118 Scanf s2 = new Scanf(); 119 f1.setFenzi(s1.fenzi); 120 f1.setFenmu(s1.fenmu); 121 f2.setFenzi(s2.fenzi); 122 f2.setFenmu(s2.fenmu); 123 f1.subFraction(f2); 124 }else if(bbbb == 3){ 125 System.out.println("请输入第一个分数的分子和分母:"); 126 Scanf s1 = new Scanf(); 127 System.out.println("请输入第二个分数的分子和分母:"); 128 Scanf s2 = new Scanf(); 129 f1.setFenzi(s1.fenzi); 130 f1.setFenmu(s1.fenmu); 131 f2.setFenzi(s2.fenzi); 132 f2.setFenmu(s2.fenmu); 133 f1.multFraction(f2); 134 }else if(bbbb == 4){ 135 System.out.println("请输入第一个分数的分子和分母:"); 136 Scanf s1 = new Scanf(); 137 System.out.println("请输入第二个分数的分子和分母:"); 138 Scanf s2 = new Scanf(); 139 f1.setFenzi(s1.fenzi); 140 f1.setFenmu(s1.fenmu); 141 f2.setFenzi(s2.fenzi); 142 f2.setFenmu(s2.fenmu); 143 f1.divFractio(f2); 144 }else{ 145 System.out.println("输入错误!只能输入1-2-3-4其中的一个!!"); 146 } 147 } 148 }

浙公网安备 33010602011771号