用java求1+1/3+1/5+1/7+1/9

 我来答
Cool__Wu
2020-10-23 · 超过53用户采纳过TA的回答
知道小有建树答主
回答量:182
采纳率:83%
帮助的人:29.2万
展开全部

秉承面向抽象编程的原则:

对方无视了你的分数计算题,并直接扔给了你一个分数计算器。


import java.util.Scanner;
import java.util.StringTokenizer;

/**
* @Author: Cool_Wu
* @Date: 2020-10-22 22:59
*/
public class Calculator {
int numerator;  // 分子
int denominator; // 分母

Calculator(){
}

Calculator(int a,int b){
if(a == 0){
numerator = 0;
denominator = 1;
}
else{
setNumeratorAndDenominator(a,b);
}
}

void setNumeratorAndDenominator(int a, int b){  // 设置分子和分母
int c = f(Math.abs(a),Math.abs(b));         // 计算最大公约数
numerator = a / c;
denominator = b / c;
if(numerator<0 && denominator<0){
numerator = - numerator;
denominator = - denominator;
}
}

int getNumerator(){
return numerator;
}

int getDenominator(){
return denominator;
}

int f(int a,int b){  // 求a和b的最大公约数
if(a < b){
int c = a;
a = b;
b = c;
}
int r = a % b;
while(r != 0){
a = b;
b = r;;
r = a % b;
}
return b;
}

Calculator add(Calculator r){  // 加法运算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b + denominator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}

Calculator sub(Calculator r){  // 减法运算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b - denominator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}

Calculator muti(Calculator r){ // 乘法运算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}

Calculator div(Calculator r){  // 除法运算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b;
int newDenominator = denominator * a;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}

// 封装了具体运算,主要为对输入进行转换,对输出封装
public static void compute(String data1,String operation,String data2){
StringTokenizer fenxi = new StringTokenizer(data1,"/");
int data1_1 = Integer.parseInt(fenxi.nextToken());
int data1_2 = Integer.parseInt(fenxi.nextToken());
fenxi = new StringTokenizer(data2,"/");
int data2_1 = Integer.parseInt(fenxi.nextToken());
int data2_2 = Integer.parseInt(fenxi.nextToken());

Calculator r1 = new Calculator(data1_1,data1_2);
Calculator r2 = new Calculator(data2_1,data2_2);

Calculator result;
int a,b;
if(operation.equals("+")){
result = r1.add(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}

if(operation.equals("-")){
result = r1.sub(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}

if(operation.equals("×")){
result = r1.muti(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}

if(operation.equals("÷")){
result = r1.div(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请用户输入运算次数(1~N):");
int count = input.nextInt();
for (int i = 0; i < count; i++) {
// 用户输入两分数和运算符
System.out.println("请用户输入第一个分数(格式a/b):");
String data1 = input.next();
System.out.println("请用户输入要进行运算的运算符(+-×÷ 可复制粘贴):");
String operation = input.next();
System.out.println("请用户输入第二个分数(格式c/d):");
String data2 = input.next();

// 根据用户输入进行具体运算
Calculator cal = new Calculator();
System.out.println("运算结果为:");
cal.compute(data1, operation, data2);
}
}
}

microroom
科技发烧友

2020-10-22 · 智能家居/数码/手机/智能家电产品都懂点
知道大有可为答主
回答量:7118
采纳率:83%
帮助的人:1648万
展开全部
//代码如下
public class Test {
public static void main(String []args) {
double sum=0;

for(int i=1;i<10;i+=2)
sum+=1.0/i;
System.out.println(sum);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式