小弟现在刚刚开始学习java,老师留了两道题,第一道题是C=5/9*(F–32)WhereFistheFahrenheittemperatureandCistheCelsiustemperature.WriteamethodnamedCelsiusthatacceptsasingleargu...
小弟现在刚刚开始学习java,老师留了两道题,
第一道题是
C= 5/9 * (F – 32)
WhereF is the Fahrenheit temperature and C is the Celsius temperature. Write a methodnamed Celsius that accepts a single argument, the temperature in Fahrenheit.The method should return the temperature converted to Celsius. Demonstrate themethod by calling it in a loop that displays a table of Fahrenheit temperatures0 through 20 and their Celsius equivalents.
现在我已经把这个转换写完了,但是不知道怎么可以加上这个table,然后这个method也不确定是否正确....
public static void main(String[] args) {
// TODO Auto-generated method stub
showTemperatures();
}
public static void showTemperatures()
{
int [] F = new int[21];
for(int i=0;i<21;i++)
{
F[i]=i;
double C = 5d/9d*(F[i]-32);
System.out.println("Fahrenheit Temperatire is:"+ " " +(i)+" " +"Celsius Temperatire is: "+ " " +C);
}
第二道题是
Convert program 3 in assignment 11 suchthat the average of the numbers in the array is computed in a method. Your main program should call a method calledaverage, passing the entire array. Themethod should compute the average of all numbers in the array and return theaverage to the caller. This methodshould be able to deal with any size array. Once main receives the average backfrom method average it should call another method to display the average.
之前的程序已经写好了,应该没有问题,但是这次的感觉有些晕,不知道能否帮助一二?
import java.util.Scanner;
import javax.swing.*;
public class LinhanYangProgram2
{
public static void main(String[] args) {
int b;
double sum=0;
System.out.print("How many numbers do you want to use to get a average:");
Scanner Keyboard = new Scanner(System.in);
b=Keyboard.nextInt();
if(b<=0)
{
JOptionPane.showMessageDialog(null,"The number you entered cannot be negative or 0");
}
if(b>0)
{
int[] a = new int[b];
{for (int i=0;i
{ if(b>0)
System.out.print("Please enter number"+(i+1)+" = ");
a [i]=Keyboard.nextInt();
sum=sum+a[i]; }
JOptionPane.showMessageDialog(null,"Average is" + sum/b, "result",1);}}}}
还需要继续完善程序,第一个题,是需要通过表格来呈现这些数据,但是我不知道该怎么把表格的程序加进去
第二个题是说在原有的程序上能够增加调用,就是在显示完一次平均数后,能够回到开始,准备显示下一次的平均数
展开