JBTALKS.CC

标题: java 入门功课求救!! [打印本页]

作者: 007pig    时间: 2013-12-3 06:54 PM
标题: java 入门功课求救!!
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,是你要的嗎?
作者: 007pig    时间: 2013-12-3 09:39 PM
slay_alex92 发表于 2013-12-3 09:34 PM
不會啦哈哈
你跑跑看我寫的code,是你要的嗎?

改改下就差不多了  哈哈

话说,大大  我还能打扰你一下子帮我解释这两题吗。。array 还有那个 method 我看真不懂呢 /.\

1)Write a java program to prompt a user to input five integer numbers into array.
      Display the array elements in the reverse order of input. Use loops for both the input
      and printing.

2)Write a java program that takes 10 integers as input. The program places the even
integers into an array called evenList, the odd integers into an array called oddList,
and the negative integers into an array called negativeList. The program displays the
contents of the three arrays after all the integers have been entered.
作者: slay_alex92    时间: 2013-12-3 09:54 PM
007pig 发表于 2013-12-3 09:39 PM
改改下就差不多了  哈哈  

话说,大大  我还能打扰你一下子帮我解释这两题吗。。array 还有那 ...

第一題
  1. import java.io.*;

  2. public class test1
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 int[] input = new int[5];
  7.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.                
  9.                 System.out.println("Please enter 5 integers.");
  10.                
  11.                 for(int i = 4; i >= 0; i--)
  12.                         input[i] = Integer.valueOf(br.readLine());
  13.                
  14.                 for(int i = 0; i < 5; i++)
  15.                         System.out.print(input[i] + " ");
  16.         }
  17. }
复制代码

作者: slay_alex92    时间: 2013-12-3 10:07 PM
本帖最后由 slay_alex92 于 2013-12-3 10:10 PM 编辑
007pig 发表于 2013-12-3 09:39 PM
改改下就差不多了  哈哈  

话说,大大  我还能打扰你一下子帮我解释这两题吗。。array 还有那 ...


第二題
  1. import java.io.*;

  2. public class test2
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 int[] evenList = new int[10];
  7.                 int[] oddList = new int[10];
  8.                 int[] negativeList = new int[10];
  9.                
  10.                 int evenIndex = 0, oddIndex = 0, negativeIndex = 0;
  11.                
  12.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  13.                 System.out.println("Please enter 10 integers.");
  14.                
  15.                 for(int i = 0; i < 10; i++){
  16.                         int j = Integer.valueOf(br.readLine());
  17.                         if(j < 0){
  18.                                 negativeList[negativeIndex] = j;
  19.                                 negativeIndex++;
  20.                         } else if(j % 2 == 0){
  21.                                 evenList[evenIndex] = j;
  22.                                 evenIndex++;
  23.                         } else{
  24.                                 oddList[oddIndex] = j;
  25.                                 oddIndex++;
  26.                         }
  27.                 }
  28.                
  29.                 System.out.print("evenList: ");
  30.                 for(int i = 0; i < evenIndex; i++)
  31.                         System.out.print(evenList[i] + " ");
  32.                
  33.                 System.out.print("\noddList: ");
  34.                 for(int i = 0; i < oddIndex; i++)
  35.                         System.out.print(oddList[i] + " ");
  36.                
  37.                 System.out.print("\nnegativeList: ");
  38.                 for(int i = 0; i < negativeIndex; i++)
  39.                         System.out.print(negativeList[i] + " ");
  40.         }
  41. }
复制代码

作者: slay_alex92    时间: 2013-12-3 10:11 PM
007pig 发表于 2013-12-3 09:39 PM
改改下就差不多了  哈哈  

话说,大大  我还能打扰你一下子帮我解释这两题吗。。array 还有那 ...

這是學校的assignment嗎??
作者: 007pig    时间: 2013-12-3 10:15 PM
slay_alex92 发表于 2013-12-3 10:11 PM
這是學校的assignment嗎??

是啊==我学的很差
作者: slay_alex92    时间: 2013-12-3 10:19 PM
本帖最后由 slay_alex92 于 2013-12-3 10:25 PM 编辑
007pig 发表于 2013-12-3 10:15 PM
是啊==我学的很差


你看看我寫的對嗎

你是讀甚麼的阿 有JAVA的課好爽喔
作者: 神幻    时间: 2013-12-3 11:32 PM
各位大大其实java什么啊?好奇
作者: Kikokent    时间: 2013-12-4 12:07 AM
slay_alex92 发表于 2013-12-3 09:28 PM
格式不對的話System.out.println()裡面可以自己改,就可以print出你要的格式

哇,你的答案也给到太完整了吧?
谢啦。。。
作者: XiaoNaruto    时间: 2013-12-4 02:59 AM
我觉得
Write a java program to prompt a user to input five integer numbers into array.
Display the array elements in the reverse order of input. Use loops for both the input
and printing.

在display loop那里你不应该用
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

初学者的话应该是
用类似这样的,我不是很专业也是入门罢了,由于现在学了很多语言还不是很精通,以下如果我是初学者我会这样做。
for(i = 0; i<5 ; i++)
{
     value[i] = keyboard.nextInt();
}

for(i = 5; i>-1 ; i--)
{
      System.out.print(value[i] + " ");
}





作者: slay_alex92    时间: 2013-12-4 11:10 AM
XiaoNaruto 发表于 2013-12-4 02:59 AM
我觉得
Write a java program to prompt a user to input five integer numbers into array.
Display th ...

哦哦~ 其實我也是剛學不久而已~
而且我是自己買書來學的,所以沒有學得很完整 T.T

我現在才知道有keyboard.nextInt()這個用法~
謝謝你^^
作者: 007pig    时间: 2013-12-4 04:56 PM
XiaoNaruto 发表于 2013-12-4 02:59 AM
我觉得
Write a java program to prompt a user to input five integer numbers into array.
Display th ...

请问是怎么加进去,怎么我尝试了几次都不行  一直一大堆cannnot find the symbol
作者: 007pig    时间: 2013-12-4 04:58 PM
神幻 发表于 2013-12-3 11:32 PM
各位大大其实java什么啊?好奇

一种电脑语言
作者: 007pig    时间: 2013-12-4 04:59 PM
slay_alex92 发表于 2013-12-4 11:10 AM
哦哦~ 其實我也是剛學不久而已~
而且我是自己買書來學的,所以沒有學得很完整 T.T

好强大哦  自己也能学的那么好,我有老师教  都学得半死
作者: slay_alex92    时间: 2013-12-4 05:23 PM
007pig 发表于 2013-12-4 04:59 PM
好强大哦  自己也能学的那么好,我有老师教  都学得半死

算是有興趣吧哈哈
感覺有興趣的東西會學得比較順~
作者: yirhpnyfc    时间: 2013-12-25 11:35 AM
提示: 作者被禁止或删除 内容自动屏蔽




欢迎光临 JBTALKS.CC (https://www.jbtalks.cc/) Powered by Discuz! X2.5