21xrx.com
2025-04-09 17:01:19 Wednesday
文章检索 我的文章 写文章
Java程序设计实验指导书 详细答案下载和学习指南
2023-06-18 09:51:56 深夜i     9     0
Java程序设计 实验指导书 答案下载 学习指南

在学习Java程序设计实验过程中,答案的获取和学习指导是至关重要的一环。为帮助读者更好地学习Java程序设计实验,笔者搜集了一些该书实验题的详细答案,供读者参考。以下是该书中部分实验题的详细答案和一些学习指南。

1、Lab1-1 使用notepad编写helloworld.java程序

编写此程序的主要目的是测试Java的环境变量是否配好,代码如下:

public class helloworld {
  public static void main(String[] args) {
   System.out.println("Hello World!"); // 输出 Hello World!
  }
}

2、Lab1-2 使用记事本编写并运行简单的控制台应用程序

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
  public static void main(String[] args) throws IOException {
    System.out.println("请输入你的名字:");
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String name = reader.readLine().trim();
    System.out.println("欢迎你," + name + "!");
  }
}

3、Lab2-3 完成计算器的设计

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame {
  private JPanel pan = new JPanel(); // 定义面板
  private JTextField txt = new JTextField("0"); // 显示文本框
  private JButton[] btns = new JButton[16]; // 按钮数组
  private double n1 = 0, n2 = 0, ans = 0; // 运算参数和结果
  private boolean flag = false; // 运算符标志
  private String op = ""; // 运算符
  public Calculator() {
    super("Calculator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(240, 260);
    GridLayout grid = new GridLayout(5, 4);
    pan.setLayout(grid);
    for (int i = 0; i < 10; i++)
      btns[i] = new JButton(String.valueOf(i));
    btns[10] = new JButton("C");
    btns[11] = new JButton(".");
    btns[12] = new JButton("+");
    btns[13] = new JButton("-");
    btns[14] = new JButton("*");
    btns[15] = new JButton("/");
    btns[10].setForeground(Color.red);
    for (int i = 0; i < 16; i++) {
      btns[i].setFont(new Font("楷体", Font.PLAIN, 20));
      pan.add(btns[i]);
    }
    txt.setHorizontalAlignment(SwingConstants.RIGHT);
    txt.setFont(new Font("楷体", Font.PLAIN, 24));
    txt.setEditable(false);
    pan.add(txt);
    setContentPane(pan);
    setVisible(true);
    for (int i = 0; i < 16; i++) {
      btns[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          String str = ((JButton) e.getSource()).getText();
          if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) {
            if (!flag) { // 如果没有输入运算符
              n1 = Double.parseDouble(txt.getText());
              flag = true;
              op = str;
              txt.setText("");
            } else { // 如果已经输入运算符,执行上一个运算符
              n2 = Double.parseDouble(txt.getText());
              ans = compute(op, n1, n2);
              n1 = ans;
              n2 = 0;
              op = str;
              txt.setText(String.valueOf(ans));
            }
          } else if (str.equals(".")) {
            if (!txt.getText().contains(".")) {
              txt.setText(txt.getText() + ".");
            }
          } else if (str.equals("C")) {
            n1 = 0;
            n2 = 0;
            ans = 0;
            flag = false;
            op = "";
            txt.setText("");
          } else { // 按下数字键
            if (flag) {
              txt.setText("");
              flag = false;
            }
            txt.setText(txt.getText() + str);
          }
        }
      });
    }
  }
  public static void main(String[] args) {
    new Calculator();
  }
  double compute(String op, double a, double b) { // 进行双个数的四则运算
    double ans = 0;
    if (op.equals("+")) {
      ans = a + b;
    } else if (op.equals("-"))
      ans = a - b;
     else if (op.equals("*")) {
      ans = a * b;
    } else if (op.equals("/")) {
      if (b == 0) {
        JOptionPane.showMessageDialog(null, "除数不能为零!");
        txt.setText("");
      } else
        ans = a / b;
      
    }
    return ans;
  }
}

本文收录了《Java程序设计实验指导书》中部分实验题的详细答案和学习指南,帮助读者更好地学习Java程序设计实验,方法包括使用notepad编写helloworld.java程序,使用记事本编写并运行简单的控制台应用程序,完成计算器的设计等等。 

  
  

评论区

请求出错了