java編程作業
以下是源代碼和運行結果截圖,如果哪裡有問題的話可以來找我。
classPet{
intage;
intweight;
publicPet(intage,intweight){
super();
this.age=age;
this.weight=weight;
}
publicvoidshowInfo(){
System.out.println("myweightis"+this.weight+"andmyageis"+this.age);
}
publicintgetWeightInfo(){
returnthis.weight;
}
publicintgetAgeInfo(){
returnthis.age;
}
}
interfaceEatable{
publicabstractvoidbeEatted();//默認為public和abstract
}
classCatextendsPet{
publicCat(intage,intweight){
super(age,weight);
//TODOAuto-generatedconstructorstub
}
privateintcry;
publicvoidshowInfo(){
System.out.println("I'maCat,myweightis"+this.weight+"andmyageis"+this.age);
}
publicintgetWeightInfo(){
returnsuper.getWeightInfo();
}
publicintgetAgeInfo(){
returnsuper.getAgeInfo();
}
}
{
publicDog(intage,intweight){
super(age,weight);
//TODOAuto-generatedconstructorstub
}
privateintcry;
publicvoidshowInfo(){
System.out.println("I'maDog,myweightis"+this.weight+"andmyageis"+this.age);
}
publicintgetWeightInfo(){
returnsuper.getWeightInfo();
}
publicintgetAgeInfo(){
returnsuper.getAgeInfo();
}
publicvoidbeEatted(){
}
}
classFruitimplementsEatable{
publicFruit(Stringcolor,Stringaddress){
super();
this.color=color;
this.address=address;
}
Stringcolor;
Stringaddress;
publicvoidbeEatted(){
System.out.println("I'mbeeneaten!");
}
publicvoidshowInfo(){
System.out.println("mycoloris"+this.color+"andmyaddressis"+this.address);
}
publicStringgetColorInfo(){
returnthis.color;
}
publicStringgetAddressInfo(){
returnthis.address;
}
}
classBananaextendsFruit{
publicBanana(Stringcolor,Stringaddress){
super(color,address);
//TODOAuto-generatedconstructorstub
}
publicvoidbeEatted(){
System.out.println("I'maBanana,I'mbeeneaten!");
}
publicvoidshowInfo(){
System.out.println("I'maBanana,mycoloris"+this.color+"andmyaddressis"+this.address);
}
@Override
publicStringgetColorInfo(){
//TODOAuto-generatedmethodstub
returnthis.getColorInfo();
}
@Override
publicStringgetAddressInfo(){
returnthis.getAddressInfo();
}
}
classAppleextendsFruit{
Stringtype;
publicApple(Stringcolor,Stringaddress,Stringtype){
super(color,address);
this.type=type;
//TODOAuto-generatedconstructorstub
}
publicStringgetType(){
returntype;
}
publicvoidbeEatted(){
System.out.println("I'manapple,I'mbeeneaten!");
}
publicvoidshowInfo(){
System.out.println("I'manApple,mycoloris"+this.color+",myaddressis"+this.address
+"andmytypeis"+type);
}
@Override
publicStringgetColorInfo(){
//TODOAuto-generatedmethodstub
returnthis.getColorInfo();
}
@Override
publicStringgetAddressInfo(){
returnthis.getAddressInfo();
}
}
publicclassPrograms{
publicstaticvoidmain(String[]args){
Catmycat=newCat(10,20);
Dogmydog=newDog(10,24);
Bananamybanana=newBanana("黃色","北京");
Applemyapple=newApple("紅色","上海","新品種");
mycat.showInfo();
mydog.showInfo();
mybanana.showInfo();
myapple.showInfo();
mydog.beEatted();
mybanana.beEatted();
myapple.beEatted();
}
}
② 簡單的java編程作業~~強烈求援助~~
import java.util.Scanner;
public class Mytest {
public static void main (String args[]){
Mytest tMytest=new Mytest();
tMytest.order();
tMytest.avg();
tMytest.min();
}
private void avg(){
float[] tNumbers=new float[10];
float sum=0;
for(int i=0;i<10;i++){
Scanner tScan=new Scanner(System.in);
tNumbers[i]=tScan.nextFloat();
sum+=tNumbers[i];
}
System.out.print("平均值為:"+sum/10+"\n");
int num=0;
String cont="";
for(int i=0;i<10;i++){
if(tNumbers[i]>sum/10){
num+=1;
cont+=tNumbers[i]+", ";
}
}
System.out.println(num+"個數字在平均值以上:"+cont);
}
private void min(){
float[] tNumbers=new float[10];
float tMin=0;
for(int i=0;i<10;i++){
Scanner tScan=new Scanner(System.in);
tNumbers[i]=tScan.nextFloat();
if(i==0){
tMin=tNumbers[i];
}else {
if(tMin>=tNumbers[i]){
tMin=tNumbers[i];
}
}
}
System.out.print("最小值為:"+tMin);
}
private void order(){
float[] tNumbers=new float[10];
for(int i=0;i<10;i++){
Scanner tScan=new Scanner(System.in);
tNumbers[i]=tScan.nextFloat();
}
for(int i=0;i<10;i++){
for(int j=1;j<10-i;j++){
if(tNumbers[j-1]>tNumbers[j]){
tNumbers[j]=tNumbers[j]+tNumbers[j-1];
tNumbers[j-1]=tNumbers[j]-tNumbers[j-1];
tNumbers[j]=tNumbers[j]-tNumbers[j-1];
}
}
System.out.print(tNumbers[10-i-1]+", ");
}
}
}
③ 關於java編程的作業,有沒有誰會幫一下忙啊TAT(回答請用英文),萬分感謝
1, java.util.Scanner
2,nextInt()
3,nextDouble()
4,next()
5,nextLine()
6,nextShort()
part2
int One=keyboard.nextInt();
double One=keyboard.nextDouble();
short One =keyboard.nextShort();
byte One=keyboard.nextByte();
④ java編程題作業二
publicclassShuzu{
publicstaticvoidmain(String[]args){
getMax();
getSum();
}
publicstaticvoidgetMax(){
int[]buf={10,15,12,9,7};
intmax=0;
for(inti=0;i<buf.length;i++){
if(buf[i]>max){
max=buf[i];
}
}
System.out.println("最大值:"+max);
}
publicstaticvoidgetSum(){
int[]buf={10,15,12,9,7};
intsum=0;
for(inti=0;i<buf.length;i++){
sum=sum+buf[i];
}
System.out.println("和為:"+sum);
}
}
⑤ 關於JAVA編程作業
好久沒寫代碼了。拿你這兩題試試。作業以後還是要自己做。就幫你玩這一次,寫的不一定好,不足之處你自己看著改改吧。
第一題
import java.util.Scanner;
public class MyWork {
public static int input;
private int sum;
public void fact(int n) {
for(int i=1; i<=n; i++){
sum+=i;
}
if(n>0){
fact(--n);
}else{
System.out.println(input+"階乘之和為:"+sum);
}
}
public static void main(String[] args) {
System.out.print("輸入N的值:");
Scanner scan = new Scanner(System.in);
input = scan.nextInt();
MyWork work = new MyWork();
work.fact(input);
}
}
第二題
public class HomeWork {
public void printRectangle(){
for(int i=1; i<=9; i++){
for(int j=1; j<=9; j++){
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}
}
public void printTriangle(){
for(int i=1; i<=9; i++){
for(int j=1; j<=i; j++){
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
HomeWork work = new HomeWork();
System.out.println("矩形列印:");
work.printRectangle();
System.out.println();
System.out.println("三角形列印:");
work.printTriangle();
}
}
⑥ Java四個編程作業: <1>從鍵盤輸入一個雙精度數,再向屏幕輸出; <2>判斷
importjava.util.Scanner;
publicclassexp{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.print("輸入數:");
intn=scanner.nextInt();
inttemp=1,sum=0;
for(inti=1;i<=n;i++){
//*temp保存每一個數的階乘
temp=temp*i;
//sum保存所有數的階乘的和
sum=sum+temp;
}
System.out.println("結果是:"+sum);
}
}
⑦ JAVA編程作業
importjava.math.BigDecimal;
importjava.util.Random;
publicclassIncrease{
publicstaticbooleanisPrime(inta){
booleanflag=true;
if(a<2){//素數不小於2
returnfalse;
}else{
for(inti=2;i<=Math.sqrt(a);i++){
if(a%i==0){//若能被整除,則說明不是素數,返回false
flag=false;
break;//跳出循環
}
}
}
returnflag;
}
publicstaticvoidtest1(){
inti,n,k=0;
System.out.println("1-1000內素數");
for(n=3;n<=1000;n++){//3~1000的所有數
i=2;
while(i<n){
if(n%i==0)break;//若能整除說明n不是素數,跳出當前循環
i++;
}
if(i==n){//如果i==n則說明n不能被2~n-1整除,是素數
k++;//統計輸出數的個數
System.out.print(i+" ");
if(k%6==0)//每輸出5個則換行
System.out.println();
}
}
}
publicstaticvoidtest2()
{
Randomr=newRandom();
System.out.println(r.nextInt(301)-100);
}
publicstaticinttest3(intm,intn){
//輾轉相除法
intr;
do{
if(m<n)
{
r=m;
m=n;
n=r;
}
r=m%n;
m=n;
n=r;
}while(r!=0);
returnm;
}
publicstaticdoubletest4(intn){
doublee=1f;
doubletotal=1.0;
for(inti=0;i<n;i++)
{
total/=i+1;
e+=total;
}
BigDecimalb=newBigDecimal(e);
e=b.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();//表明四捨五入,保留四位小數
returne;
}
publicstaticvoidmain(String[]args){
//第一題測試
System.out.println(isPrime(131));
test1();
//第二題測試
test2();
//第三題測試
System.out.println("最大公約數為:"+test3(1302,19924));
//第四題測試
System.out.println("e="+test4(100));
}
}
運行結果:
true
1-1000內素數
3 5 7 11 13 17
19 23 29 31 37 41
43 47 53 59 61 67
71 73 79 83 89 97
101 103 107 109 113 127
131 137 139 149 151 157
163 167 173 179 181 191
193 197 199 211 223 227
229 233 239 241 251 257
263 269 271 277 281 283
293 307 311 313 317 331
337 347 349 353 359 367
373 379 383 389 397 401
409 419 421 431 433 439
443 449 457 461 463 467
479 487 491 499 503 509
521 523 541 547 557 563
569 571 577 587 593 599
601 607 613 617 619 631
641 643 647 653 659 661
673 677 683 691 701 709
719 727 733 739 743 751
757 761 769 773 787 797
809 811 821 823 827 829
839 853 857 859 863 877
881 883 887 907 911 919
929 937 941 947 953 967
971 977 983 991 997 106
最大公約數為:2
e=2.7183