ورود

View Full Version : مشکل کد(فقط صفر بر میگرداند)



olampiad
یک شنبه 10 خرداد 1394, 17:00 عصر
سلام و خسته نباشید به دوستان عزیز
به این کد من ی نگا بندازید.
چرا این کد همش صفر نشان میدهد.
مشکل از کجاست.
مرسی


package javaapplication4;


import javax.swing.JOptionPane;


public class JavaApplication4 {
public static void main(String[] args) {


int c;
int f;
String s;
s=JOptionPane.showInputDialog("enter f");
f= Integer.parseInt(s);
c=(int)(5/9)*(f-32);
JOptionPane.showMessageDialog(null,c);



}
}

ahmad.mo74
یک شنبه 10 خرداد 1394, 17:21 عصر
5 تقسیم بر 9 میشه حدودا 0.5 و مقدار صحیحش میشه 0. پس جواب خط 16 همیشه صفر میشه.

باید حداقل یکی از عدد هارو اعشاری تعریف کنید تا جواب درست رو بده.

String input = JOptionPane.showInputDialog("enter f");int f = Integer.parseInt(input);
float c = 5.f / 9 * (f - 32);
JOptionPane.showMessageDialog(null, c);

این کار هم بکنید قشنگ تره :

static final float FLOAT_CONSTANT = 0.5555556f; // 5 / 9

public static void main(String[] args) {
String input = JOptionPane.showInputDialog("enter f");
int f = Integer.parseInt(input);
float c = FLOAT_CONSTANT * (f - 32);
JOptionPane.showMessageDialog(null, c);
}