|登录 |注册

查看: 2614|回复: 24
打印 上一主题 下一主题

java 入门功课求救!!

[复制链接]
007pig
2013-12-3 06:54 PM
Question 2

Write a program to perform the conversion between Celsius and Fahrenheit. In your
program, it should contain the following two methods:

/**Convert from Celsius to Fahrenheit**/
public static double celsiusToFahrenheit (double celsius)

/**Convert from Fahrenheit to Celsius **/
public static double fahrenheitToCelsius (double fahrenheit)

formula
Formula to convert Fahrenheit degrees to Celsius:
C = (F - 32) x 5/9
where C = Celsius and F = Fahrenheit
Formula to convert Celsius degrees to Fahrenheit:
F = (C x 9/5) + 32
where C = Celsius and F = Fahrenheit



(Hint: Using for loops and methods to display the above output.)

救命,我努力了2天都写不出结果T.T
buzZsk
2013-12-3 07:12 PM
不明白要foor loop干嘛
还有麻烦你放你的东西上来
我们改
而不是我们做给你吧
007pig
2013-12-3 07:20 PM
buzZsk 发表于 2013-12-3 07:12 PM
不明白要foor loop干嘛
还有麻烦你放你的东西上来
我们改

paiseh==
这是我写的,但一直搞不出结果

public class q2{
        public static void main(String[]args){
               
                double a1=celsiusToFahrenheit(celsius);
                System.out.println("" +a1);
               
                double a2=fahrenheitToCelsius(fahrenheit);
                System.out.println("" +a2);
        }
       
       
        public static double celsiusToFahrenheit (double celsius){
                double result;
                for(result=40;result>31;result++)
                result=(result*9/5)+32;
                return result;
        }
       
       
       
        public static double fahrenheitToCelsius (double fahrenheit){
                double result;
                for(result=120;result>30;result++)
                result=(result-32)*5/9;
                return result;
        }
}
buzZsk
2013-12-3 08:15 PM
007pig 发表于 2013-12-3 07:20 PM
paiseh==
这是我写的,但一直搞不出结果

public class q2{

        public static void main(String[]args){


                double a1=celsiusToFahrenheit(celsius);
                System.out.println("" +a1);
                /*
                错了吧
                function 用法:
                function(value);
                value应该是user自己打
                不然就是你自己放号码
                e.g.:
                celsiusToFahrenheit(50); convert 50 to something else
                */

                //function里面已经直接return了,直接print就好
                System.out.println(fahrenheitToCelsius(fahrenheit);
        }


        public static double celsiusToFahrenheit (double celsius){
                double result;
                for(result=40;result>31;result++)
                result=(result*9/5)+32;
                return result;
        }



        public static double fahrenheitToCelsius (double fahrenheit){
                double result;
                for(result=120;result>30;result++)
                result=(result-32)*5/9;
                return result;
        }
}
slay_alex92
2013-12-3 08:38 PM
本帖最后由 slay_alex92 于 2013-12-3 09:02 PM 编辑
007pig 发表于 2013-12-3 07:20 PM
paiseh==
这是我写的,但一直搞不出结果


說真的你寫的code真的很多錯誤,我直接幫妳寫了一個,可是我不知道哪裡需要用到for =.=
我剛剛compile過了,沒問題
  1. public class q2
  2. {
  3.         public static void main(String[] args){
  4.                 System.out.println(celsiusToFahrenheit(50)); //50的地方可以隨便改別的double
  5.                 System.out.println(fahrenheitToCelsius(100)); //100也可以隨便改
  6.         }
  7.        
  8.         public static double celsiusToFahrenheit(double celsius){
  9.                 return celsius * 9 / 5 + 32;
  10.         }
  11.        
  12.         public static double fahrenheitToCelsius(double fahrenheit){
  13.                 return (fahrenheit - 32) * 5 / 9;
  14.         }
  15. }
复制代码
你很多基本觀念都不瞭解吧 =.= 要加油哦
007pig
2013-12-3 09:18 PM
slay_alex92 发表于 2013-12-3 08:38 PM
說真的你寫的code真的很多錯誤,我直接幫妳寫了一個,可是我不知道哪裡需要用到for =.=
我剛剛compile ...

Celsius       Fahrenheit                         Fahrenheit         Celsius
40.0                104.0                              120.0                 48.89
39.0                102.2                              110.0                 43.33
…                       …                                   …                     …
32.0                 89.6                                40.0                   4.44
31.0                  87.8                               30.0                  -1.11
  

(Hint: Using for loops and methods to display the above output.)

其实他是要我print出这样的一列东西,我搞得很头大==
slay_alex92
2013-12-3 09:22 PM
007pig 发表于 2013-12-3 09:18 PM
Celsius       Fahrenheit                         Fahrenheit         Celsius
40.0                1 ...

OK 我懂了
稍等 馬上弄給你
slay_alex92
2013-12-3 09:28 PM
本帖最后由 slay_alex92 于 2013-12-3 09:29 PM 编辑
007pig 发表于 2013-12-3 09:18 PM
Celsius       Fahrenheit                         Fahrenheit         Celsius
40.0                1 ...
  1. public class q2
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 for(int i = 40; i > 30; i--)
  6.                 {
  7.                         String temp = i + "C is equals to " + celsiusToFahrenheit(i) + "F";
  8.                         System.out.println(temp);
  9.                 }
  10.                
  11.                 for(int i = 120; i > 29; i--)
  12.                 {
  13.                         String temp = i + "F is equals to " + fahrenheitToCelsius(i) + "C";
  14.                         System.out.println(temp);
  15.                 }
  16.         }
  17.        
  18.         public static double celsiusToFahrenheit(double celsius)
  19.         {
  20.                 return celsius * 9 / 5 + 32;
  21.         }
  22.        
  23.         public static double fahrenheitToCelsius(double fahrenheit)
  24.         {
  25.                 return (fahrenheit - 32) * 5 / 9;
  26.         }
  27. }
复制代码
格式不對的話System.out.println()裡面可以自己改,就可以print出你要的格式
007pig
2013-12-3 09:32 PM
slay_alex92 发表于 2013-12-3 09:22 PM
OK 我懂了
稍等 馬上弄給你

大大神人,大恩大德,没齿难忘 太谢谢谢谢谢谢谢谢了  T.T
slay_alex92
2013-12-3 09:34 PM
007pig 发表于 2013-12-3 09:32 PM
大大神人,大恩大德,没齿难忘 太谢谢谢谢谢谢谢谢了  T.T

不會啦哈哈
你跑跑看我寫的code,是你要的嗎?
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2026-1-1 03:33 AM , Processed in 0.108620 second(s), 21 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部