简体   繁体   中英

Value of integer returning a value of zero

I am trying to run a simple temperature conversion in a JFrame . However the temperature conversion of Fahrenheit to celuis (integer) returns as zero regardless of the Fahrenheit temperature. Code is below followed by the main class:

/*
* ch 11 GUI programming questions
* Q 11.12
*/
package Chapter11GUI;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.ActiveEvent;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class TempConverter extends JFrame {
//declare instance varibles

JLabel label1;
JLabel label2;
JTextField faherinheit;

//construto constrots GUI and habldes events
public TempConverter() {
    super("Temperature Converter");
    setLayout(new BorderLayout());

    label1 = new JLabel("Eneter ferinheit to convert to celuis: ");
    //get usre to enter temp i  fioaherinet and trigger event andler to
    //assign string to a integer value
    //use annomonous class
    faherinheit = new JTextField(10);
    faherinheit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //get numbers enetered
            int temp = Integer.parseInt(faherinheit.getText());
            int celuis = (int) ((5 / 9) * (temp - 32));
            label2.setText("Farinheit Temp " + temp + " in celuis is: " + celuis);




        }//end actionEvent Listener annmonous classs
    });//and eveny handling for textfiel

    //add all compnments to JFrame using birder Layout
    label2 = new JLabel("Temperature in Celuis is : ");
    add(label1, BorderLayout.NORTH);
    add(faherinheit, BorderLayout.CENTER);
    add(label2, BorderLayout.SOUTH);


    }//end constructor
}//end class TempCOnversion

here

int celuis = (int) ((5 / 9) * (temp - 32));

5/9 always gets truncated to 0


I'd advise something more along the lines of

int celuis = (int) ((5.0 / 9.0) * ((double)temp - 32.0));

that way, the truncation won't affect you as bad

Or better yet, don't use int's at all to calculate temperature

5 / 9 integer division is zero, you should do 5.0 / 9 instead

or you can make it (5 * (temp - 32)) / 9 this way you don't do double arithmetic at all and the truncation happens at the end

Use ((5.0 / 9.0) * (temp - 32)) for conversion. When you use (5 / 9) , Java compiler treats it as integer, and gives zero as result

By default, the "/" symbol denotes integer division if the two operands are not of type double or float. To ensure that you receive the desired fractional value, either cast one or both of the operands to type float/double, or make either operands specifically of that type, ie 5.0.

Examples: 5/9 = 0, 5.0/9 = 0.5555, 5/9.0 = 0.5555, 5.0/9.0 = 0.5555, ((float)5)/9 = 0.5555, ((double)5)/9 = 0.5555, 5/((float)9) = 0.5555, 5/((double)9) = 0.5555

This is because you are using int type:

int celuis = (int) ((5 / 9) * (temp - 32));

so, 5/9 is always 0 .

Perform calculations using double :

int celuis = (int) ((5.0 / 9.0) * (temp - 32.0));

int celuis = (int) ((5 / 9) * (temp - 32)); should use decimal values. One way of fixing it is: int celuis = (int) ((5D / 9D) * ((double)temp - 32D));

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