java小程序代码

合集下载

闹钟java小程序

闹钟java小程序
JLabel jl1, jl2, jl3, jl4;
JMenu jm1, jm2, jm3, jm4;
JMenuBar jmb;
JMenuItem jmi1, jmi2, jmi3, jmi4, jmi5, jmi6, jmi7, jmi8, jmi9,jmi10;
JTextField jtf1, jtf2, time;
g.drawOval(L0 + 51, T0 + 51, h - 102, h - 102);
r = h / 2 - 30;
x0 = 30 + r - 5 + L0;
y0 = 30 + r - 5 - T0;
ang = 60;
for (int i = 1; i <= 12; i++) {
int sdo, mdo, hdo, old_M, old_H, hh, mm, ss;
int delay = 1000;
Calendar now;
String st, alarm, Items1, Items2,str[];
JButton jb;
JComboBox jc1, jc2, jc3;
public class ClockPanel extends JPanel {
public void paint(Graphics g) {
h = getSize().height - 200;
// 绘制圆形
g.setColor(C1);
g.fillOval(L0 + 30, T0 + 30, h - 60, h - 60);
JPanel jp1, jp2, jp3;

推金币Java小程序(Java)

推金币Java小程序(Java)

推金币Java小程序(Java)推金币游戏是一种常见的娱乐方式,通过这个小程序,我们可以学习如何使用Java语言来实现一个简单的推金币游戏。

本文将介绍推金币游戏的规则以及Java代码的实现。

一、游戏规则推金币游戏是一个单人游戏,游戏中有一堆金币,玩家的目标是通过不断推动金币使其从堆中掉落。

游戏的规则如下:1. 开始时,堆中有一定数量的金币。

2. 玩家每次可以选择推动金币的个数,推动的个数范围是1到3个金币。

3. 玩家和电脑交替进行推金币的操作,直到堆中的金币被推空。

4. 最后将判断推完金币时,是玩家推金币的次数奇数还是偶数,奇数次则玩家获胜,偶数次则电脑获胜。

二、Java代码实现下面是使用Java语言实现推金币小程序的代码:```javaimport java.util.Scanner;public class PushCoinGame {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int coins = 10; // 初始金币数量int count = 0; // 推金币次数while (coins > 0) {// 玩家推金币System.out.print("请输入你要推的金币个数(1-3个):"); int num = scanner.nextInt();if (num < 1 || num > 3 || num > coins) {System.out.println("输入不合法,请重新输入!");continue;}coins -= num;count++;// 判断堆中金币是否已经被推空if (coins <= 0) {break;}// 电脑推金币int compNum = 4 - num; // 电脑总是推剩余金币个数和为4的倍数的数量coins -= compNum;count++;}// 判断胜负if (count % 2 == 0) {System.out.println("电脑获胜!");} else {System.out.println("玩家获胜!");}}}```三、小结通过以上代码,我们实现了一个简单的推金币游戏的Java小程序。

Java猜拳小游戏源代码

Java猜拳小游戏源代码

第一个文件:public class Computer {String name;int score;public int showfist(){int quan;quan=(int)(Math.random()*10);if(quan<=2){quan=1;}else if(quan<=5){quan=2;}else{quan=3;}switch(quan){case 1:System.out.println(name+"出拳:剪刀");break;case 2:System.out.println(name+"出拳:石头");break;case 3:System.out.println(name+"出拳:布");break;}return quan;}}第二个文件:import java.util.Scanner;public class Game {int count=0;int countP=0;Person person=new Person();Computer computer=new Computer();Scanner input=new Scanner(System.in);public void initial(){System.out.print("请选择你的角色(1.刘备 2.孙权 3.曹操):");int juese=input.nextInt();switch(juese){case 1:="刘备";break;case 2:="孙权";break;case 3:="曹操";break;}System.out.print("请选择对手角色(1.关羽 2.张飞 3.赵云):");int JueSe=input.nextInt();switch(JueSe){case 1:="关羽";break;case 2:="张飞";break;case 3:="赵云";break;}}public void begin(){System.out.print("\n要开始吗? (y/n)");String ans=input.next();if(ans.equals("y")){String answ;do{int a=person.showFist();int b=computer.showfist();if(a==1&&b==3||a==2&&b==1||a==3&&b==2){System.out.println("结果:你赢了!");person.score++;}else if(a==1&&b==1||a==2&&b==2||a==3&&b==3){System.out.println("结果:平局,真衰!嘿嘿,等着瞧吧!");countP++;}else{System.out.println("结果:你输了!");computer.score++;}count++;System.out.print("\n是否开始下一轮? (y/n)");answ=input.next();}while(answ.equals("y"));}}public String calcResult(){String a;if(person.score>computer.score){a="最终结果:恭喜恭喜!你赢了!";}else if(person.score==computer.score){a="最终结果:打成平手,下次再和你一决高下!";}else{a="最终结果:呵呵,你输了!笨笨,下次加油啊!";}return a;}public void showResult(){System.out.println("---------------------------------------------------");System.out.println("\t\t"++" VS"++"\n");System.out.println("对战次数:"+count+"次");System.out.println("平局:"+countP+"次");System.out.println(+"得:"+person.score+"分");System.out.println(+"得:"+computer.score+"分\n");System.out.println(calcResult());System.out.println("---------------------------------------------------");}}第三个文件:import java.util.Scanner;public class Person {String name;int score;Scanner input=new Scanner(System.in);public int showFist(){System.out.print("\n请出拳:1.剪刀2.石头3.布");int quan=input.nextInt();switch(quan){case 1:System.out.println("你出拳:剪刀");break;case 2:System.out.println("你出拳:石头");break;case 3:System.out.println("你出拳:布");break;}return quan;}}第四个文件:public class Test {public static void main(String[]args){Game g=new Game();System.out.println("-----------------欢迎进入游戏世界--------------------\n\n");System.out.println("\t\t******************");System.out.println("\t\t** 猜拳开始 **");System.out.println("\t\t******************\n\n");System.out.println("出拳规则:1.剪刀2.石头3.布");g.initial();g.begin();g.showResult();}}。

java小游戏源代码

java小游戏源代码

Java小游戏第一个Java文件:import ;public class GameA_B {public static void main(String[] args) {Scanner reader=new Scanner(System.in);int area;"Game Start…………Please enter the area:(1-9)" +'\n'+"1,2,3 means easy"+'\n'+"4,5,6 means middle"+'\n'+"7,8,9 means hard"+'\n'+"Please choose:");a rea=reader.nextInt();s witch((area-1)/3){c ase 0:"You choose easy! ");break;c ase 1:"You choose middle! ");break;c ase 2:"You choose hard! ");break;}"Good Luck!");G ameProcess game1=new GameProcess(area);game1.process();}}第二个Java文件:import ;import ;public class GameProcess {int area,i,arrcount,right,midright,t;int base[]=new int[arrcount],userNum[]=new int[area],sysNum[]=new int[area];Random random=new Random();Scanner reader=new Scanner(System.in);GameProcess(int a){area=a;arrcount=10;right=0;midright=0;t=0;base=new int[arrcount];userNum=new int[area];sysNum=new int[area];for(int i=0;i<arrcount;i++){base[i]=i;//;}}void process(){r and();w hile(right!=area){s canf();c ompare();p rint();c heck();}}void rand(){f or(i=0;i<area;i++){t=random.nextInt(arrcount);//;sysNum[i]=base[t];;delarr(t);}}void delarr(int t){f or(int j=t;j<arrcount-1;j++)base[j]=base[j+1];a rrcount--;}void scanf(){"The system number has created!"+"\n"+"Please enter "+area+" Numbers");for(int i=0;i<area;i++){userNum[i]=reader.nextShort();}}void check(){if(right==area)"You win…………!");}boolean check(int i){r eturn true;}void compare(){i nt i=0,j=0;r ight=midright=0;f or(i=0;i<area;i++){for(j=0;j<area;j++){if(userNum[i]==sysNum[j]){if(i==j)right++;elsemidright++;}}}}void print(){" A "+right+" B "+midright);}}import java.awt.*;import ;import javax.swing.*;class TestGame {public static void main(String[] args) {App ap = new App(); //调用App()开始运行程序ap.show();}}class App extends JFrame {MainPanel mp;public App() {mp = new MainPanel();this.getContentPane().add(mp);this.setSize(400, 450);this.setTitle("小游戏");}}/*** 主面板* 显示格子* @author Administrator**/class MainPanel extends JPanel { ButtonPanel bp = new ButtonPanel(); CtrlPanel rp = new CtrlPanel();public MainPanel() {this.setLayout(new BorderLayout());StartListener());this.add(bp, "Center");this.add(rp, "South");}class StartListener implements ActionListener { /*** 重新开始按钮的事件* 调用按钮面板里面的颜色初始化方法*/public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "重新开始") {bp.ColorInit();}}}}class ButtonPanel extends JPanel {JButton[][] b = new JButton[5][5];/*** 按钮界面的构造器* 设置布局方式为Grid布局,并生成5*5的格子,* 在每个格子生成一个按钮,* 为每个按钮添加一个监听事件*/public ButtonPanel() {this.setLayout(new GridLayout(5, 5));for (int i = 0; i < 5; i++) {for (int j = 0; j < 5; j++) {b[i][j] = new JButton();b[i][j].setActionCommand("" + (i + 1) + (j + 1));b[i][j].addActionListener(new MyButtonListener());this.add(b[i][j]);}}this.ColorInit();}/*** 面板初始化时候给所有的格子都绘上深灰色* i.j分别是行和列*/public void ColorInit() {for (int i = 0; i < 5; i++) {for (int j = 0; j < 5; j++) {b[i][j].setBackground(Color.DARK_GRAY);}}}/*** 按钮上监听的时事件,监听点击* @author Administrator**/class MyButtonListener implements ActionListener { int r, c;/*** 需要改变颜色的行和列* r row* c colunm* 调用change()来改变颜色*/public void actionPerformed(ActionEvent e) {int i = Integer.parseInt(e.getActionCommand());r = i / 10 - 1;c = i % 10 - 1;this.changer();}/*** 传一个按钮控件进去* 判断颜色,如果是深灰则变为粉红* 否则义相反* @param b*/public void btnChange(JButton b) {if (b.getBackground() == Color.DARK_GRAY) {b.setBackground(Color.pink);} else {b.setBackground(Color.DARK_GRAY);}}/*** 这个方法是根据点击的按钮判断周围需要* 不能超越数组的下标*/public void changer() {this.btnChange(b[r][c]);if (r > 0) //行号大于0this.btnChange(b[r - 1][c]);if (r < 4)this.btnChange(b[r + 1][c]);if (c > 0)//列号大于0this.btnChange(b[r][c - 1]);if (c < 4)//列好小余0this.btnChange(b[r][c + 1]);}}}/*** 控制面板* @author Administrator*下面的开始按钮*/class CtrlPanel extends JPanel {JButton btnstart;public CtrlPanel() {btnstart = new JButton("重新开始");this.add(btnstart);}}import java.util.*;public class Cai {enum Res{SHITOU, JIANZI, BU};Res res;public static void main(String[] args) throws Exception { // TODO Auto-generated method stubCai cai = new Cai();"请输入你的选择:");"0表示石头,1表示剪子,2表示布");char yourResultOfChar =(char) ;int yourResultOfInt = yourResultOfChar - '0';int computerResult = pb();cai.getYourResult(yourResultOfInt); switch (computerResult){case 0:"电脑选择石头");break;case 1:"电脑选择剪子");break;case 2:"电脑选择布");break;}cai.pa(computerResult);}public void getYourResult(int count) {Res[] result = Res.values();res = result[count];}void pa(int computer){Res[] result = Res.values();if(this.res == Res.SHITOU){"我选择石头");switch(result[computer]){case SHITOU:"平局,再来!");break;case JIANZI:"我赢了!");break;case BU:"我输了!");break;}} else if(this.res == Res.JIANZI) {"我选择剪子");switch(result[computer]){case JIANZI:"平局,再来!");break;case BU:"我赢了!");break;case SHITOU:"我输了!");break;}} else if(this.res == Res.BU) {"我选择布");switch(result[computer]){case BU:"平局,再来!");break;case SHITOU:"我赢了!");break;case JIANZI:"我输了!");break;}}}static int pb(){Random ran = new Random();int res = ran.nextInt(3);return res;//输出0-2的整数,0表示石头,1表示剪子,2表示布,和enum Res中的顺序相对应}}import java.util.*; //导入实用包util下所有的类import javax.swing.*;import java.awt.*;import ;public class CaiShu {public static void main(String[] args) {Win f = new Win();f.setVisible(true);}}class Win extends JFrame implements ActionListener {JLabel labe;JButton butt;JButton button;Random a = new Random();private int i = 0;private int num;JTextField text1, text2;JPanel p;public Win() {super("猜数游戏");labe = new JLabel("我心里有个数,它是1---100之间的,你能猜出来吗?");butt = new JButton("确认");button = new JButton("重开");text1 = new JTextField(5);text2 = new JTextField(20);p = new JPanel();Container con = getContentPane();// 调用JFrame的getContentPane得到容器text2.setEditable(false);// 使输出结果文本域不可编辑butt.addActionListener(this);// 执行结果动作con.setLayout(new GridLayout(4, 1));// 设置整个界面的长宽比p.add(text1);// 添加输入数字文本域p.add(butt);p.add(button);button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { text1.setText("");text2.setText("");i=0;}});con.add(labe);// 添加游戏标签con.add(p);con.add(text2);// 添加输出结果信息文本域setSize(300, 300);// 设置窗口尺寸setVisible(true);// 设置窗口可视pack();addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false);System.exit(0);}});}public void actionPerformed(ActionEvent e) {int shu;while (true) {shu = Integer.parseInt(text1.getText());if (i == 0) {num = a.nextInt(100);}i++;if (i == 10) {text2.setText("结束吧,你没有希望了!!");i = 0;break;}if (e.getSource() == butt) {if (shu > num) {text2.setText("输入的数大了,输小点的!");} else if (shu < num) {text2.setText("输入的数小了,输大点的!");} else if (shu == num) {text2.setText("恭喜你,猜对了!");if (i <= 2)text2.setText("你真是个天才!");else if (i <= 6)text2.setText("还将就,你过关了!");else if (i <= 8)text2.setText("但是你还……真笨!");elsetext2.setText("你实在是太笨了!");}break;}}}}。

代码统计小程序—Java代码

代码统计小程序—Java代码

代码统计⼩程序—Java代码 这⼏天在看正则表达式,看到尚学堂的正则表达式的专题,⾥⾯有个⽤正则表达式代码统计的⼩程序,⾃⼰练习了⼀下,现在记录下来。

⽤于统计⼀个⽂件夹内所有后缀名为.java的⽂件的所有的代码总量1package com.test;23import java.io.BufferedReader;4import java.io.File;5import java.io.FileNotFoundException;6import java.io.FileReader;7import java.io.IOException;89public class CodeCounter {1011//有效程序⾏数12public static int normalLines = 0;13//空⽩⾏数14public static int whiteLines = 0;15//注释⾏数16public static int commentLines = 0;171819public static void main(String [] args){20 File f = new File("f:\\test");21 File[] codeFile = f.listFiles();22//循环23for(File file : codeFile ){24//提取后缀名为.java的⽂件25if(file.getName().matches(".*\\.java")){26 parse(file);27 }28 }2930 System.out.println("normalLines " + normalLines);31 System.out.println("whiteLines " + whiteLines);32 System.out.println("commentLines " + commentLines);33 }3435public static void parse(File file){36 BufferedReader br = null;37//判断此⾏是否为注释⾏38boolean comment = false;3940try {41 br = new BufferedReader(new FileReader(file));42 String line = "";43while((line = br.readLine()) != null){44 line = line.trim();45if(line.matches("^[\\s&&[^\\n]]*$")){46//空⾏47 whiteLines ++;48 }else if(line.startsWith("/*")&& ! line.endsWith("*/")){49//判断此⾏为"/*"开头的注释⾏50 commentLines ++ ;51 comment = true;52 }else if(comment == true && !line.endsWith("*/")){53//为多⾏注释中的⼀⾏(不是开头和结尾)54 commentLines ++ ;55 }else if(comment == true && line.endsWith("*/")){56//为多⾏注释的结束⾏57 commentLines ++ ;58 comment = false;59 }else if(line.startsWith("//")){60//单⾏注释⾏61 commentLines ++ ;62 }else{63//正常代码⾏64 normalLines ++;65 }66 }67 } catch (FileNotFoundException e) {68 e.printStackTrace();69 }catch (IOException e) {70 e.printStackTrace();71 }finally{72if(br != null){73try {74 br.close();75 br = null;76 } catch (IOException e) {77 e.printStackTrace();78 }79 }80 }81 }82 }83。

java解析小程序码

java解析小程序码

java解析小程序码小程序码是微信小程序的一种推广方式,用户可以通过扫描小程序码快速打开对应的小程序。

而在Java开发中,我们可以使用一些库来解析小程序码,从而获取相关的信息。

首先,我们需要引入相关的库。

在Java中,有很多开源的库可以用来解析小程序码,比如zxing、javase、jbarcode等。

这些库都提供了丰富的API,可以方便地解析小程序码。

接下来,我们需要获取小程序码的图片。

可以通过网络请求或者本地文件的方式获取小程序码的图片。

如果是网络请求,可以使用Java的HttpURLConnection或者HttpClient等库来发送请求并获取图片数据。

如果是本地文件,可以使用Java的FileInputStream来读取文件数据。

获取到小程序码的图片后,我们就可以使用相应的库来解析了。

以zxing为例,我们可以使用它提供的MultiFormatReader类来解析小程序码。

首先,我们需要将图片数据转换成Bitmap对象,然后将Bitmap对象转换成BinaryBitmap对象,最后使用MultiFormatReader的decode方法来解析。

解析后,我们可以获取到小程序码的内容。

小程序码的内容一般是一个URL,可以通过解析后的结果来获取。

我们可以使用正则表达式或者字符串处理的方式来提取URL。

除了获取小程序码的内容,我们还可以获取其他的信息,比如小程序码的宽度、高度、颜色等。

这些信息可以通过解析后的结果来获取。

在解析小程序码的过程中,我们还需要注意一些异常情况的处理。

比如图片解析失败、解析结果为空等情况,我们需要进行相应的处理,以保证程序的稳定性。

总结一下,Java解析小程序码可以通过引入相关的库来实现。

我们可以获取小程序码的图片,然后使用相应的库来解析。

解析后,我们可以获取小程序码的内容以及其他的信息。

在解析过程中,我们需要注意异常情况的处理,以保证程序的稳定性。

通过Java解析小程序码,我们可以方便地获取小程序码的相关信息,为小程序的推广和使用提供便利。

JAVA小程序—贪吃蛇源代码

JAVA小程序—贪吃蛇源代码

JAVA贪吃蛇源代码SnakeGame。

javapackage SnakeGame;import javax.swing。

*;public class SnakeGame{public static void main( String[]args ){JDialog。

setDefaultLookAndFeelDecorated( true ); GameFrame temp = new GameFrame();}}Snake.javapackage SnakeGame;import java。

awt.*;import java。

util。

*;class Snake extends LinkedList{public int snakeDirection = 2;public int snakeReDirection = 4;public Snake(){this。

add( new Point( 3, 3 ));this。

add(new Point(4, 3 ));this.add( new Point(5,3 ) );this。

add( new Point(6,3 ) );this。

add(new Point(7,3 ));this。

add( new Point( 8,3 ) );this。

add( new Point( 9, 3 ));this。

add( new Point( 10,3 ));}public void changeDirection( Point temp, int direction ) {this。

snakeDirection = direction;switch(direction ){case 1://upthis.snakeReDirection = 3;this。

add( new Point(temp.x,temp.y - 1 ));break;case 2://rightthis。

编写java小程序的示例代码

编写java小程序的示例代码

深入学习Java编程:示例代码演示引言Java编程语言是世界上最流行的编程语言之一,它的广泛应用范围包括Web应用程序、移动应用、嵌入式系统和大数据处理。

无论你是初学者还是有经验的开发者,本文将为你提供一个深入学习Java编程的好起点。

我们将通过示例代码演示Java的基本概念,帮助你逐步理解这门语言。

第一步:Hello World编写Java程序的第一步通常是创建一个简单的Hello World程序。

这个程序将向你展示如何编写基本的Java代码、编译它并运行它。

下面是一个Hello World示例代码:public class HelloWorld {public static void main(String[] args) {System.out.println("Hello, World!");}}上述代码创建了一个名为HelloWorld的Java类,其中包含一个名为main的方法。

main方法是Java程序的入口点,它会在程序运行时首先执行。

在main方法中,我们使用System.out.println来输出文本到控制台。

第二步:变量和数据类型在Java中,你可以声明各种不同类型的变量来存储数据。

下面是一些常见的数据类型和如何声明变量的示例:int myNumber = 42; // 整数double myDouble = 3.14; // 双精度浮点数boolean isJavaFun = true; // 布尔值String greeting = "Hello, Java!"; // 字符串在上述示例中,我们声明了整数、双精度浮点数、布尔值和字符串类型的变量。

你可以根据需要选择合适的数据类型来存储不同类型的数据。

第三步:条件语句和循环Java提供了条件语句和循环结构,允许你控制程序的流程和执行重复的操作。

以下是一些示例代码:条件语句(if-else)int age = 20;if (age >= 18) {System.out.println("你已经成年了");} else {System.out.println("你还未成年");}循环(for循环)for (int i = 1; i <= 5; i++) {System.out.println("循环迭代次数:" + i);}第四步:函数和方法在Java中,你可以创建自己的函数或方法,以便组织和重用代码。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if(l==w){
System.out.println("图形是正方形!");
}else{
System.out.println("图形不是正方形!");
}
}
public static void main(String args[]){
Zhengfangxingjuxing=new Zhengfangxing();
s2 = Integer.parseInt(t2.getText());
if(s1 > s2)
t3.setText(t1.getText());
else
t3.setText(t2.getText());
}
public static void main(String args[]) {
new CompareTwo().launchFrame(); //new出来一个匿名类直接运行窗口
Scanner sca=new Scanner(System.in);
int[] a=new int[3];
for(int i=0;i<a.length;i++){
a[i]=Integer.parseInt(sca.next());
}
Arrays.sort(a);
for(int i=0;i<a.length;i++){
*考点:1、图形化界面显示
*2、给容器中添加组件
*3、给某一组件添加事件,事件的写法
*/
import java.awt.*;
import java.awt.event.*;
public class CompareTwo extends Frame implements ActionListener {
1.从键盘键入三个整数,然后按照从小到大的顺序将其输出。
package test;
import ng.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class Bidx {
public static void main(String[] args) {
public Box1(float l,float w,float h){
this.l=l;
this.w=w;
this.h=h;
}
public float getV(){
float v=l*w*h;
return v;
}
public static void main(String[] args){
Box1 box=new Box1(10,9,8);
System.out.println(s);
}
}
publicclassTest{
publicstaticvoidmain(String [] args){
Test1 test=newTest1();
test.printS();
Test1 test2=newTest1("nihao");
test2.printS();
System.out.println("求50以内的素数:");
for(int i=1;i<50;i++){
if(i==2){
System.out.println(i);
}
for(int j=2;j<=(i/2+1);j++){
if(i%j==0){
break;
}
if(j==(i/2+1)){
System.out.println(i);
System.out.println("无参数的构造函数!");
}
public Test1(String s){
System.out.println("带参数的构造函数!参数值为:"+s);
}
}
public class Test{
public static void main(String[] args){
TextField t1 = new TextField(5);
TextField t2 = new TextField(5);
TextField t3 = new TextField(5);
Button b = new Button("比较");
Label l = new Label("比较大的是:");
public class HelloWorld {
public static void main(String[] args) {
System.out.println("helloworld!");
}
}
/*要求:写一个图形化输入界面,输入两个整数,然后点击按钮”比较“,显示出两个
*整数中比较大的一个
}
}
2.定义一个矩形类,再定义接口EqualDiagonal,其中包含方法getDiagonal();由矩形类派生出一个正方形类,自行扩充成员变量和方法,并实现此接口EqualDiagonal。
父类
package test;
public class Juxing implements EqualDiagonal {
this.add(b);
this.add(l);
this.add(t3);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter() { //下面四行是关闭窗口程序,运用匿名类,这段程序可以记忆
public void windowClosing(WindowEvent e) {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[9];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < a.length; i++) {
public class Sum {
public static void main(String[] args){
int sum=0;
System.out.println("求50以内的奇数和:");
for(int i=1;i<=50;i=i+2){
sum+=i;
}
System.out.println(sum);
System.out.println(box.getV());
}
}
1.创建一个类,声明一个无参数的构造函数,打印类已创建的信息;再重载一个具有String参数的构造函数,打印参数信息;并创建主类验证之。
package test;
class Test1 {
String s;
public Test1(){
public void launchFrame() {//用于启动窗口
this.setSize(300,400);//设置窗口大小
this.setLayout(new FlowLayout()); //设置顺序布局,否则可能显示一个很大的文本输入框
this.add(t1);
this.add(t2);
}
}
定义一个复数类complex,内有两个成员变量:实部和虚部,还有两个方法:求两个复数和以及求两个复数差。编程验证该程序的复数加减运算时正确的。
package test;
class complex
{ double real,imag;
complex()
{
real=0;
imag=0;
}
complex(double i,double j)
Test1 test=new Test1();
Test1 test2=new Test1("nihao");
}
}

packagecandy;
classTest1 {
Strings="这是已创建的信息!";
publicTest1(){
}
publicTest1(String s){
this.s=s;
}
voidprintS(){
k.real=real-a.real;
k.imag=imag-a.imag;
return k;
}
void print()
{
System.out.println(real+","+imag);
}
}
public class second
float h;
public void setDemo(float l,float w,float h){
this.l=l;
this.w=w;
this.h=h;
}
public float getV(){
float v=l*w*h;
return v;
}
public static void main(String[] args){
}
}
/*要求,从命令行输入参数n,计算出结果s = 1+1/(1+2)+1/(1+2+3)+...+1/(1+2+...+n)
*此程序中考点:1、用jbuilder输入命令行参数以及如何使用命令行参数
相关文档
最新文档