×

java小程序入门代码

java小程序入门代码(java初学者求小程序代码)

admin admin 发表于2024-07-17 10:35:45 浏览13 评论0

抢沙发发表评论

大家好,java小程序入门代码相信很多的网友都不是很明白,包括java初学者求小程序代码也是一样,不过没有关系,接下来就来为大家分享关于java小程序入门代码和java初学者求小程序代码的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

本文目录

java初学者求小程序代码

//java计算器===========import java.awt.*;import java.awt.event.*;import java.text.DecimalFormat;import java.util.Dictionary;import javax.swing.*;public class Calculator extends JFrame { private JTextField jtf; private JPanel panel1, panel2, panel3, panel4; private num numActionListener; private JTextField jtf1; private boolean IfResult = true; private boolean point; private String oper = "="; private double result = 0; private JMenuBar mybar; private JMenu menu1, menu2, menu3; private JMenuItem edititem1, edititem2, help1, help2, help3; private JRadioButtonMenuItem seeitem1, seeitem2; private JCheckBoxMenuItem seeitem3; private ButtonGroup bgb; private String back; private DecimalFormat df; private boolean Resultr = false; private boolean flag = false; public Calculator(String title) { super(title); this.setLayout(new BorderLayout(10, 5)); panel1 = new JPanel(new GridLayout(1, 3, 10, 10)); panel2 = new JPanel(new GridLayout(4, 5, 5, 5)); panel3 = new JPanel(new BorderLayout(5, 5)); panel4 = new JPanel(new GridLayout(5, 1, 3, 5)); df = new DecimalFormat("0.#####"); mybar = new JMenuBar(); menu1 = new JMenu("(E)"); menu2 = new JMenu("查看(V)"); menu3 = new JMenu("帮助(H)"); menu1.setMnemonic(’E’); menu2.setMnemonic(’V’); menu3.setMnemonic(’H’); menu1.setFont(new Font("宋体", Font.PLAIN, 12)); menu2.setFont(new Font("宋体", Font.PLAIN, 12)); menu3.setFont(new Font("宋体", Font.PLAIN, 12)); edititem1 = new JMenuItem("复制(C)"); edititem2 = new JMenuItem("粘贴(P)"); edititem1.setIcon(new ImageIcon("icons/copy.png")); edititem2.setIcon(new ImageIcon("icons/paste.png")); bgb = new ButtonGroup(); seeitem1 = new JRadioButtonMenuItem("标准型(T)"); seeitem2 = new JRadioButtonMenuItem("科学型(S)"); seeitem3 = new JCheckBoxMenuItem("数字分组(I)"); help1 = new JMenuItem("关于计算器(A)"); help2 = new JMenuItem("帮助主题(H)"); help1.addActionListener(new clear()); help1.setIcon(new ImageIcon("icons/about.png")); help2.setIcon(new ImageIcon("icons/help.png")); bgb.add(seeitem1); bgb.add(seeitem2); menu1.add(edititem1); menu1.add(edititem2); menu2.add(seeitem1); menu2.add(seeitem2); menu2.addSeparator(); menu2.add(seeitem3); menu3.add(help1); menu3.addSeparator(); menu3.add(help2); mybar.add(menu1); mybar.add(menu2); mybar.add(menu3); this.setJMenuBar(mybar); edititem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); edititem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK)); seeitem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK)); seeitem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); seeitem3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK)); help1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); help2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK)); jtf = new JTextField(20); jtf.setEditable(false); jtf.setBackground(Color.white); jtf.setHorizontalAlignment(JTextField.RIGHT); jtf.setText("0"); jtf.setBorder(BorderFactory.createLoweredBevelBorder()); numActionListener = new num(); init(); } public void init() { // panel 面板按钮的添加 addButton(panel1, "Blackspace", new clear(), Color.red); addButton(panel1, "CE", null, Color.red); addButton(panel1, "C", new clear(), Color.red); addButton(panel2, "7", numActionListener, Color.blue); addButton(panel2, "8", numActionListener, Color.blue); addButton(panel2, "9", numActionListener, Color.blue); addButton(panel2, "/", new signs(), Color.red); addButton(panel2, "sqrt", new signs(), Color.blue); addButton(panel2, "4", numActionListener, Color.blue); addButton(panel2, "5", numActionListener, Color.blue); addButton(panel2, "6", numActionListener, Color.blue); addButton(panel2, "*", new signs(), Color.red); addButton(panel2, "%", new signs(), Color.blue); addButton(panel2, "1", numActionListener, Color.blue); addButton(panel2, "2", numActionListener, Color.blue); addButton(panel2, "3", numActionListener, Color.blue); addButton(panel2, "-", new signs(), Color.red); addButton(panel2, "1/x", new signs(), Color.blue); addButton(panel2, "0", numActionListener, Color.blue); addButton(panel2, "-/+", new clear(), Color.blue); addButton(panel2, ".", new dot(), Color.blue); addButton(panel2, "+", new signs(), Color.red); addButton(panel2, "=", new signs(), Color.red); JButton btns = new JButton(""); btns.setBorder(BorderFactory.createLoweredBevelBorder()); btns.setEnabled(false); btns.setPreferredSize(new Dimension(10, 10)); // panel4面板按钮的添加 panel4.add(btns); addButton(panel4, "MC", null, Color.red); addButton(panel4, "MR", null, Color.red); addButton(panel4, "MS", null, Color.red); addButton(panel4, "M+", null, Color.red); // 将面板添加倒顶层容器中 panel3.add(panel1, BorderLayout.NORTH); panel3.add(panel2, BorderLayout.CENTER); add(jtf, BorderLayout.NORTH); add(panel3); add(panel4, BorderLayout.WEST); pack(); this.setResizable(false); this.setLocation(300, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 按钮的添加方法 public void addButton(JPanel p, String name, ActionListener action, Color c) { JButton btn = new JButton(name); p.add(btn); btn.setForeground(c); btn.addActionListener(action); } public void addButton(JPanel p, String name, ActionListener action) { JButton btn = new JButton(name); p.add(btn); btn.addActionListener(action); } public void getResult(double x) { if (oper.equals("+")) { result += x; } else if (oper.equals("-")) result -= x; else if (oper.equals("*")) result *= x; else if (oper.equals("/")) result /= x; else if (oper.equals("=")) result = x; else if (oper.equals("s")) result = Math.sqrt(x); jtf.setText(df.format(result)); } class num implements ActionListener { public void actionPerformed(ActionEvent evt) { String str = evt.getActionCommand(); if (IfResult) { jtf.setText(""); IfResult = false; } jtf.setText(jtf.getText().trim() + str); if (jtf.getText().equals("0")) { jtf.setText("0"); IfResult = true; flag = true; } } } class signs implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("sqrt")) { double i = Double.parseDouble(jtf.getText()); if (i 》= 0) { jtf.setText(String.valueOf(df.format(Math.sqrt(i)))); } else jtf.setText("负数不能开平根!!"); } else if (command.equals("1/x")) { if (Double.parseDouble(jtf.getText()) == 0) { jtf.setText("除数不能为零"); } else jtf.setText(df.format((1 / Double.parseDouble(jtf.getText() .trim())))); } else if (command.equals("%")) { jtf .setText(df.format(Double.parseDouble(jtf.getText() .trim()) / 100)); } else { // 测试点 if (flag) IfResult = false; if (IfResult) { oper = command; } else { getResult(Double.parseDouble(jtf.getText())); oper = command; IfResult = true; } } } } // 清除的一些按钮类监听对象。 class clear implements ActionListener { public void actionPerformed(ActionEvent arg0) { String getaction = arg0.getActionCommand(); if (getaction.equals("C")) { jtf.setText("0"); result = 0; IfResult = true; } else if (getaction.equals("-/+")) { double i = 0 - Double.parseDouble(jtf.getText().trim()); jtf.setText(df.format(i)); } else if (getaction.equals("Blackspace")) { // 测试点 if (Double.parseDouble(jtf.getText()) 》 0 || jtf.getText().charAt(0) == ’负’) { if (jtf.getText().length() 》 1) { jtf.setText(jtf.getText().substring(0, jtf.getText().length() - 1)); } else { IfResult = true; jtf.setText("0"); } } else { if (jtf.getText().length() 》 2 || jtf.getText().charAt(0) == ’负’) { jtf.setText(jtf.getText().substring(0, jtf.getText().length() - 1)); } else { IfResult = true; jtf.setText("0"); } } } } } // 内部类 class dot implements ActionListener { public void actionPerformed(ActionEvent evt) { IfResult = false; if (jtf.getText().trim().indexOf(".") == -1) jtf.setText(jtf.getText() + "."); } } public static void main(String args) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); new Calculator("Java版计算器").setVisible(true); }}

