Java编程挑战:最少钱币组合求解及圆锥体类实现详解
Java编程:最少钱币数,这是一个古老而又经典的问题,用给定的几种钱币
/***普通人民币金额的简单初始化*/publicclassTest{publicstaticvoidmain(String[]args){int[]commonMoney={100,50,20,10,5,1};System.out.println(getLeastNum(108,commonMoney));}staticStringgetLeastNum(inttargetMoney,int[]commonMoney){StringBufferbuffer=newStringBuffer();intnum=0,total=0;Stringresult=targetMoney+"需要$最低金额";for(intmoney:commonMoney){if(targetMoney>=money){total+=num=targetMoney/money;targetMoney=targetMoney%money;buffer.append(","+num+"张"+money+"$");}if(0==targetMoney)break;}returnresult+total+"张币分别为:"+buffer.substring(1);}}输出:108$需要至少5点钱,即:1件100$,1件5$,3件1$
有关java编程题目?
按照课题要求编写的圆、锥体和测试类Java程序如下
test.java文件内容如下
classCircle{
PrivateDoubler;
PrivateStringgcolor;
publiccircle(doubler){
this.r=r;
}
publicdoublearea(){
returnMath.PI*r*r;
}
publicdoubleperimeter(){
returnMath.PI*2*r;
}
publicdoublegetR()
返回is.r;
}
publicvoidsetR(doubler){
this.r=r;
}
publicStringgetColor(){
返回它的.color;
}
publicvoidsetColor(Stringcolor){
>this.color=color;
}
publicStringToString(){
返回"圆的半径为"+r+",颜色为"+color;
}
}
classcon{privatecircle;
privateedoubleh;
privateStringcolor;
publicconic(Circlec,doubleh){
this.c=c;
this.h=h;
}
publicdoublevolume(){
return1.0/3*c.area()*h;
}
publicCirclegetCircle(){
退货.c;
}
publicvoidsetCircle(circle){
this.c=c;
}
publicdoublegetH(){
返回他的。
publicStringgetColor(){
返回this.color;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
publicStringToString(){
return"圆锥体的底面积为"+c.area()+",高度是“+h+”,颜色是“+color;
}
}
publicClassTest
publiccstaticvoidmain(String[]args){
Circlecircle1=newCircle(2.5);
circle1.setColor('red');
System.out.println(circle1.toString());
System.out.('圆的面积是'+circle1.area());
System.out.println("圆的周长为"+circle1.perimeter());
Conecirclar1=newCone(circle1,2.7);
circlar1.setColor("blue");
System.outt.println(circlar1.toString());
System.out.println("圆锥体的体积是"+circlar1.volume());
}
}
Java编程题
1.importjava.util.Scanner;publicclassCube{privatedoublelen;//确定边长//获取边长publicdoublegetLen(){returnlen;}//设置边长publicvoidsetLen(doublelen){this.len=len;}//计算表面积publicdoublegetArea(doublelen){returnlen*len*6;}//计算体积publicdoublegetVolume(doublelen){returnlen*len*len;}publicstaticvoidmain(String[]args){Cubec=newCube();System.out.print("请输入边长:");c.setLen(newScanner(System.in))).nextDouble());System.out.println("一个立方体边长为:“+c.getLen()+”,表面积为:“+c.getArea(c.getLen())+”,体积为:“+c.getVolume(c.getLen())+”);}}2.importjava.util.Scanner;publicclassStudent{privateStringstuName;//定义姓名PrivatedoublechineseScore;//定义中文分数PrivatedoublemathScore;//定义数学分数PrivateoublecomputerScore;publicStringgetStuName(){returnstuName;}publicvoidsetStuName(StringstuName){this.stuName=stuName;}publicdoublegetChineseScore(){returnchineseScore;}publicvoidsetChineseScore(doublechineseScore){this.chineseScore=chineseScore;}publicdoublegetMathScore(){returnmathScore;}publicvoidsetMathScore(doublemathScore){this.mathScore=mathScore;}publicdoublegetComputerScore(){returncomputerScore;}publicvoidsetComputerScore(doublecomputerScore){this.computerScore=computerScore;}publicdoubleavg(doublechineseScore,doublemathScore,doublecomputerScore){return(chineseScore+mathScore+computerScore)*1.0/3;}publicstaticvoidmain(String[]args){Studentstu=newStudent();System.out.print("请输入姓名Student:");stu.setStuName(newScanner(System.in).next());System.out.print("请输入学生中文成绩:");stu.setChineseScore(newScanner(System.in).nextDouble());System.out.print("请输入你的数学结果对于学生:");stu.setMathScore(newScanner(System.in).nextDouble());System.out.print("请输入学生的计算机分数:");stu.setComputerScore(newScanner(System.in).nextDouble());System.out.println(stu.getStuName()+"学生平均成绩是"+stu.avg(stu.getChineseScore(),stu.getMathScore(),stu.getComputerScore()));}}相关文章

C语言实现兔子繁殖问题:循环队列解斐波那...
2024-12-28 09:43:17
C语言实现字母串排序:小写转换与大写字母...
2024-12-21 11:08:03
C语言多行数据输入技巧:轻松实现多组数据...
2024-12-26 10:09:48
Java代码实现:判断三角形类型及绘制倒...
2025-04-04 05:11:32
C语言数组排序函数:选择排序与排序方法详...
2024-12-26 00:18:44
程序员创意表白:编程语言中的浪漫情书
2024-12-17 20:31:04
C语言编程实现ASCII码表白:创意表白...
2024-12-20 03:36:00
C++数字转字符串技巧:Visual C...
2025-02-06 02:28:02
JavaScript轻松实现字符串换行显...
2024-12-16 13:05:23