1. 程式人生 > >16個經典java小程式

16個經典java小程式

               

1

public class add{

public static void main(String args[]){

String a,b,c;

a="Hello World!";

b="from all students of";

c="2007";

System.out.println(a+b+c);

}

}

2

import java.awt.*;

import java.applet.Applet;

public class javademo extends Applet{

public void paint(Graphics g){

String a,b,c,d;

a="Hello World!";

b="from all students of";

c=" 2007";

d=a+b+c;

g.drawString(d,60,30);

}

}

3

public class javademo{

public static void main(String args[]){

int i,j;

int h=7;

int yanghui[][]=new int[7][];

System.out.println("楊輝三角形");

for(i=0;i<yanghui.length;i++){yanghui[i]=new int[i+1];yanghui[0][0]=1;} for(i=1;i<yanghui.length;i++){

yanghui[i][0]=1;

for(j=1;j<yanghui[i].length-1;j++){yanghui[i][j]=yanghui[i-1][j-1]+yanghui[i-1][j];} yanghui[i][yanghui[i].length-1]=1;

}

for(i=0;i<yanghui.length;i++){

for(j=0;j<yanghui[i].length;j++)

System.out.print(yanghui[i][j]+" ");

System.out.println();

}

}

}

4

public class javademo{

public static void main(String args[]){

int i,j;

int h=7;

int yanghui[][]=new int[7][];

System.out.println("楊輝三角形");

for(i=0;i<yanghui.length;i++){yanghui[i]=new int[i+1];yanghui[0][0]=1;} for(i=1;i<yanghui.length;i++){

yanghui[i][0]=1;

for(j=1;j<yanghui[i].length-1;j++){yanghui[i][j]=yanghui[i-1][j-1]+yanghui[i-1][j];} yanghui[i][yanghui[i].length-1]=1;

}

for(i=0;i<yanghui.length;i++){

for(int k=0;k<2*(yanghui.length-i)-1;k++){System.out.print(" ");}

for(j=0;j<yanghui[i].length;j++)

System.out.print(yanghui[i][j]+" ");

System.out.println();

}

}

5

public class javademo{

public static void main(String args[]){

int f1=1,f2=2,f,n=24;

System.out.println("第1個月兔子對數:"+f1);

System.out.println("第2個月兔子對數:"+f2);

for(int i=3;i<=24;i++){

f=f2;

f2=f1+f2;

f1=f;

System.out.println("第"+i+"個月兔子對數:"+f2);

}

}

}

6

public class javademo{

public static void main(String args[]){ int count=0;

for(int i=101;i<200;i+=2){ boolean b=false;

for(int j=2;j<Math.sqrt(i);j++){ if(i%j==0){b=false;break;} else b=true;

}

if(b==true){count++;System.out.print(i+" ");} }

System.out.print("\n素數的個數為:\n"+count); }

}

7

public class javademo{

public static void main(String args[]){ int b1,b2,b3;

for(int i=100;i<1000;i++){ b1=i%10;

b2=i%100/10;

b3=i/100;

if(i==b1*b1*b1+b2*b2*b2+b3*b3*b3){ System.out.println(+i+"是一個水仙花數"); }

}

}

}

8

import java.util.*;

public class javademo{

public static void main(String args[]){ Scanner s=new Scanner(System.in); System.out.println("請輸入一個正整數"); int n=s.nextInt();

int k=2;

System.out.print(n+"=");

while(k<=n){

if(k==n){System.out.println(n);break;}

else if(n%k==0){System.out.print(k+"*");n=n/k;}

else k++;

}

}

}

9

import java.util.*;

public class javademo{

public static void main(String args[]){

Scanner s=new Scanner(System.in);

System.out.println("請輸入一個正整數");

int n=s.nextInt();

char grade=n>=90? 'A':n>=60?'B':'C';

System.out.println("該生等級為"+grade);

}

}

10

import java.util.*;

public class javademo{

public static void main(String args[]){

int a,b,m;

Scanner s=new Scanner(System.in);

System.out.println("請輸入一個數:");

a=s.nextInt();

System.out.println("請輸入另一個數:");

b=s.nextInt();

deff de=new deff();

m=de.deff(a,b);

int n=a*b/m;

System.out.println("最大公約數為:"+m);

System.out.println("最大公倍數為:"+n);

}

}

class deff{

public int deff(int x,int y){

int t,k;

if(x<y){t=x;x=y;y=t;}

while(y!=0){

if(x==y){return x;}

else{k=x%y;x=y;y=k;}

}

return x;

}

}

import java.util.*;

public class javademo{

public static void main(String args[]){

Scanner s=new Scanner(System.in);

System.out.println("請輸入數a:");

int a=s.nextInt();

System.out.println("請輸入相加項數:");

int n=s.nextInt();

int sum=0,b=0;

for(int i=0;i<n;i++){

b=b+a;

sum=sum+b;

a=10*a;

}

System.out.println(sum);

}

}

11

public class javademo{

public static void main(String args[]){

System.out.println("1000以內的完數有:");

for(int i=1;i<1000;i++){

int t=0;

for(int j=1;j<=i/2;j++){if(i%j==0)t=t+j;}

if(i==t)

System.out.print(i+" ");

}

}

}

12

public class javademo{

public static void main(String args[]){

int h=100,s=100;

for(int i=1;i<10;i++){

s=s+h;

h=h/2;

System.out.println("第"+(i+1)+"次下落經過的路程:"+s);

System.out.println("第"+(i+1)+"次彈起的高度:"+h/2);

}

}

}

13

public class javademo{

public static void main(String args[]){

int count=0;

int c;

for(int i=1;i<5;i++)

for(int j=1;j<5;j++)

for(int k=1;k<5;k++){

if(i!=j&&i!=k&&j!=k){

count++;

c=100*i+10*j+k;

System.out.print(c+" ");

if(count%10==0){System.out.println();}

}

}

System.out.println("\n符合要求的數個數為:"+count);

}

}

14

import java.applet.*;

import java.awt.*;

public class javademo extends Applet{

Image img;

public void init(){

img=getImage(getCodeBase(),"w.jpg");

}

public void paint(Graphics g){

int w=img.getWidth(this);

int h=img.getHeight(this);

g.drawImage(img,20,40,this);

g.drawImage(img,100,100,w/2,h/2,this);

g.drawImage(img,160,10,2*w,2*h,this);


}

}

15

import java.applet.*;

import java.awt.*;

public class MoveDemo extends Applet{

Image sky;

int x=10;

public void init(){

sky=getImage(getCodeBase(),"China.jpg");

}

public void paint(Graphics g){

g.drawImage(sky,x,0,this);

try{

Thread.sleep(500);

x=x+15;

if(x==10000){

x=10;

Thread.sleep(1000);

}}catch(Exception e){}

repaint();

}

}

16

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

import java.applet.AudioClip;

public class SoundPlay extends Applet implements ItemListener,ActionListener{ AudioClip sound;

Choice c=new Choice();

Button play=new Button("播放");

Button loop=new Button("連續");

Button stop=new Button("停止");

public void init(){

c.add("背景音樂 - Mid.wav");

//c.add("不得不愛 Wav 鈴音.wav");

c.add("王傑 - 一場遊戲一場夢 - 傷心幾分痴 粵語版 Wav8.wav"); c.add("兩隻小蜜蜂 - For Wav.wav");

//c.add("夏傷.wav");

add(c);

c.addItemListener(this);

add(play);

add(loop);

add(stop);

play.addActionListener(this);

loop.addActionListener(this);

stop.addActionListener(this);

sound=getAudioClip(getCodeBase(),"背景音樂 - Mid.wav"); }

public void itemStateChanged(ItemEvent e){

sound.stop();

sound=getAudioClip(getCodeBase(),c.getSelectedItem()); }

public void actionPerformed(ActionEvent e){

if(e.getSource()==play) sound.play();

else if(e.getSource()==loop) sound.loop();

else if(e.getSource()==stop) sound.stop();

}

}

對於學習有困難不知道如何提升自己可以加扣:578024144進行交流得到幫助,獲取學習資料


           

再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://blog.csdn.net/jiangjunshow