如何更正此代码才能正常工作 [英] How do I correct this code to work properly

查看:82
本文介绍了如何更正此代码才能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在netbeans中使用这个代码来设计计算器,但是分区符号的问题立即我将数字分开,例如9/8会给我1但是当我将数字分开时,例如。 8/2会给我正确答案,因为没有提醒



  private   void  jBtnDivActionPerformed(java.awt.event.ActionEvent evt){
firstnum = Double.parseDouble(jtxtDisplay.getText());
jtxtDisplay.setText( );
operations = /


< span class =code-keyword> else if (operations == /
{
result = firstnum / secondnum;
answer = 字符串 .format( % .0f,结果);
jtxtDisplay.setText(回答);

解决方案

听起来像是在划分两个整数:



执行整数除法时要小心。将整数除以整数时,答案将是整数(不是舍入的)。



比较这些除法:(5是整数,5.0是双精度)



整数除法8/5 = 1

双师8.0 / 5.0 = 1.6

混合师8.0 / 5 = 1.6



在分割之前,确保至少有一个数字是 double


I used this code in netbeans to design calculator but the is problem with the division sign immediately I divided the number eg 9 / 8 will give me 1 but when i divided the number eg. 8/2 will give me the correct answer since there is no reminder

private void jBtnDivActionPerformed(java.awt.event.ActionEvent evt) {
      firstnum = Double.parseDouble(jtxtDisplay.getText());
      jtxtDisplay.setText("");
      operations="/"


else if (operations == "/")
   {
       result = firstnum / secondnum;
       answer = String.format("%.0f", result);
       jtxtDisplay.setText(answer);

解决方案

Sounds like you're dividing two integers:


Be careful when performing integer division. When dividing an integer by an integer, the answer will be an integer (not rounded).

Compare these divisions: (5 is an integer while 5.0 is a double)

Integer division 8 / 5 = 1
Double division 8.0 / 5.0 = 1.6
Mixed division 8.0 / 5 = 1.6


Make sure at least one of the numbers is a double before dividing.


这篇关于如何更正此代码才能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