Compare commits
No commits in common. 'master2' and 'main' have entirely different histories.
@ -1,106 +0,0 @@
|
|||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
public class Counter2158 extends JFrame implements ActionListener {
|
|
||||||
private double result;
|
|
||||||
private JTextField resultField;
|
|
||||||
|
|
||||||
public Counter2158() {
|
|
||||||
super("Simple Calculator");
|
|
||||||
initializeCalculator();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initializeCalculator() {
|
|
||||||
this.result = 0;
|
|
||||||
|
|
||||||
// 创建布局
|
|
||||||
setLayout(new BorderLayout());
|
|
||||||
|
|
||||||
// 创建结果显示区域
|
|
||||||
resultField = new JTextField("0", 10);
|
|
||||||
resultField.setEditable(false);
|
|
||||||
add(resultField, BorderLayout.NORTH);
|
|
||||||
|
|
||||||
// 创建按钮面板
|
|
||||||
JPanel buttonPanel = new JPanel();
|
|
||||||
buttonPanel.setLayout(new GridLayout(4, 4));
|
|
||||||
|
|
||||||
// 添加按钮
|
|
||||||
String[] buttons = {"+", "-", "*", "/", "Reset", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
|
|
||||||
for (String buttonText : buttons) {
|
|
||||||
JButton button = new JButton(buttonText);
|
|
||||||
button.addActionListener(this);
|
|
||||||
buttonPanel.add(button);
|
|
||||||
}
|
|
||||||
|
|
||||||
add(buttonPanel, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
// 设置窗口属性
|
|
||||||
setSize(300, 200);
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
setLocationRelativeTo(null);
|
|
||||||
setVisible(true); // 显示窗口
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
String command = e.getActionCommand();
|
|
||||||
|
|
||||||
switch (command) {
|
|
||||||
case "+":
|
|
||||||
add(Double.parseDouble(resultField.getText()));
|
|
||||||
break;
|
|
||||||
case "-":
|
|
||||||
subtract(Double.parseDouble(resultField.getText()));
|
|
||||||
break;
|
|
||||||
case "*":
|
|
||||||
multiply(Double.parseDouble(resultField.getText()));
|
|
||||||
break;
|
|
||||||
case "/":
|
|
||||||
try {
|
|
||||||
divide(Double.parseDouble(resultField.getText()));
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "Reset":
|
|
||||||
reset();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
add(Double.parseDouble(command));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
resultField.setText(String.valueOf(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void add(double num) {
|
|
||||||
result += num;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void subtract(double num) {
|
|
||||||
result -= num;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void multiply(double num) {
|
|
||||||
result *= num;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void divide(double num) {
|
|
||||||
if (num == 0) {
|
|
||||||
throw new IllegalArgumentException("Divisor cannot be zero.");
|
|
||||||
}
|
|
||||||
result /= num;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reset() {
|
|
||||||
result = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SwingUtilities.invokeLater(Counter2158::new);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
|
|
||||||
//TIP 要<b>运行</b>代码,请按 <shortcut actionId="Run"/> 或
|
|
||||||
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
|
|
||||||
public class Main {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
//TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
|
|
||||||
// 查看 IntelliJ IDEA 建议如何修正。
|
|
||||||
System.out.print("Hello and welcome!");
|
|
||||||
|
|
||||||
for (int i = 1; i <= 5; i++) {
|
|
||||||
//TIP 按 <shortcut actionId="Debug"/> 开始调试代码。我们已经设置了一个 <icon src="AllIcons.Debugger.Db_set_breakpoint"/> 断点
|
|
||||||
// 但您始终可以通过按 <shortcut actionId="ToggleLineBreakpoint"/> 添加更多断点。
|
|
||||||
System.out.println("i = " + i);
|
|
||||||
}}}
|
|
@ -1,99 +0,0 @@
|
|||||||
import javax.swing.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Transform2158{
|
|
||||||
|
|
||||||
public static String convertBase(String input, int fromBase, int toBase) {
|
|
||||||
try {
|
|
||||||
int number = Integer.parseInt(input, fromBase);
|
|
||||||
return Integer.toString(number, toBase);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isValidBase(int base) {
|
|
||||||
return base == 2 || base == 8 || base == 10 || base == 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
if (args.length > 0 && args[0].equals("cli")) {
|
|
||||||
runCLI();
|
|
||||||
} else {
|
|
||||||
SwingUtilities.invokeLater(() -> new Transform2158().createAndShowGUI());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void runCLI() {
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
System.out.print("请输入输入数字: ");
|
|
||||||
String input = scanner.nextLine();
|
|
||||||
|
|
||||||
System.out.print("请输入从进制 (2, 8, 10, 16): ");
|
|
||||||
int fromBase = scanner.nextInt();
|
|
||||||
|
|
||||||
System.out.print("请输入到进制 (2, 8, 10, 16): ");
|
|
||||||
int toBase = scanner.nextInt();
|
|
||||||
|
|
||||||
if (isValidBase(fromBase) && isValidBase(toBase)) {
|
|
||||||
String result = convertBase(input, fromBase, toBase);
|
|
||||||
if (result != null) {
|
|
||||||
System.out.println("转换结果: " + result);
|
|
||||||
} else {
|
|
||||||
System.out.println("输入的数字在指定的进制中无效。");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
System.out.println("无效的进制,请输入 (2, 8, 10, 16)。");
|
|
||||||
}
|
|
||||||
|
|
||||||
scanner.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createAndShowGUI() {
|
|
||||||
JFrame frame = new JFrame("进制转换器");
|
|
||||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
frame.setSize(300, 200);
|
|
||||||
frame.setLayout(null);
|
|
||||||
|
|
||||||
JButton button = new JButton("开始转换");
|
|
||||||
button.setBounds(80, 70, 140, 30);
|
|
||||||
button.addActionListener(new ConvertAction());
|
|
||||||
frame.add(button);
|
|
||||||
|
|
||||||
frame.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ConvertAction implements ActionListener {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
String input = JOptionPane.showInputDialog("请输入输入数字:");
|
|
||||||
if (input == null) return;
|
|
||||||
|
|
||||||
String fromBaseStr = JOptionPane.showInputDialog("请输入从进制 (2, 8, 10, 16):");
|
|
||||||
if (fromBaseStr == null) return;
|
|
||||||
|
|
||||||
String toBaseStr = JOptionPane.showInputDialog("请输入到进制 (2, 8, 10, 16):");
|
|
||||||
if (toBaseStr == null) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
int fromBase = Integer.parseInt(fromBaseStr);
|
|
||||||
int toBase = Integer.parseInt(toBaseStr);
|
|
||||||
|
|
||||||
if (isValidBase(fromBase) && isValidBase(toBase)) {
|
|
||||||
String result = convertBase(input, fromBase, toBase);
|
|
||||||
if (result != null) {
|
|
||||||
JOptionPane.showMessageDialog(null, "转换结果: " + result);
|
|
||||||
} else {
|
|
||||||
JOptionPane.showMessageDialog(null, "输入的数字在指定的进制中无效。");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
JOptionPane.showMessageDialog(null, "无效的进制,请输入 (2, 8, 10, 16)。");
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException ex) {
|
|
||||||
JOptionPane.showMessageDialog(null, "输入的数字在指定的进制中无效。");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Transform4381 {
|
|
||||||
|
|
||||||
//此代码不实现界面
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
|
|
||||||
System.out.print("请输入输入数字: ");
|
|
||||||
String input = scanner.nextLine();
|
|
||||||
|
|
||||||
System.out.print("请输入从进制 (2, 8, 10, 16): ");
|
|
||||||
int fromBase = scanner.nextInt();
|
|
||||||
|
|
||||||
System.out.print("请输入到进制 (2, 8, 10, 16): ");
|
|
||||||
int toBase = scanner.nextInt();
|
|
||||||
|
|
||||||
try {
|
|
||||||
int number = Integer.parseInt(input, fromBase);
|
|
||||||
String result = Integer.toString(number, toBase);
|
|
||||||
System.out.println("转换结果: " + result);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
System.out.println("输入的数字在指定的进制中无效。");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue