ورود

View Full Version : تبدیل دما



SaidJan
پنج شنبه 20 شهریور 1393, 18:15 عصر
سلام
می‌خوام کاربر دما رو به صورت ۵۲f وارد کنه و دما رو به سانتیگراد تبدیل کنه و برعکس.
نمی‌خوام بین ۵۲f فاصله باشه.
اینو نوشتم اجرا می‌شه، دما رو هم وارد می‌کنم، ولی‌ نتیجه رو نمید. مشکل از کجاس؟
قسمت for رو فک کنم باید حذف کنم و یه چیز دیگه اضاف کنم



import javax.swing.*;
import java.text.*;
import java.util.*;


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


String tcf,cf,a,b;
double tc,tf;


Scanner scan = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");


JOptionPane.showInputDialog(null,"Enter the temp and a c or f (52f):" );
tcf = scan.next();


for(int i=0; i<tcf.length(); i++)


{
if (tcf.charAt(i)=='f'){
cf = tcf.substring (0,tcf.indexOf("f"));
double tt = Double.parseDouble(cf);
tc= tt* (9/5)+32;
System.out.print("The temperature in f is: "+ df.format(tc));


}


}




if (tcf.indexOf("f")!=-1)
{
cf = tcf.substring (0,tcf.indexOf("f"));
double tt = Double.parseDouble(cf);
tc= tt* (9/5)+32;
System.out.print("The temperature in c is: "+ df.format(tc));
}


if (tcf.indexOf("c")!=-1)
{
cf = tcf.substring (0,tcf.indexOf("c"));
double tt = Double.parseDouble(cf);
tf= (tt-32)*(5/9);
System.out.print("The temperature in f is: "+ df.format(tf));
}



}


}

dasssnj
شنبه 22 شهریور 1393, 11:33 صبح
سلام.

مشکلات زیادی که کد شما داشت یکیش این بود که شما دیالوگ باز می کردید ولی مقدار را از کنسول می گرفتید ! که توی کد درستش کردم .
و بقیه ی مشکلاتتون را با دیدن کدی که میزارم متوجه بشید . این کد کاملا درست کار می کنه.

import javax.swing.*;
import java.text.*;
import java.util.*;


public class lab4 {

public static void main (String[] args) {

String tcf,cf,a,b;
double tc,tf;


//Scanner scan = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");

tcf = JOptionPane.showInputDialog(null,"Enter the temp and a c or f (52f):" );
//tcf = scan.next();

if (tcf.contains("c")){
cf = tcf.substring (0,tcf.indexOf("c"));
double tt = Double.parseDouble(cf);
tc= tt* (9f/5f)+32f;
System.out.print("The temperature in f is: "+ df.format(tc));
}

else if (tcf.contains("f"))
{
cf = tcf.substring (0,tcf.indexOf("f"));
double tt = Double.parseDouble(cf);
tf = (tt-32f)*(5f/9f);
System.out.print("The temperature in c is: "+ df.format(tf));
}



}
}