java程序题目:输入一个十进制整数,分别以二进制、八进制、十六进制形式输出该整数,图形界面文本框竖排

2024-11-29 04:03:19
推荐回答(2个)
回答(1):

public class Calc extends Frame implements ActionListener {
TextField text1, text3, text4, text5;
Button btn;

public Calc() {
setLayout(new GridLayout(7, 3));
// 第一行
text1 = new TextField(10);
add(text1);

btn = new Button("do");
btn.addActionListener(this);
add(btn);

text3 = new TextField(30);
add(text3);

text4 = new TextField(30);
add(text4);

text5 = new TextField(30);
add(text5);

}

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btn)) {
text3.setText("二进制数是:" + String.valueOf(Integer.toBinaryString(Integer.valueOf(text1.getText()))));
text4.setText("八进制数是:" + String.valueOf(Integer.toOctalString(Integer.valueOf(text1.getText()))));
text5.setText("十六进制数是:" + String.valueOf(Integer.toHexString(Integer.valueOf(text1.getText()))).toUpperCase());
}
}

public static void main(String[] agrs) {
Calc my = new Calc();
my.setSize(250, 150);
my.setVisible(true);
}

}

回答(2):

public static void main(String args[])
{
int a;

System.out.println("输入一个整数:");

Scanner s=new Scanner(System.in);
a=s.nextInt();

System.out.println("二进制:"+Integer.toBinaryString(a));
System.out.println("八进制:"+Integer.toOctalString(a));
System.out.println("十六进制:"+Integer.toHexString(a));
}
记得采纳哦~!