简体   繁体   中英

Output is incorrect from a math equation with-in java, For the first person and second

When I enter the total amount of tips into the JOptionPane that I have for getting the tips, it will just reprint what I have entered for a jOptionPane on the first person. Even though the math equation is using the rate variable that you get when you divide the total hours by the total tips. The last person calculate by the program is always correct but any of the others is incorrect. I'm not sure what could be causing this to happen I have tried all I know to fix this and I have not found any information online to help me out. I'm using NetBeans to create the gui and I have plug-in my code for each button

private void getHoursActionPerformed(java.awt.event.ActionEvent evt) {                                         
        DecimalFormat monn = new DecimalFormat( "0.00" );

        hourss = blake += barker += kevin += pierce += sam += dolphy += johnC += johnD += slut += tanner += ray += trevor += zachB += zachW += petty += mikey += mitch += marge += mattB += kolb += kyle += jackson += clinton += cody += cam += bailey += bMart += addison;

       tip = JOptionPane.showInputDialog( null, "Total Amount Of Tips?" );

       tips = Double.parseDouble( String.valueOf( tip ) );

       String th = String.valueOf( hourss );

           double hh = Double.parseDouble( String.valueOf(hourss) );

       totalHoursJTextField.setText( th );

       Rate = tips / hh;

           rateJTextField.setText( String.valueOf(monn.format(Rate) ) );

           test1.setText( bp );
           test2.setText( kev );
           test3.setText( bar );
    }                                        

    private void calcBnActionPerformed(java.awt.event.ActionEvent evt) {                                       




    }                                      

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String rrate = rateJTextField.getText();
        double ff = Double.parseDouble( rrate );

        br2 = barker * ff;
        kv2 = kevin * ff;
        bp2 = ff * blake;
        pk = pierce * ff;

        test.setText( tip );

        output.append( "\n" + "Barker" + "\t" + br2 + "\n" + "--------------------" );
        output.append( "\n" + "Kevin" + "\t" + kv2 + "\n" + "--------------------" );
        output.append( "\n" + "Blake" + "\t" + bp2 + "\n" + "--------------------" );
        output.append( "\n" + "Pierce" + "\t" + pk + "\n" + "--------------------" );

该图显示了输出以及费率和总小时数。

Your problem is this line:

hourss = blake += barker += kevin += pierce; // rest omitted

Lets start with these values:

blake = 3.4; barker = 6.3; kevin = 3.2; pierce = 0;

Now if you execute the offending line, you get these values:

hourss == 12.9; blake == 12.9; barker == 9.5; kevin == 3.2; pierce == 0;

With 230 for tips , you get a rate of 17.8294 (rounded to four decimal places), and then

  • for blake: 17.83*12.9 (rounded 230.007) instead of 17.8294*3.4 (rounded 60.62)
  • for barker: 17.83*9.5 (rounded 169.385) instead of 17.8294*6.3 (rounded 112.33)
  • for kevin: 17.83*3.2 (rounded 57.056) instead of 17.8294*3.2 (rounded 57.05)

and if you add together the "instead"-values you get a total of 230...


So you definitely should replace the line

hourss = blake += barker += kevin += pierce += sam += dolphy += johnC += johnD += slut += tanner += ray += trevor += zachB += zachW += petty += mikey += mitch += marge += mattB += kolb += kyle += jackson += clinton += cody += cam += bailey += bMart += addison;

with

hourss = blake + barker + kevin + pierce + sam + dolphy + johnC + johnD + slut + tanner + ray + trevor + zachB + zachW + petty + mikey + mitch + marge + mattB + kolb + kyle + jackson + clinton + cody + cam + bailey + bMart + addison;

Also you do a awful lot of conversions from double to String and back, which costs you precision (and performance, but this is currently not the problem).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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