进制转换器java程序_java编写简易的进制转换器

hi,大家好,

这是我的“21天coding不间断”任务的Day 5,

是一个简洁的10进制与2进制互转的算法实现。

import java.util.Scanner;

public class SystemConvert {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("please input a number:");

int numb = scanner.nextInt();

System.out.println("choose a way:\\n输入1,表示10进制转2进制;\\n"

+ "输入2,表示2进制转10进制;\\n");

int input = scanner.nextInt();

String s = "";

switch (input) {

case 1:

for (int i = numb; i > 0; i /= 2)

s += i % 2;

break;

case 2:

int k = 0,

temp = 0;

for (int i = numb; i > 0; i /= 10) {

temp += (i % 10) * Math.pow(2, k);

k++;

}

s = "" + temp;

break;

default:

System.out.println("Wrong input!");

}

System.out.println(s);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个Java编写进制转换的基本框架,包括界面和转换算法。以下是示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class NumberConverter extends JFrame implements ActionListener { private JTextField decimalField; private JTextField binaryField; private JTextField octalField; private JTextField hexadecimalField; public NumberConverter() { // 设置窗口标题 setTitle("进制转换"); // 创建组件 JLabel decimalLabel = new JLabel("十进制:"); decimalField = new JTextField(10); JLabel binaryLabel = new JLabel("二进制:"); binaryField = new JTextField(10); JLabel octalLabel = new JLabel("八进制:"); octalField = new JTextField(10); JLabel hexadecimalLabel = new JLabel("十六进制:"); hexadecimalField = new JTextField(10); JButton convertButton = new JButton("转换"); // 添加组件到面板 JPanel panel = new JPanel(new GridLayout(5, 2)); panel.add(decimalLabel); panel.add(decimalField); panel.add(binaryLabel); panel.add(binaryField); panel.add(octalLabel); panel.add(octalField); panel.add(hexadecimalLabel); panel.add(hexadecimalField); panel.add(new JLabel()); panel.add(convertButton); // 添加事件监听 convertButton.addActionListener(this); decimalField.addActionListener(this); binaryField.addActionListener(this); octalField.addActionListener(this); hexadecimalField.addActionListener(this); // 设置窗口属性 add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 150); setLocationRelativeTo(null); setVisible(true); } // 实现转换算法 public void actionPerformed(ActionEvent e) { try { if (e.getSource() == decimalField) { int decimal = Integer.parseInt(decimalField.getText()); binaryField.setText(Integer.toBinaryString(decimal)); octalField.setText(Integer.toOctalString(decimal)); hexadecimalField.setText(Integer.toHexString(decimal)); } else if (e.getSource() == binaryField) { int decimal = Integer.parseInt(binaryField.getText(), 2); decimalField.setText(Integer.toString(decimal)); octalField.setText(Integer.toOctalString(decimal)); hexadecimalField.setText(Integer.toHexString(decimal)); } else if (e.getSource() == octalField) { int decimal = Integer.parseInt(octalField.getText(), 8); decimalField.setText(Integer.toString(decimal)); binaryField.setText(Integer.toBinaryString(decimal)); hexadecimalField.setText(Integer.toHexString(decimal)); } else if (e.getSource() == hexadecimalField) { int decimal = Integer.parseInt(hexadecimalField.getText(), 16); decimalField.setText(Integer.toString(decimal)); binaryField.setText(Integer.toBinaryString(decimal)); octalField.setText(Integer.toOctalString(decimal)); } } catch (NumberFormatException ex) { // 处理非法输入 JOptionPane.showMessageDialog(this, "请输入有效的数字", "错误", JOptionPane.ERROR_MESSAGE); } } public static void main(String[] args) { new NumberConverter(); } } ``` 这个程序使用Java Swing库创建了一个简单的图形用户界面,有四个文本框用于输入和显示不同进制的数值,并有一个转换按钮。当用户输入一个数字或点击按钮时,程序将执行相应的转换算法,并将结果显示在其他文本框中。 您可以根据自己的需求进行修改和扩展,例如添加其他进制的转换或美化界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值