Driver 和方法似乎无法协同工作
Driver and method dont seem to be working toghether
我有一个学校项目,我们需要创建一个程序来将数字转换为二进制,但我似乎无法让它们协同工作。他们会编译但实际上不会得到正确的答案我的 toString() 方法有效它只是没有得到用户输入的小数或来自 convertToBinary 的二进制所以我不确定它失败的地方。任何帮助都会很棒。 Driver 和下面的方法!谢谢!
Driver:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class DecimalConverter extends JPanel{
//Sets up the Window
public DecimalConverter(){
JFrame window = new JFrame("Binary To Decimal");
//exit program when window closes
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//WINDOW COMPONENETS:
JLabel lblPrompt = new JLabel("Enter your number to convert: ");
JLabel lblBinary = new JLabel("Binary: ");
JTextField txtDecimal = new JTextField();
JButton btnToBinary = new JButton("To Binary");
//SET POSITIONS
lblPrompt.setBounds(40, 40, 200, 30);
txtDecimal.setBounds(250, 40, 100, 30);
lblBinary.setBounds(40, 80, 300, 30);
btnToBinary.setBounds(250, 120, 100, 30);
setLayout(null);
add(lblPrompt);
add(txtDecimal);
add(lblBinary);
add(btnToBinary);
window.add(this);
window.setSize(400, 200);
window.setVisible(true);
//Event for button
btnToBinary.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str = txtDecimal.getText();
DecimalBinary b = new DecimalBinary();
lblBinary.setText(b.toString());
}//ends Action Performed
}//Ends ActionListener
);//End Event
}//End Constructor
public static void main(String args[]){
new DecimalConverter();
}//ends main
}//End Class
方法:
class DecimalBinary{
private String decimal = "0";
private String binary = "";
private int dec;
public void setDecimal(String decimal){
int dec = Integer.parseInt(decimal);
convertToBinary(dec);
}
public String convertToBinary(int dec){
int pow = 128;
while (dec > 0){
if (dec >= pow){
binary += "1";
dec = dec - pow;
}
else if (dec < pow){
binary += "0";
}
pow = pow / 2;
}
return decimal;
}
public String toString(){
return decimal + " is " + binary + " in binary";
}
}
按如下方式更改您的代码 (// added
)
public void setDecimal(String decimal){
this.decimal = decimal // added
int dec = Integer.parseInt(decimal);
convertToBinary(dec);
}
public void actionPerformed(ActionEvent e){
String str = txtDecimal.getText();
DecimalBinary b = new DecimalBinary();
b.setDecimal(str) // added
lblBinary.setText(b.toString());
}//ends Action Performed
我有一个学校项目,我们需要创建一个程序来将数字转换为二进制,但我似乎无法让它们协同工作。他们会编译但实际上不会得到正确的答案我的 toString() 方法有效它只是没有得到用户输入的小数或来自 convertToBinary 的二进制所以我不确定它失败的地方。任何帮助都会很棒。 Driver 和下面的方法!谢谢!
Driver:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class DecimalConverter extends JPanel{
//Sets up the Window
public DecimalConverter(){
JFrame window = new JFrame("Binary To Decimal");
//exit program when window closes
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//WINDOW COMPONENETS:
JLabel lblPrompt = new JLabel("Enter your number to convert: ");
JLabel lblBinary = new JLabel("Binary: ");
JTextField txtDecimal = new JTextField();
JButton btnToBinary = new JButton("To Binary");
//SET POSITIONS
lblPrompt.setBounds(40, 40, 200, 30);
txtDecimal.setBounds(250, 40, 100, 30);
lblBinary.setBounds(40, 80, 300, 30);
btnToBinary.setBounds(250, 120, 100, 30);
setLayout(null);
add(lblPrompt);
add(txtDecimal);
add(lblBinary);
add(btnToBinary);
window.add(this);
window.setSize(400, 200);
window.setVisible(true);
//Event for button
btnToBinary.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str = txtDecimal.getText();
DecimalBinary b = new DecimalBinary();
lblBinary.setText(b.toString());
}//ends Action Performed
}//Ends ActionListener
);//End Event
}//End Constructor
public static void main(String args[]){
new DecimalConverter();
}//ends main
}//End Class
方法:
class DecimalBinary{
private String decimal = "0";
private String binary = "";
private int dec;
public void setDecimal(String decimal){
int dec = Integer.parseInt(decimal);
convertToBinary(dec);
}
public String convertToBinary(int dec){
int pow = 128;
while (dec > 0){
if (dec >= pow){
binary += "1";
dec = dec - pow;
}
else if (dec < pow){
binary += "0";
}
pow = pow / 2;
}
return decimal;
}
public String toString(){
return decimal + " is " + binary + " in binary";
}
}
按如下方式更改您的代码 (// added
)
public void setDecimal(String decimal){
this.decimal = decimal // added
int dec = Integer.parseInt(decimal);
convertToBinary(dec);
}
public void actionPerformed(ActionEvent e){
String str = txtDecimal.getText();
DecimalBinary b = new DecimalBinary();
b.setDecimal(str) // added
lblBinary.setText(b.toString());
}//ends Action Performed