導航:首頁 > 編程語言 > java編程作業

java編程作業

發布時間:2022-07-31 21:12:45

A. 簡單的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]+", ");
}

}
}

B. java編程作業

/**
* 實現3的階乘
* @author Administrator * */
public class JieCheng {
public static void main(String[] args) {
System.out.println(getFactorialSum(3)+"");//通過循環實現階乘
int sum=1,n=0;
Scanner scan = new Scanner(System.in);

System.out.println("輸入一個數字");
int n= in.nextInt();//這里沒做校驗,可以用try防止錯誤輸入

for(int i=1;i<=n;i++){
sum=sum*i;
}
System.out.println(sum);
}
//通過遞歸實現階乘
private static int getFactorialSum(int n){
if(n==1||n==0){
return 1;
}else{
return getFactorialSum(n-1)*n;
}
}
}
需要注意的是n如果太大 int會溢出,所以換成biginteger

C. java編程作業,求答案

import java.awt.*;
import java.awt.event.*;

public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("姓名:xxx");
frame.setLayout(new BorderLayout());
Panel panel1 = new Panel();
Panel panel2 = new Panel();
TextField tf = new TextField(20);
panel1.add(tf);
panel2.setLayout(new GridLayout(4,4));

panel2.add(new Button("0"));
panel2.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("3"));
panel2.add(new Button("4"));
panel2.add(new Button("5"));
panel2.add(new Button("6"));
panel2.add(new Button("7"));
panel2.add(new Button("8"));
panel2.add(new Button("9"));
panel2.add(new Button("A"));
panel2.add(new Button("B"));
panel2.add(new Button("C"));
panel2.add(new Button("D"));
panel2.add(new Button("E"));
panel2.add(new Button("F"));

frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}

D. java編程作業數列求解

代碼如下:

importjava.util.Scanner;

publicclassApp{

publicstaticvoidmain(String[]args){

Scannerscanner=newScanner(System.in);

int[]numbers={8,4,2,1,23,344,12};

intval=scanner.nextInt();

booleanfound=false;

for(inti=0;i<numbers.length;i++){
if(val==numbers[i]){
found=true;
}
}

if(found){
System.out.println("數列中包含數"+val);
}else{
System.out.println("數列中不包含數"+val);
}
}
}

E. 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();

}

}

F. java編程作業,求打救,急.............................

import java.util.Random;

public class StudentsAward {

public static void main(String[] args) {
int[] id = {1,2,3,4,5,6,7,8,9,10};
boolean[] used = new boolean[id.length];
Random r = new Random();
int i ,j=0;
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("三等獎獲得者是:"+id[i]+"號");
used[i] =true;
}while(j!=3);
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("二等獎獲得者是:"+id[i]+"號");
used[i] =true;
}while(j!=5);
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("一等獎獲得者是:"+id[i]+"號");
used[i] =true;
}while(j!=6);
}
}

G. Java程序作業編寫

這個題的寫法有幾種
寫法一:
public
class
Circle
{
public
static
void
main(String[]
args)
{
//定義圓的半徑
int
r=10;
//定義π的取值范圍
double
π=3.14;
System.out.println("當圓的半徑為"+r+"時"+"其周長為"+2*r*π);
}
}
寫法二:
public
class
Circle
{
public
static
void
main(String[]
args)
{
//定義圓的半徑
int
r=10;
//直接調用API中現成的方法math.PI
//Math
類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數
//PI比任何其他值都更接近
pi(即圓的周長與直徑之比)的
double
值。
System.out.println("當圓的半徑為"+r+"時"+"其周長為"+2*r*Math.PI);
}
}
寫法三:
public
class
Circle
{
public
static
void
main(String[]
args)
{
//定義圓的半徑
int
r=10;
//定義π的取值范圍
double
π=3.14;
//計算圓的周長
double
perimeter=2*r*π
System.out.println("當圓的半徑為"+r+"時"+"其周長為"+perimeter);
}
}
寫法四
//利用面向對象的編程方法,設計類Circle計算圓的周長。
public
class
TestCircle
{
public
static
void
main(String[]
args)
{
Circle
c1=new
Circle();
c1.setRais(10);//c1.rais
System.out.println(c1.findArea());
}
}
class
Circle
{
//定義半徑
int
radius=10;
//提供方法用於獲取圓的面積
public
double
findArea(){
System.out.println("當圓的半徑為"+radius+"時"+"其面積為");
return
3.14*2*radius;
}
//返回圓的半徑
public
double
getRais(){
return
radius;
}
//設置圓的半徑
public
void
setRais(int
r){
radius
=
r;
}
}
等等種寫法

H. 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);
}
}

I. 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

閱讀全文

與java編程作業相關的資料

熱點內容
python列表求交集 瀏覽:872
解壓包如何轉音頻 瀏覽:447
機明自動編程軟體源碼 瀏覽:325
php埠號設置 瀏覽:540
phperegreplace 瀏覽:320
androidgridview翻頁 瀏覽:537
ssh協議編程 瀏覽:634
如何開我的世界電腦伺服器地址 瀏覽:861
玄關pdf 瀏覽:609
程序員學習論壇 瀏覽:940
程序員的毒雞湯怎麼做 瀏覽:548
安卓怎麼降級軟體到手機 瀏覽:281
雲與伺服器入門書籍推薦產品 瀏覽:636
delphi編程助手 瀏覽:762
電腦遇到伺服器問題怎麼辦 瀏覽:515
加工中心編程結束方法 瀏覽:296
了解什麼是web伺服器 瀏覽:140
面向對象的編程的基本特徵 瀏覽:718
php定時執行任務linux 瀏覽:787
php數組中刪除元素 瀏覽:725