分数类java代码 java分数运算-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
分数类java代码 java分数运算

JAVA定义分数类,定义加减法。。 求代码

public class NFactorial

创新互联主要从事成都做网站、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务应县,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

{

public static int add(int a, int b)

{

return a + b;

}

public static int sun(int a, int b)

{

return a - b;

}

public static double mul(int a, int b)

{

return a * b;

}

public static double div(int a, int b)

{

return (double)a/b;

}

public static void main(String[] args)

{

System.out.println(NFactorial.add(5, 5));

System.out.println(NFactorial.sun(5, 5));

System.out.println(NFactorial.mul(5, 5));

System.out.println(NFactorial.div(5, 5));

System.out.println();

}

}

急求两个Java程序!

强烈要求追加分,我花了一个多小时给你写的

累死我了!

1,分数类

public class Fraction {

private int numerator; //分子

private int denominator; //分母

public Fraction() {

numerator = 0;

denominator = 1;

}

public Fraction(int newNumerator, int newDenominator) {

if (newDenominator == 0) { //分母不能为零

System.out.println("分母为零!");

System.exit(0);

}

numerator = newNumerator;

denominator = newDenominator;

}

public void setNumerator(int newNumerator) {

numerator = newNumerator;

}

public void setDenominator(int newDenominator) {

denominator = newDenominator;

}

public void setBoth(int newNumerator, int newDenominator) {

numerator = newNumerator;

denominator = newDenominator;

}

public int getNumerator() {

return numerator;

}

public int getDenominator() {

return denominator;

}

public Fraction plus(Fraction f) { //加法(返回分数和)

int newNumerator = getNumerator() * f.getDenominator() +

f.getNumerator() * getDenominator();

int newDenominator = getDenominator() * f.getDenominator();

Fraction plus = new Fraction(newNumerator, newDenominator);

plus.reduce();

return plus;

}

public Fraction minus(Fraction f) { //减法(返回分数差)

int newNumerator = getNumerator() * f.getDenominator() -

f.getNumerator() * getDenominator();

int newDenominator = getDenominator() * f.getDenominator();

Fraction minus = new Fraction(newNumerator, newDenominator);

minus.reduce();

return minus;

}

public Fraction multiply(Fraction f) { //乘法(返回分数积)

int newNumerator = getNumerator() * f.getNumerator();

int newDenominator = getDenominator() * f.getDenominator();

Fraction minus = new Fraction(newNumerator, newDenominator);

minus.reduce();

return minus;

}

public Fraction divide(Fraction f) { //除法(返回分数商)

int newNumerator = getNumerator() * f.getDenominator();

int newDenominator = getDenominator() * f.getNumerator();

Fraction minus = new Fraction(newNumerator, newDenominator);

minus.reduce();

return minus;

}

public void output() { //输出分数

System.out.print(getNumerator() + "/" + getDenominator());

}

private void reduce() { //化简分数(分子分母同时除以最大公约数)

int max = maxCommonDivisor(getNumerator(), getDenominator());

numerator = getNumerator() / max;

denominator = getDenominator() / max;

}

private int maxCommonDivisor(int m, int n) { //返回m,n的最大公约数

int max = 1; //初始化最大公约数

int min = Math.min(Math.abs(m), Math.abs(n)); //m,n中绝对值较小的

for (int i = 1; i = min; i++) {

if ((m % i == 0) (n % i == 0)) { //m,n的公约数

max = i;

}

}

return max;

}

}

分数类测试

public class FractionDemo {

public static void main(String[] args) {

Fraction demo1 = new Fraction(1, 4);

Fraction demo2 = new Fraction(4, 6);

demo1.output();

System.out.print(" + ");

demo2.output();

System.out.print(" = ");

demo1.plus(demo2).output();

System.out.println();

demo1.output();

System.out.print(" - ");

demo2.output();

System.out.print(" = ");

demo1.minus(demo2).output();

System.out.println();

demo1.output();

System.out.print(" * ");

demo2.output();

System.out.print(" = ");

demo1.multiply(demo2).output();

System.out.println();

demo1.output();

System.out.print(" / ");

demo2.output();

System.out.print(" = ");

demo1.divide(demo2).output();

System.out.println();

}

}

2,长方形类

//矩形类,求矩形的周长和面积

public class Rectangle {

private double length; //长

private double width; //宽

public Rectangle() {

length = 0;

width = 0;

}

public Rectangle(double newLength, double newWidth) {

length = newLength;

width = newWidth;

}

public void setLength(double newLength) {

length = newLength;

}

public void setWidth(double newWidth) {

length = newWidth;

}

public void setBoeder(double newLength, double newWidth) {

length = newLength;

width = newWidth;

}

public double getLength() {

return this.length;

}

public double getWidth() {

return this.width;

}

public double getPerimeter() { //求周长

return (length + width) * 2;

}

public double getArea() { //求面积

return length * width;

}

}

长方形类演示

public class RectangleDemo {

public static void main(String[] args) {

Rectangle demo = new Rectangle(5, 4); //创建对象,长为5,宽为4

System.out.println("矩形的周长是:" + demo.getPerimeter());

System.out.println("矩形的面积是:" + demo.getArea());

}

}

