繁体   English   中英

十进制,二进制,八进制,十六进制转换中的错误

[英]errors in conversion decimal,binary,octal,hex

我有一个正在处理的代码,我想为bin,八进制,十六进制和十进制构建一个转换器。 我有一个框架,其中有一个文本空间,您可以在其中输入任何一个选项。 根据输入内容的不同,程序会将其转换为其他形式。 我的问题是,它可以将小数转换为其他形式,反之亦然吗? 我很困惑,因为我认为逻辑流程就在那里,对此编码器将不胜感激。

import javax.swing.*; //library used for the layout
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Gui extends JFrame implements ActionListener{  

JLabel l1 = new JLabel("   Decimal   ");
JLabel l2 = new JLabel("    Binary   ");
JLabel l3 = new JLabel("    Octal    "); 
JLabel l4 = new JLabel(" Hexadecimal ");
JTextField f1 = new JTextField(20);
JTextField f2 = new JTextField(20);
JTextField f3 = new JTextField(20);
JTextField f4 = new JTextField(20);
JButton b1 = new JButton("Calculate");

public Gui() {
    setLayout(new FlowLayout()); //row x column

    add(l1);
    add(f1);
    add(l2);
    add(f2);        
    add(l3);
    add(f3);
    add(l4);
    add(f4);
    add(b1);
    b1.addActionListener(this);
}

public void actionPerformed(ActionEvent e){
    if(e.getSource()==this.b1){
        if(this.f1.getText()!="")
        {
            String text1 = this.f1.getText();//get the text and store it in a string named text1
            int num1 = Integer.parseInt(text1); // get the integer value of the variable text1

            String binary = Integer.toBinaryString(num1); //convert the variable num1 to binary
            this.f2.setText(binary);

            String octal = Integer.toOctalString(num1); //convert the variable num1 to binary
            this.f3.setText(octal);

            String hexadecimal = Integer.toHexString(num1); //convert the variable num1 to binary
            this.f4.setText(hexadecimal);       
        } 
    }
}
}

该行期望字符串为十进制。

int num1 = Integer.parseInt(text1);

如果文本不总是十进制,则需要一种方法供用户指定文本的格式。

您可以使用radix参数解析二进制,八进制和十六进制。

解析二进制:

int num1 = Integer.parseInt(text,2);

解析八进制:

int num1 = Integer.parseInt(text,8);

解析十六进制:

int num1 = Integer.parseInt(text,16);

要指定使用哪个,可以有多个按钮,也可以向每个JTextField添加一个ActionListener。 每个人都从该字段中读取文本并设置其他值。

我很困惑,因为我认为逻辑流程在那里,

不,您拥有的这种逻辑会查看f1字段并将该十进制值转换为其他格式。

根据输入内容的不同,程序会将其转换为其他形式。

我想您想要实现的是具有相应形式的任何形式的值,并在用户按下“计算”按钮时将其转换为其他格式。

因此,您将决定如何执行此操作,

  1. 您想删除计算按钮并更新字段以侦听每个JTextField的更改的天气。

  2. 保留“计算”按钮,并假定最后更新的JTextField是用于转换为其他格式的JTextField。

在任一情况下,

您将不得不听JTextField的更改。 使用,

 // Listen for changes in the text f1
     f1.getDocument().addDocumentListener(new DocumentListener() {
      public void changedUpdate(DocumentEvent e) {
        update();
      }
      public void removeUpdate(DocumentEvent e) {
        update();
      }
      public void insertUpdate(DocumentEvent e) {
        update();
      }

      public void update() {
         // update a local variable to keep the last updated JTextFeild 
         // or update to the rest of the formats on the fly.
         // You can update/ show errors of the input values 

         //Scenario 1,

         int decimal = Integer.parseInt(f1.getText());
         String binary = Integer.toBinaryString(decimal); //convert the variable num1 to binary
         this.f2.setText(binary);

         String octal = Integer.toOctalString(decimal); //convert the variable num1 to binary
         this.f3.setText(octal);

         String hexadecimal = Integer.toHexString(decimal); //convert the variable num1 to binary
         this.f4.setText(hexadecimal);  
      }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM