当我将双打放在最上面时,该程序无法正常运行

时间:2019-02-14 00:50:28

标签: java variables

分配:变量     该程序提示用户输入-58°F至         41°F并且风速大于或等于2,然后显示         显示风冷温度。

// Imports util.Scanner
import java.util.Scanner; 

public class Windchill {

    public static void main(String[] args) { 
    Scanner input = new Scanner(System.in);

    // Tempurature
    double temperature = input.nextDouble();
    // Windspeed
    double speed = input.nextDouble();
    // Compute the wind chill tempurature
    double windChill = 35.74 + 0.6215 * temperature -         
                   35.75 * Math.pow(speed, 
                           0.16) + 0.4275 * temperature * 
                           Math.pow(speed, 0.16);

    // Prompt the user to enter a temperature between -58F and 41F.
    System.out.print("Enter the temperature in Fahrenheit " +
    "between -58\u00b0F and 41\u00b0F: ");

    // Prompt the user to enter the wind speed greter than or equal to 2.
    System.out.print("Enter the wind speed (>= 2) in miles per hour: ");

    // Display result
    System.out.println("The wind chill tempurature is " + windChill);

        }
}

3 个答案:

答案 0 :(得分:1)

这似乎是学校的作业。但是,看来您已经完成了大部分工作。恭喜你!现在,我觉得这里的问题可以通过解释为什么“双打放在首位”来使您的程序不起作用来解决。希望我的回答可以帮助您更好地理解java解释代码的方式!

事不宜迟,所有类型的编程语言都有变量。 Java也不例外。例如...

    double number = 0.0; // Java variable declaration
    number = 0.0 # Python variable declaration
    var number = 0.0 // JavaScript variable declaration

您的代码将自上而下执行。对此的说明如下所示。

int money = 0;
System.out.println(money);
money = 10;
System.out.println(money);
money = 9000;
System.out.println("I have over " + money);

这将输出

0
10
I have over 9000

但是,如果您像下面这样编写这段代码

System.out.println(money);
int money = 0;

您将得到一个错误!这是因为处决还没有看到钱甚至是东西!这就像没有牙刷刷牙。不能,因为你没有刷子。

因此,这同样适用于您的程序。

public static void main(String[] args) { 
    double temperature = input.nextDouble();
    Scanner input = new Scanner(System.in);

            // Prompt the user to enter a temperature between -58F and 41F.
    System.out.print("Enter the temperature in Fahrenheit " +
    "between -58\u00b0F and 41\u00b0F: ");
    // Tempurature

        // Prompt the user to enter the wind speed greter than or equal to 2.
    System.out.print("Enter the wind speed (>= 2) in miles per hour: ");
    // Windspeed
    double speed = input.nextDouble();
    // Compute the wind chill tempurature
    double windChill = 35.74 + 0.6215 * temperature -         
                   35.75 * Math.pow(speed, 
                           0.16) + 0.4275 * temperature * 
                           Math.pow(speed, 0.16);

        // Display result
    System.out.println("The wind chill tempurature is " + windChill);

}

注意扫描仪线上方的温度。输入是您创建的对象,用于读取该双精度字。如果在创建输入对象之前尝试使用此功能,则程序将不知道该输入对象是什么!

答案 1 :(得分:0)

只需重新排列下面的代码

// Imports util.Scanner
import java.util.Scanner; 

public class Windchill {

    public static void main(String[] args) { 
    Scanner input = new Scanner(System.in);

            // Prompt the user to enter a temperature between -58F and 41F.
    System.out.print("Enter the temperature in Fahrenheit " +
    "between -58\u00b0F and 41\u00b0F: ");
    // Tempurature
    double temperature = input.nextDouble();
        // Prompt the user to enter the wind speed greter than or equal to 2.
    System.out.print("Enter the wind speed (>= 2) in miles per hour: ");
    // Windspeed
    double speed = input.nextDouble();
    // Compute the wind chill tempurature
    double windChill = 35.74 + 0.6215 * temperature -         
                   35.75 * Math.pow(speed, 
                           0.16) + 0.4275 * temperature * 
                           Math.pow(speed, 0.16);

    // Display result
    System.out.println("The wind chill tempurature is " + windChill);

        }
}

但没有与double相关的问题:)

答案 2 :(得分:0)

谢谢大家。我知道了。

/ *分配:变量     该程序提示用户输入-58°F至41°F之间的温度     然后风速大于或等于2,然后显示,然后显示     风寒温度。 * /

//导入util.Scanner 导入java.util.Scanner;

公共类Windchill {

public static void main(String[] args) {
    // Declare variables
    double temperature;
    double windspeed;
    double wind_chill;

    // Create a Scanner object to read input
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter a temperature between -58F and 41F.
    System.out.print("Enter the temperature in Fahrenheit " +
                     "between -58\u00b0F and 41\u00b0F: ");
    temperature = input.nextDouble();       

    // Prompt the user to enter the wind speed greter than or equal to 2.
    System.out.print("Enter the wind speed (>= 2) in miles per hour: ");
    windspeed = input.nextDouble();

    // Display result
    wind_chill = 35.74 + 0.6215 * temperature -
               35.75 * Math.pow(windspeed, 0.16) +
               0.4275 * temperature * Math.pow(windspeed, 0.16);

    System.out.println("The wind chill temprature is " + wind_chill);

}

}