用java怎么写分数?

你可以写个分数类,下面是参考:\x0d\x0a\x0d\x0apublic class Fraction {\x0d\x0a private Integer numerator; // 分子;\x0d\x0a private Integer denominator; // 分母\x0d\x0a\x0d\x0a /**\x0d\x0a * 构造器\x0d\x0a */\x0d\x0a public Fraction() {\x0d\x0a }\x0d\x0a\x0d\x0a public Fraction(Integer numerator, Integer denominator) {\x0d\x0a this.numerator = numerator;\x0d\x0a this.denominator = denominator;\x0d\x0a }\x0d\x0a\x0d\x0a public Integer getDenominator() {\x0d\x0a return denominator;\x0d\x0a }\x0d\x0a\x0d\x0a public void setDenominator(Integer denominator) {\x0d\x0a this.denominator = denominator;\x0d\x0a }\x0d\x0a\x0d\x0a public Integer getNumerator() {\x0d\x0a return numerator;\x0d\x0a }\x0d\x0a\x0d\x0a public void setNumerator(Integer numerator) {\x0d\x0a this.numerator = numerator;\x0d\x0a }\x0d\x0a\x0d\x0a /**\x0d\x0a * 求最大公约数\x0d\x0a * \x0d\x0a * @return\x0d\x0a */\x0d\x0a private int getGCD(int a, int b) {\x0d\x0a int max = Math.max(a, b);\x0d\x0a int min = Math.min(a, b);\x0d\x0a int mod = max % min;\x0d\x0a if (mod == 0) {\x0d\x0a return min;\x0d\x0a } else {\x0d\x0a return this.getGCD(mod, min);\x0d\x0a }\x0d\x0a }\x0d\x0a\x0d\x0a /**\x0d\x0a * 简化和约分\x0d\x0a * \x0d\x0a * @return\x0d\x0a */\x0d\x0a public Fraction simplify() {\x0d\x0a // 格式化\x0d\x0a if (this.numerator == 0) {\x0d\x0a this.denominator = null;\x0d\x0a return this;\x0d\x0a } else if (this.denominator * this.numerator 0\x0d\x0a this.denominator

回答于 2022-12-11

JAVA代码问题!输入5名学生的成绩,并计算总成绩,平均分,最高分,最低分

public static void main(String[] args) {

double scores[] = new double[5];

double total = 0;

double avg = 0;

double max = 0;

double min = 0;

int count=0;

String inputStr=null;

System.out.println("请输入5名学生的成绩:");

Scanner input = new Scanner(System.in);

while(count5){

try{

if(count 5){

System.out.println("请输入第"+(count+1)+"个分数:");

}

inputStr=input.nextLine();

scores[count++]=Double.valueOf(inputStr.trim());

}catch(Exception e){

if(inputStr!=null "exit".equals(inputStr.trim())){

System.out.println("您已成功结束程序");

System.exit(0);

}

System.out.println("若想结束请输入:exit");

System.out.print("您输入的分数不是数值类型,");

count--;

}

}

input.close();

Arrays.sort(scores);

min=scores[0];

max=scores[scores.length-1];

for(double score :scores){

total += score;

}

avg=total/scores.length;

System.out.println("总成绩是" + total);

System.out.println("最高分是" + max);

System.out.println("最低分是" + min);

System.out.println("平均分是" + avg);

}

//-------------------------------------------------------------------------

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

while(true){

Double[] scores = null;

double total = 0;

double avg = 0;

double max = 0;

double min = 0;

int count=1;

ListDouble inputScores=new ArrayListDouble();

String inputStr=null;

System.out.println("请输入要统计学生的成绩(理论上可以输入无限个,前提是你有那么大的内存):");

while(true){

try{

System.out.println("请输入第"+count+++"个分数,或输入ok进行计算,离开请输入exit");

inputStr=input.nextLine();

inputScores.add((double)Double.valueOf(inputStr.trim()));

}catch(Exception e){

if(inputStr!=null "exit".equals(inputStr.trim().toLowerCase())){

System.out.println("您已成功结束程序");

input.close();

System.exit(0);

}

if(inputStr!=null "ok".equals(inputStr.trim().toLowerCase())){

break;

}

System.out.println("您输入的分数不是数值类型,");

System.out.println("若想结束请输入exit ,若想计算结果请输入ok");

count--;

}

}

if(inputScores.size()==0){

System.out.println("您没有输入学生成绩,无数据可统计,程序结束。");

return ;

}

scores=inputScores.toArray(new Double[inputScores.size()]);

Arrays.sort(scores);

min=scores[0];

max=scores[scores.length-1];

for(double score :scores){

total += score;

}

avg=total/scores.length;

System.out.println("总成绩是" + total);

System.out.println("最高分是" + max);

System.out.println("最低分是" + min);

System.out.println("平均分是" + avg);

}

}


网页名称:分数类java代码 java分数运算
转载来源:http://scgulin.cn/article/docijop.html