第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

驅(qū)動(dòng)程序和方法似乎不能一起工作

驅(qū)動(dòng)程序和方法似乎不能一起工作

拉風(fēng)的咖菲貓 2023-11-10 16:23:06
我有一個(gè)學(xué)校項(xiàng)目,我們需要?jiǎng)?chuàng)建一個(gè)程序?qū)?shù)字轉(zhuǎn)換為二進(jìn)制,但我似乎無法讓它們一起工作。他們會(huì)編譯,但實(shí)際上不會(huì)得到正確的答案,我的 toString() 方法有效,它只是沒有獲取用戶輸入的十進(jìn)制或從 ConvertToBinary 中獲取的二進(jìn)制文件,所以我不確定它在哪里失敗。任何幫助都會(huì)很棒。驅(qū)動(dòng)程序和方法如下!謝謝!司機(jī):import javax.swing.*;import java.awt.*;import java.awt.event.*;class DecimalConverter extends JPanel{//Sets up the Windowpublic 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";  } }
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊

更改您的代碼如下 ( // 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


查看完整回答
反對(duì) 回復(fù) 2023-11-10
  • 1 回答
  • 0 關(guān)注
  • 109 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)