簡體   English   中英

計算器不能有小數

[英]Calculator Can't have Decimal

我的代碼現在可以求和,減,乘,除。 但是,當我輸入10.0和2.0或5.4和2時,它不顯示小數。應如何更改,然后將其用於小數?

這是我的代碼。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Calculator extends JFrame implements ActionListener {

    // instance variables
    JLabel num1Label = null;
    JTextField num1Text = null;
    JLabel num2Label = null;
    JTextField num2Text = null;
    JButton sumButton = null;
    JButton multipleButton=null;
    JButton divideButton=null;
    JButton subtractButton = null;
    JLabel ansLabel = null;

    // constructor
    public Calculator() {
        setLayout(new GridBagLayout());    // set layout (how elements are arranged in window)
        // ****  label:  num 1
        GridBagConstraints layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 0;
        layoutCon.insets = new Insets(10,10,10,10);

        num1Label = new JLabel("Num 1:");
        add(num1Label,layoutCon);   // added label to JFrame


        // ****  text field:  num 1
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 1;
        layoutCon.gridy = 0;
        layoutCon.insets = new Insets(10,10,10,10);

        num1Text = new JTextField(20);
        add(num1Text, layoutCon);

        // ****  label:  num 2
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 1;
        layoutCon.insets = new Insets(10,10,10,10);

        num2Label = new JLabel("Num 2:");
        add(num2Label,layoutCon);   // added label to JFrame


        // ****  text field: num 2
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 1;
        layoutCon.gridy = 1;
        layoutCon.insets = new Insets(10,10,10,10);

        num2Text = new JTextField(20);
        add(num2Text, layoutCon);




        // ****  Sum Button
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 2;
        layoutCon.insets = new Insets(10,10,10,10);

        sumButton = new JButton("Sum");
        add(sumButton, layoutCon);
        sumButton.addActionListener(this);  // register sumButton with the ActionListener in Calculator

        // Multiple Button
        layoutCon= new GridBagConstraints();
        layoutCon.gridx=0;
        layoutCon.gridy=4;
        layoutCon.insets=new Insets(10,10,10,10);

        multipleButton= new JButton("Multiple");
        add(multipleButton, layoutCon);
        multipleButton.addActionListener(this);

        // Divide Button
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 5;
        layoutCon.insets = new Insets(10,10,10,10);

        divideButton = new JButton("Divide");
        add(divideButton, layoutCon);
        divideButton.addActionListener(this);

        // ****  label:  answer
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 1;
        layoutCon.gridy = 2;
        layoutCon.insets = new Insets(10,10,10,10);

        ansLabel = new JLabel("Answer");
        add(ansLabel,layoutCon);   // added label to JFrame  





        // ****  Subtract Button
        layoutCon = new GridBagConstraints();
        layoutCon.gridx = 0;
        layoutCon.gridy = 3;
        layoutCon.insets = new Insets(10,10,10,10);

        subtractButton = new JButton("Subtract");
        add(subtractButton, layoutCon);
        subtractButton.addActionListener(this);  // register subtractButton with the ActionListener in Calculator

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Sum")) {
            ansLabel.setText("" + (Integer.parseInt(num1Text.getText()) + Integer.parseInt(num2Text.getText())));
        }
        else if (e.getActionCommand().equals("Subtract")) {
            ansLabel.setText("" + (Integer.parseInt(num1Text.getText()) - Integer.parseInt(num2Text.getText())));
        }
        else if (e.getActionCommand().equals("Multiple")){
            ansLabel.setText(""+(double)(Integer.parseInt(num1Text.getText())*Integer.parseInt(num2Text.getText())));
        }
        else if (e.getActionCommand().equals("Divide")){
            ansLabel.setText(""+(Integer.parseInt(num1Text.getText())/Integer.parseInt(num2Text.getText())));

        }
        }

    public static void main(String[] args) {
        Calculator calc = new Calculator();

        calc.pack();    // resizes window (JFrame) so you can see the elements in it
        calc.setVisible(true);   // make window visible
        calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit program when close window
    }

}

actionPerformed正在將其輸入解析為int並對其進行操作。 如果要使用浮點數,請嘗試使用double精度代替。

解析為整數不能處理雙精度數或任何浮點數(十進制),因為這樣會更改您的actionPerformed來解析雙精度數或解析int:

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Sum")) {
        ansLabel.setText("" + (Double.parseDouble(num1Text.getText()) + Double.parseDouble(num2Text.getText())));
    }
    else if (e.getActionCommand().equals("Subtract")) {
        ansLabel.setText("" + (Double.parseDouble(num1Text.getText()) - Double.parseDouble(num2Text.getText())));
    }
    else if (e.getActionCommand().equals("Multiple")){
        ansLabel.setText(""+(double)(Double.parseDouble(num1Text.getText())*Double.parseDouble(num2Text.getText())));
    }
    else if (e.getActionCommand().equals("Divide")){
        ansLabel.setText(""+(Double.parseDouble(num1Text.getText())/Double.parseDouble(num2Text.getText())));

    }
}

您應該對4個操作使用雙精度數據類型而不是整數

像這樣:

   public void actionPerformed( ActionEvent e )
   {
      if( e.getActionCommand().equals( "Sum" ) )
         ansLabel.setText( "" + ( Double.
                 parseDouble(num1Text.getText() ) + Double.parseDouble( 
                         num2Text.getText() ) ) );
   // etc.

你用

Integer.parseInt(num1Text.getText());

將一個String解析為一個int。

Double.parseDouble(num1Text.getText());

你得到雙倍。

請注意,您可能想捕獲NumberFormatException ,因為大多數情況下您都不希望程序在格式錯誤的用戶輸入上停止:

double num1;
try
{
    num1 = Double.parseDouble(num1Text.getText());
} catch (NumberFormatException e)
{
    // either use a default value or give feedback to the user on what he did wrong
    // for example
    num1 = 1;
}

如果您不想打印雙精度值,如果用戶僅輸入整數值,則可以首先嘗試將其解析為int,捕獲異常(如果有),然后解析為雙精度。


分界線中的另一個錯誤:

ansLabel.setText(
    ""+(Integer.parseInt(num1Text.getText())/Integer.parseInt(num2Text.getText())));

您無需檢查第二個參數是否為!= 0,否則可以除以0!

為了使代碼更具可讀性,您可能需要創建一個額外的方法來將String解析為數字:

private static double parseDouble(String s)
{
    double num;
    try
    {
        num = Double.parseDouble(num1Text.getText());
    } catch (NumberFormatException e)
    {
        // either use a default value or give feedback to the user on what he did wrong
        // for example
        num = 1;
    }

    return num;
}

除以0的可能解決方法是:

double num1 = parseDouble(num1Text.getText());
double num2 = parseDouble(num2Text.getText());

String command = e.getActionCommand();

if (command.equals("Sum"))
{
    ansLabel.setText("" + (num1 + num2));
} else if (command.equals("Subtract"))
{
    ansLabel.setText("" + (num1 - num2));
} else if (command.equals("Multiple"))
{
    ansLabel.setText("" + (num1 * num2));
} else if (command.equals("Divide"))
{
    if (num2 == 0)
    {
        ansLabel.setText("ERROR: Division by zero!");
    } else
    {
        ansLabel.setText("" + (num1 / num2));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM