21xrx.com
2024-11-10 00:43:59 Sunday
登录
文章检索 我的文章 写文章
《用Java编写有趣简单的程序》
2023-06-16 22:51:26 深夜i     --     --
Java编程 简单代码 机器人 计算器 数组操作

Java是一种面向对象的编程语言,与许多其他编程语言不同,它既是简单的,也是强大的。如果你想玩一些有趣的代码,下面就有一些简单的Java代码示例。

1. 机器人

这个机器人程序是一个简单的示例,但它可以为你提供很多乐趣。它使用AWT和Swing库创建一个窗体,然后在窗体中绘制一个机器人。你可以使用鼠标移动机器人的头和手臂,也可以使机器人向前移动。


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Robot extends JPanel implements MouseListener, MouseMotionListener {

  int x, y; // robot 的头的坐标

  int armX, armY; // 机器手臂的坐标

  boolean armGrabbed = false; // 手臂是否在运动

  int headSize = 100; // 头的半径

  int armLength = 75; // 手臂的长度

  int bodyLength = 150; // 身体长度

  int bodyWidth = 60;

  public Robot() {

   addMouseListener(this);

   addMouseMotionListener(this);

  }

  public void paint(Graphics g) {

   super.paint(g);

   Graphics2D g2d = (Graphics2D) g;

   int robotCentreX = x;

   int robotCentreY = y - headSize;

   // 旋转机器人

   g2d.rotate(Math.toRadians(armX), robotCentreX, robotCentreY);

   g2d.setColor(Color.blue);

   g2d.fillOval(x-headSize/2, y-headSize/2, headSize, headSize); // 画个圆

   g2d.setColor(Color.white);

   g2d.fillRect(x - bodyWidth/2, y, bodyWidth, bodyLength);

   g2d.setColor(Color.red);

   int armCentreX = x + bodyWidth/2;

   int armCentreY = y + bodyLength - armLength/2;

   g2d.drawLine(x, y, armCentreX, armCentreY); 

   g2d.drawLine(x+bodyWidth, y, armCentreX + armLength, armCentreY);

   g2d.setColor(Color.blue);

   if (armGrabbed) { // 机器人抓住某物

     g2d.drawLine(armCentreX, armCentreY, armX, armY);

   } else { // 机器人手臂自动运动

     g2d.drawLine(armCentreX, armCentreY, 

      armCentreX + (int) (armLength * Math.cos(Math.toRadians(armX))),

      armCentreY + (int) (armLength * Math.sin(Math.toRadians(armX))));

   }

  }

  // MouseListener 实现方法,此处略去

  public void mouseClicked(MouseEvent e) { }

  public void mouseEntered(MouseEvent e) { }

  public void mouseExited(MouseEvent e) { }

  public void mousePressed(MouseEvent e) {

   if (e.getButton() == MouseEvent.BUTTON1) // 点击鼠标左键

     armGrabbed = true;

   

  }

  public void mouseReleased(MouseEvent e) {

   if (e.getButton() == MouseEvent.BUTTON1) // 松开鼠标左键

     armGrabbed = false;

   

  }

  // MouseMotionListener 实现方法,此处略去

  public void mouseDragged(MouseEvent e) {

   if (armGrabbed) {

     armX = e.getX();

     armY = e.getY();

   }

  }

  public void mouseMoved(MouseEvent e) { }

  public static void main(String[] args) {

   JFrame frame = new JFrame("My Robot");

   frame.setContentPane(new Robot());

   frame.setSize(350, 250);

   frame.setVisible(true);

   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

}

2. 简单计算器

这个简单计算器程序有一个文本框,用于用户输入算术表达式,以及一些按钮,用于输入操作符和数字。程序通过解析表达式和执行相应运算来显示结果。


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator implements ActionListener {

  private JFrame frame;

  private JTextField textField;

  private double memory = Double.NaN;

  private char lastOperator;

  public Calculator() {

   frame = new JFrame("Calculator");

   textField = new JTextField();

   JButton button0 = new JButton("0");

   JButton button1 = new JButton("1");

   JButton button2 = new JButton("2");

   JButton button3 = new JButton("3");

   JButton button4 = new JButton("4");

   JButton button5 = new JButton("5");

   JButton button6 = new JButton("6");

   JButton button7 = new JButton("7");

   JButton button8 = new JButton("8");

   JButton button9 = new JButton("9");

   JButton buttonAdd = new JButton("+");

   JButton buttonSubtract = new JButton("-");

   JButton buttonMultiply = new JButton("*");

   JButton buttonDivide = new JButton("/");

   JButton buttonEquals = new JButton("=");

   JButton buttonClear = new JButton("C");

   button0.addActionListener(this);

   button1.addActionListener(this);

   button2.addActionListener(this);

   button3.addActionListener(this);

   button4.addActionListener(this);

   button5.addActionListener(this);

   button6.addActionListener(this);

   button7.addActionListener(this);

   button8.addActionListener(this);

   button9.addActionListener(this);

   buttonAdd.addActionListener(this);

   buttonSubtract.addActionListener(this);

   buttonMultiply.addActionListener(this);

   buttonDivide.addActionListener(this);

   buttonEquals.addActionListener(this);

   buttonClear.addActionListener(this);

   JPanel buttonPanel = new JPanel();

   buttonPanel.setLayout(new GridLayout(4,4));

   buttonPanel.add(button1);

   buttonPanel.add(button2);

   buttonPanel.add(button3);

   buttonPanel.add(buttonAdd);

   buttonPanel.add(button4);

   buttonPanel.add(button5);

   buttonPanel.add(button6);

   buttonPanel.add(buttonSubtract);

   buttonPanel.add(button7);

   buttonPanel.add(button8);

   buttonPanel.add(button9);

   buttonPanel.add(buttonMultiply);

   buttonPanel.add(buttonClear);

   buttonPanel.add(button0);

   buttonPanel.add(buttonEquals);

   buttonPanel.add(buttonDivide);

   frame.add(textField, BorderLayout.NORTH);

   frame.add(buttonPanel, BorderLayout.CENTER);

   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   frame.setSize(200, 250);

   frame.setVisible(true); 

  }

  public void actionPerformed(ActionEvent event) {

   String text = textField.getText();

   String button_text = event.getActionCommand();

   if (button_text.equals("C")) { // 清空文本框

     memory = Double.NaN;

     textField.setText("");

   }

   else if (button_text.equals("+") || button_text.equals("-") ||

        button_text.equals("*") || button_text.equals("/")) { // 执行运算

     char operator = button_text.charAt(0);

     if (!Double.isNaN(memory)) {

      double num = Double.parseDouble(text);

      switch (lastOperator) {

        case '+': memory += num; break;

        case '-': memory -= num; break;

        case '*': memory *= num; break;

        case '/': memory /= num; break;

      }

      textField.setText("" + memory);

     } else {

      memory = Double.parseDouble(text);

     }

     lastOperator = operator;

   }

   else if (button_text.equals("=")) { // 显示运算结果

     double num = Double.parseDouble(text);

     switch (lastOperator) {

      case '+': memory += num; break;

      case '-': memory -= num; break;

      case '*': memory *= num; break;

      case '/': memory /= num; break;

     }

     textField.setText("" + memory);

     lastOperator = '=';

     memory = Double.NaN;

   }

   else { // 添加数字到文本框

     textField.setText(text + button_text);

   }

  }

  public static void main(String[] args) {

   Calculator calc = new Calculator();

  }

}

3. 数组操作

这个程序通过创建一个整数数组并调用一些Java数组函数来演示一些数组操作。它将显示数组中每个元素的值,并使用Arrays类的sort函数将它们排序。


import java.util.*;

public class ArrayOperations {

  public static void main(String[] args) {

   int[] numbers = new int[10];

   

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

     numbers[i] = (int) (Math.random() * 100);

   }

   System.out.print("Original Array: ");

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

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

   }

   Arrays.sort(numbers); // 排序

   System.out.print("\nSorted Array : ");

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

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

   }

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复