我正在研究我的Intro to Java类中的一个项目,我需要创建一个程序来询问用户两个数字,计算风寒并将温度转换为摄氏温度。
结果看起来应该是这样的:
Ima Java Student
Project 2 – Wind-Chill Calculator
Enter the outside temperature (Fahrenheit) [-58 to 41]: 32
Enter the wind speed [2 to 50]: 10
Outside Temp (F) Wind Speed Wind-Chill (F) Wind-Chill (C)
---------------- ---------- -------------- --------------
32 10 23.727 -4.596
我的输出如下:
Ima Java Student
Project 2 – Wind-Chill Calculator
这是我的完整代码。我在这里想念的是什么?!
/**
* Author: Dillon Carter
* Course: COP2551
* Project#: 2
* Title: Wind Chill Calculator
* Due Date: 7/9/2017
*
* Calculates the Wind Chill based on the outside Temperature and Wind Speed given by the user.
* Then, converts the outcome from Fahrenheit to Celsius for the user.
*/
import java.util.Scanner;
public class Calculator {
// Variable Declaration
int temperature = 0;
int speed = 0;
int windChill = 0;
int celsius = 0;
double windChill_C = 0;
double windChill_F = 0;
public static void main(String[] args) {
System.out.println("Ima Java Student");
System.out.println("Project 2 - Wind-Chill Calculator");
}
// Gets input from user and saves as variables
private boolean getInput() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter the outside temperature (Fahrenheit) [-58 to 41]:");
int temperature = reader.nextInt();
boolean isGoodTemp = (temperature >= -58 && temperature <= 41);
System.out.println("Enter the wind speed [2 to 50]:");
int windSpeed = reader.nextInt();
boolean isGoodSpeed = (windSpeed >= 2 && windSpeed <= 50);
return true;
}
// No clue what this is for....
private void performCalculations(int temperature, int windSpeed) {
}
// Calculates the Wind Chill based on the temperature and wind speed given by the user
private double calculateWindChill(int temperature, int windSpeed) {
windChill_F = 35.74 + 0.6215 *temperature - 35.75 * Math.pow(windSpeed, 0.16) + 0.4275*temperature * Math.pow(windSpeed, 0.16);
return windChill_F;
}
// Converts temperature from Fharenheight to Celsius
private double convertTemperature(double windChill_F) {
windChill_C = Math.round((temperature - 32) * 5 / 9);
return windChill_C;
}
// Displays the results to the user
private void displayResults(int temperature, int windSpeed, double windChill_F, double windChill_C) {
System.out.println( "Outside Temp (F) " + "Wind Speed " + "Wind-Chill (F) "+ "Wind-Chill (C) ");
System.out.println( "--------------- " + "--------------- " + "--------------- " + "--------------- ");
System.out.println( temperature + " " + windSpeed + " " + windChill_F + " " + windChill_C);
}
}
重要的是要注意我不允许创建更多方法,但必须使用我当前代码中包含的方法。我很接近这项工作,我能感觉到它大声笑。
答案 0 :(得分:0)
我做了这个程序。用户程序输入温度和速度后计算windChill并将其转换为celcium。最后显示结果。
import java.util.Scanner;
public class Calculator {
// Variable Declaration
int temperature = 0;
int speed = 0;
int windChill = 0;
int celsius = 0;
double windChill_C = 0;
double windChill_F = 0;
public static void main(String[] args) {
System.out.println("Ima Java Student");
System.out.println("Project 2 - Wind-Chill Calculator");
System.out.println();
Calculator calc = new Calculator();
while (!calc.getInput()) {
System.out.println("Temperature or wind speed is wrong. Try again");
}
calc.displayResults(calc.temperature, calc.speed, calc.windChill_F, calc.windChill_C);
}
// Gets input from user and saves as variables
private boolean getInput() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter the outside temperature (Fahrenheit) [-58 to 41]:");
temperature = reader.nextInt();
boolean isGoodTemp = (temperature >= -58 && temperature <= 41);
System.out.println("Enter the wind speed [2 to 50]:");
speed = reader.nextInt();
boolean isGoodSpeed = (speed >= 2 && speed <= 50);
performCalculations(temperature, speed);
return isGoodTemp && isGoodSpeed;
}
// No clue what this is for....
private void performCalculations(int temperature, int windSpeed) {
calculateWindChill(temperature, windSpeed);
convertTemperature(windChill_F);
}
// Calculates the Wind Chill based on the temperature and wind speed given
// by the user
private double calculateWindChill(int temperature, int windSpeed) {
windChill_F = 35.74 + 0.6215 * temperature - 35.75 * Math.pow(windSpeed, 0.16)
+ 0.4275 * temperature * Math.pow(windSpeed, 0.16);
return windChill_F;
}
// Converts temperature from Fharenheight to Celsius
private double convertTemperature(double windChill_F) {
windChill_C = Math.round((temperature - 32) * 5 / 9);
return windChill_C;
}
// Displays the results to the user
private void displayResults(int temperature, int windSpeed, double windChill_F, double windChill_C) {
System.out.println("Outside Temp (F) " + "Wind Speed " + "Wind-Chill (F) " + "Wind-Chill (C) ");
System.out.println("--------------- " + "--------------- " + "--------------- " + "--------------- ");
System.out.println(temperature + " " + windSpeed + " " + windChill_F + " " + windChill_C);
}
}
打印:
Ima Java Student
Project 2 - Wind-Chill Calculator
Enter the outside temperature (Fahrenheit) [-58 to 41]:
40
Enter the wind speed [2 to 50]:
24
Outside Temp (F) Wind Speed Wind-Chill (F) Wind-Chill (C)
--------------- --------------- --------------- ---------------
40 24 29.58922393920365 4.0