Java初学者小程序

import javax.swing.JFrame ;import javax.swing.JPanel ;import java.awt.Graphics ;import java.awt.BorderLayout ;import java.awt.Color ;import java.util.ArrayList ;public class MobileSorter extends JFrame { //上边距 public static final int TOP_SPA = 100 ; //左边距 public static final int LEFT_SPA = 100 ; public static void main(String args) { JFrame.setDefaultLookAndFeelDecorated(true) ; MobileSorter ms = new MobileSorter() ; ms.setVisible(true) ; } public MobileSorter() { //设置窗体大小 this.setSize(400 ,400) ; //new出组件 MyPanel p = new MyPanel() ; //布置窗体 this.setLayout(new BorderLayout()) ; this.add(p ,BorderLayout.CENTER) ; //启动线程 Thread t = new Thread(p) ; t.start() ; }}class MyPanel extends JPanel implements Runnable { //存储圆数据的数组 private ArrayList ca = new ArrayList(9); /** * 中心圆 */ private Circle cCenter = null ; public MyPanel() { init() ; } public void paint(Graphics g) { //画与5号位的斜连接线 for (int i = 0; i《9; i++) { if (i == 4) continue ; Circle.drawLine(g ,(Circle)ca.get(4) ,(Circle) ca.get(i)) ; } //垂直和横线和竖线 g.setColor(Color.BLACK) ; for (int i = 0; i《9; i++) for (int j = 0; j《9; j++) if (i != j && i 《 j) if (j - i ==3 || (i + 1 == j && j%3 != 0)) Circle.drawLine(g , (Circle) ca.get(i) ,(Circle) ca.get(j)) ; /** 画圆 */ for (int i = 0; i《9; i++) ((Circle) ca.get(i)).drawMe(g) ; } /** * 初始化 */ public void init() { //创建圆 for (int i = 1; i《10; i++) this.ca.add(new Circle(i)) ; //生成圆内的数 int ; for (int i = 0; i《n.length; ) { int c ; c = (int)(Math.random() * 8) + 1 ; boolean isRepeat = false ; for (int j = 0; j《i; j++) if (n == c) { isRepeat = true ; break ; } if (isRepeat) continue ; if (i == 4) i ++ ; ((Circle)this.ca.get(i)).setNum(c) ; n = c ; i ++ ; } } /** * 线程 */ public void run() { int oPos = 0 ; //值为1的圆的标号 int sPos ; //值为1的圆的下一个圆的标号 int ePos ; //终端圆标号 int cPos = 0 ; //操作圆标号 cCenter = (Circle)this.ca.get(4) ; //中心圆 //找出圆内数字的1的圆 for (int i = 0; i《this.ca.size(); i++) if (((Circle)this.ca.get(i)).getNum() == 1) { oPos = i ; break ; } sPos = Circle.getNextDeasil(oPos) ; ePos = oPos ; while (true) { cPos = sPos ; while (true) { Circle c = (Circle)this.ca.get(cPos) ; Circle n = (Circle)this.ca.get(Circle.getNextDeasil(cPos)) ; checkSwap(c ,n) ;cPos = Circle.getNextDeasil(cPos) ; if(ePos == Circle.getNextDeasil(cPos)) { ePos = cPos ; break ; } } if (ePos == Circle.getNextDeasil(sPos)) { System.out.println ("OVER!") ; break ; } } } /** * 延迟 */ private void animation() { this.repaint() ; try { Thread.sleep(1000) ;}catch (Exception ex) { } } /** * 交换 */ private void checkSwap(Circle c ,Circle n) { int cNum = c.getNum() ; int nNum = n.getNum() ; if (cNum 》 nNum) { cCenter.setNum(n.getNum()) ; n.setNum(0) ; this.animation() ; n.setNum(c.getNum()) ; c.setNum(0) ; this.animation() ; c.setNum(cCenter.getNum()) ; cCenter.setNum(0) ; this.animation() ; } }}class Circle { /** * 各圆之间的间距 */ public static final int CIR_SPA_BET = 60 ; /** * 圆的直径 */ public static final int CIR_WD = 30 ; /** * 圆的标号 ,1-9 */ private int pos ; /** * 圆的左上角x坐标 */ private int x ; /** * 圆的左上角y坐标 */ private int y ; /** * 圆内的数 */ private int num ; public Circle(int pos) { this.pos = pos ; this.x = MobileSorter.LEFT_SPA + (pos-1) % 3 * CIR_SPA_BET ; this.y = MobileSorter.TOP_SPA + (pos-1) / 3 * CIR_SPA_BET ; } /** * 画圆与圆中的数字 */ public void drawMe(Graphics g) { //圆 g.setColor(Color.BLACK) ; g.fillOval(x ,y ,CIR_WD ,CIR_WD) ; //数字 g.setColor(Color.WHITE) ; if (num != 0) g.drawString(String.valueOf(num) ,x + CIR_WD / 2 - 3 ,y + CIR_WD / 2 + 5) ; else g.drawString("空" ,x + CIR_WD / 2 - 3 ,y + CIR_WD / 2 + 5) ; } /** * 画两个圆之间的连接线 */ public static void drawLine(Graphics g ,Circle a , Circle b) { g.drawLine(a.getX() + Circle.CIR_WD / 2 ,a.getY() + Circle.CIR_WD / 2 ,b.getX() + Circle.CIR_WD / 2 ,b.getY()+ Circle.CIR_WD / 2) ; } public void setNum(int num) { this.num = num ; } public int getX() { return x ; } public int getY() { return y ; } public int getPos() { return pos ; } public int getNum() { return num ; } public static int getNextDeasil(int pos) { if (pos 》=0 && pos 《=8 && pos != 4) { if (pos == 0) return 1 ; else if (pos == 1) return 2 ; else if (pos == 2) return 5 ; else if (pos == 3) return 0 ; else if (pos == 5) return 8 ; else if (pos == 6) return 3 ; else if (pos == 7) return 6 ; else if (pos == 8) return 7 ; } return -1 ; } /** * 根据顺时针方向返回下个圆的标号 */ public int getNextDeasil() { if (pos 》=0 && pos 《=8 && pos != 4) { if (pos == 0) return 1 ; else if (pos == 1) return 2 ; else if (pos == 2) return 5 ; else if (pos == 3) return 0 ; else if (pos == 5) return 8 ; else if (pos == 6) return 3 ; else if (pos == 7) return 6 ; else if (pos == 8) return 7 ; } return -1 ; } } 0

用java写一个小程序求代码:

给你一个简单的提示 不是只要For吗 太简单了你把这个图形用星星补满可以看出来是5行5列。就是25个呵呵,耍巧的地方到了定义一个int数组int {0,0,1,0,0 0,1,0,1,0 1,0,1,0,1};int k=1;//换行for(int i:a){ k++; if(i=0) System.out.print(" "); if(i=1) System.out.print("*"); if(k%5==0) System.out.println();}是不是很猥琐 呵呵 如果是初学的话 还是别用这招。其他方法也不难。。慢慢来

初学者java小程序,求java大神帮我补充一下空缺代码!

import java.util.Arrays;public class Array2 {public static void main(String args) {//声明一个名为myArray的数组,该数组有2行,每行列数不等,并为其分配内存空间int;myArray;//第一行有5个元素,并为其分配内存空间myArray;//第二行有10个元素,并为其分配内存空间for (int j = 0; j 《 myArray.length; j++)//用1-10之间的随机整数给第一行元素赋值myArray = (int)(Math.random() * 10);//用100-200之间的随机整数给第二行元素赋值for (int j=0; j 《 myArray.length; j++) myArray=(int)(Math.random() * 100 + 100);for (int i=0; i 《 myArray.length; i++){//输出myArray数组各元素的值for (int j=0; j 《 myArray.length; j++){System.out.print(myArray+" ");}System.out.println();}Arrays.sort(myArray);//对第一行元素排序Arrays.sort(myArray);//对第二行元素排序System.out.println("\n排序后的数组元素: ");for (int i=0; i《myArray.length;i++){//再次输出myArray数组各元素的值for (int j=0; j《myArray.length;j++){System.out.print(myArray+" ");}System.out.println();} }}7 3 9 6 7 103 165 166 148 103 179 128 109 120 156 排序后的数组元素: 3 6 7 7 9 103 103 109 120 128 148 156 165 166 179

初学Java求一些Java简单小程序的代码实例

public class HelloWorld{ public static void main(String args){ System.out.println("Helloworld!"); }}java入门的第一个小程序,你安装个JDK,运行下试试,呵呵!!

java小程序初学

你把Catch块的NumberFormatException拼错了,仔细看一下try{ len=Integer.parseInt(args); } catch(NumberFormatExecption ee){ System.out.println("请输入合法的公里数!"); return; }

java小程序设计求代码

Account类:

package Test;import java.util.HashMap;import java.util.Map;public class Account {    private String cardId;//卡号    private String name;//姓名    private String password;//密码    private long leftAmt;//剩余金额        public Map《Object, Object》getAccountInfo(String id){        //这里暂设一个用户信息        //实际要建一个数据库存放多个用户的信息        Map《Object, Object》 map = new HashMap《Object, Object》();        map.put("cardId", "123");        map.put("name", "qwe");        map.put("password", "789");        map.put("leftAmt", 88);        //实际要通过sql语句在数据库中查询id对应的账户信息        if(id == "123"){            //这里没有考虑密码,全部返回            return map;        }        return null;    }        public void setCardId(String cardId) {        this.cardId = cardId;    }    public String getCardId() {        return cardId;    }    public void setName(String name) {        this.name = name;    }    public String getName() {        return name;    }    public void setPassword(String password) {        this.password = password;    }    public String getPassword() {        return password;    }    public void setLeftAmt(long leftAmt) {        this.leftAmt = leftAmt;    }    public long getLeftAmt() {        return leftAmt;    }}

userOperate类:

package Test;import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class UserOperate {    Account account = new Account();        public void operate(String id){        System.out.println("欢迎您使用ATM系统");        System.out.println("1--存款     2--查询");        System.out.println("3--取款    0--退出");        System.out.println("请选择操作类型【0-3】");        Scanner in = new Scanner(System.in);        int type = in.nextInt();        while(type!=0){            if(type==1){                System.out.println("请存入钞票");                type = in.nextInt();            }else            if(type==2){                System.out.println("您的银行账户信息为:");                Map《Object, Object》 map = new HashMap《Object, Object》();                map = account.getAccountInfo(id);                System.out.println(map);                type = in.nextInt();            }else            if(type==3){                System.out.println("请取走您的钞票");                type = in.nextInt();            }else{                System.out.println("操作有误,请重新操作");                type = in.nextInt();            }        }        System.out.println("请尽快取走您的卡,谢谢使用!再见!");        in.close();    }}

测试类(我放在main里):

package Test; public class test1 {    public static void main(String args){        //测试                UserOperate userOperate = new UserOperate();        //假设银行卡插入后读取到的id为123        String id = "123";        userOperate.operate(id);    }}

运行结果:

这是很简单的一个思路,简单跑跑可以。

也不知道你要什么样的结果,如果是要一个完整的系统,那具体还得建立数据库,还有如何读取账户信息,等等。

哪位高手帮写个JAVA入门小程序,估计就几十行代码

//第一题: import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.ArrayList;import java.util.List;public class ManageName { List《String》 namelist = new ArrayList《String》(); public void PrintMenu() { System.out.println("\n\r1.Add new name " + "2.Display all name" + " 3.Quit" + "\nPlease select menu(1,2,3)"); } public void displayname() { for (int w = 0; w 《 this.namelist.size(); w++) { System.out.println("Name" + w+":" + this.namelist.get(w)); } } public List addName(String name) { namelist.add(name); return namelist; } public static void main(String args) throws IOException { ManageName a = new ManageName(); a.addName("jing5083394"); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while (true) { a.PrintMenu(); String content = in.readLine(); if (content.equalsIgnoreCase("3")) { break; } else if (content.equalsIgnoreCase("1")) { String name; BufferedReader in2 = new BufferedReader(new InputStreamReader( System.in)); System.out.println("Please input the Name to add new user"); String content2 = in.readLine(); name = content2; a.addName(name); System.out.println("Add name successfully\r\n"); } else if (content.equalsIgnoreCase("2")) { a.displayname(); } else { System.out.println("Invalid selections!\n"); } }}} //第二题: 把文件放到namefile.txt/* * 从namefile.txt文件中读入的字符串全部转换成大写字母, * 再按原来的顺序输出到yoursData.txt文件中。 */import java.lang.Character;import java.io.*;//将一个文件复制到另一个文件中(覆盖)public class Filestream { public static void main(String args) { try { File inFile = new File("c:\\namefile.txt"); File outFile = new File("c:\\yoursData.txt"); FileInputStream fis = new FileInputStream(inFile); // 读输入文件 FileOutputStream fos = new FileOutputStream(outFile); int c; while ((c = fis.read()) != -1) { c = Character.toUpperCase(c); fos.write(c); }// 写入文件中 System.out.println("Output file finish"); fis.close(); fos.close(); } catch (FileNotFoundException e) { System.out.println("FileStreamsTest: " + e); } catch (IOException e) { System.err.println("FileStreamsTest: " + e); } }}

关于本次java小程序入门代码和java初学者求小程序代码的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。