如何在文本框中显示计算的浮点数?

发布于 2025-01-22 01:19:10 字数 2685 浏览 2 评论 0原文

我正在尝试显示“剩下的白天”和“剩下的夜晚” 进行小计算后,

我希望能够从组合框中选择3个软件包中的任何一个 然后,当我单击计算时,它应该进行计算并在文本框中显示结果。

我尝试将浮点值转换为字符串,然后在文本框中显示,它起作用了! 因此,这意味着此计算行不通。

以某种方式计算不起作用,因此它始终显示为0.0,

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        day_left = tot_day_time - day_used;
        night_data = tot_all_time - tot_day_time;
        night_left = night_data - night_used;
        jTextPane3.setText(Float.toString(day_left));jTextPane2.setText(Float.toString(night_left));
    } 

我总是得到0.0

“始终获得0.0”

package slt_package;

public class slt_jframe extends javax.swing.JFrame {
    float tot_day_time;
    float tot_all_time;
    float night_used;
    float day_used;
    float day_left;
    float night_left;
    float night_data;
    
   
    private void package_comboboxActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        int selectedPackage = package_combobox.getSelectedIndex();
        
        switch (selectedPackage) {
            case 0:
                tot_day_time = 85;tot_all_time = 205;
                break;
            case 1:
                tot_day_time = 105;tot_all_time = 260;
                break;
            case 2:
                tot_day_time = 190;tot_all_time = 280;
                break;
            default:
                break;
        }
    }                                                

    private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        day_used=Float.parseFloat(textField1.getSelectedText());   // TODO add your handling code here:
    }                                          

    private void textField2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        night_used=Float.parseFloat(textField2.getSelectedText());
            // TODO add your handling code here:
    }                                          

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        day_left = tot_day_time - day_used;
        night_data = tot_all_time - tot_day_time;
        night_left = night_data - night_used;
        
        jTextPane3.setText(Float.toString(day_left));
        jTextPane2.setText(Float.toString(night_left));
        
    }                                     

}

I'm trying to display the 'Day time left' and 'Night time left'
after doing a small calculation

I want to be able to select any of the 3 packages from the combo box
then when I click calculate it should do the calculation and display the result in the text boxes.

I tried converting a float value to string and then display in the text box, It worked !!
so that means this calculation doesen't work.

Somehow the calculation doesn't work so it always displays the value as 0.0

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        day_left = tot_day_time - day_used;
        night_data = tot_all_time - tot_day_time;
        night_left = night_data - night_used;
        jTextPane3.setText(Float.toString(day_left));jTextPane2.setText(Float.toString(night_left));
    } 

I always get 0.0

Always getting 0.0

package slt_package;

public class slt_jframe extends javax.swing.JFrame {
    float tot_day_time;
    float tot_all_time;
    float night_used;
    float day_used;
    float day_left;
    float night_left;
    float night_data;
    
   
    private void package_comboboxActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        int selectedPackage = package_combobox.getSelectedIndex();
        
        switch (selectedPackage) {
            case 0:
                tot_day_time = 85;tot_all_time = 205;
                break;
            case 1:
                tot_day_time = 105;tot_all_time = 260;
                break;
            case 2:
                tot_day_time = 190;tot_all_time = 280;
                break;
            default:
                break;
        }
    }                                                

    private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        day_used=Float.parseFloat(textField1.getSelectedText());   // TODO add your handling code here:
    }                                          

    private void textField2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        night_used=Float.parseFloat(textField2.getSelectedText());
            // TODO add your handling code here:
    }                                          

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        day_left = tot_day_time - day_used;
        night_data = tot_all_time - tot_day_time;
        night_left = night_data - night_used;
        
        jTextPane3.setText(Float.toString(day_left));
        jTextPane2.setText(Float.toString(night_left));
        
    }                                     

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我为君王 2025-01-29 01:19:10

正如安德鲁(Andrew)所写的那样,如果没有工作程序,很难找到错误。我会仔细检查float.toString方法,如

As Andrew wrote, it's difficult to find the bug without a working program. I would double-check the Float.toString method as seen in Float to String format specifier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文