Java黑皮书课后题第3章:*3.20(科学:风寒温度)编写一个程序,提示用户输入一个温度值和一个风速值。如果输入值合法,那么显示风寒温度,否则显示温度或风速是不合法数据
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第3章:*3.20(科学:风寒温度)编写一个程序,提示用户输入一个温度值和一个风速值。如果输入值合法,那么显示风寒温度,否则显示温度或风速是不合法数据
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
*3.20(科學(xué):風(fēng)寒溫度)編寫一個程序,提示用戶輸入一個溫度值和一個風(fēng)速值。如果輸入值合法,那么顯示風(fēng)寒溫度,否則顯示溫度或風(fēng)速是不合法數(shù)據(jù)
- 題目
- 題目概述
- 破題
- 如何處理
- 代碼
題目
題目概述
*3.20(科學(xué):風(fēng)寒溫度)編寫一個程序,提示用戶輸入一個溫度值和一個風(fēng)速值。如果輸入值合法,那么顯示風(fēng)寒溫度,否則顯示溫度或風(fēng)速是不合法數(shù)據(jù)
編程練習(xí)題2.17給出計算風(fēng)寒溫度的公式,這個公式適用于溫度在華氏-58°到41°之間,并且風(fēng)速大于或等于2的情況
破題
我的2.17編程練習(xí)題blog的url(點擊跳轉(zhuǎn))或者復(fù)制以下url到瀏覽器:
https://blog.csdn.net/weixin_46356698/article/details/119790165
2.17代碼:
import java.util.Scanner;public class Test2_17 {public static void main(String[] args) {// 等式簡化為:t0 = 35.74 + 0.6125 * t1 - 35.75 * v + 0.4275 * t1 * v// 接收t1 vScanner input = new Scanner(System.in);System.out.println("Enter the temperature in Fahrenheit between -58°F and 41°F: ");double t1 = input.nextDouble();System.out.println("Enter the wind speed (>=2) in miles per hour: ");double v0 = input.nextDouble();double v = Math.pow(v0, 0.16);// 計算t0double t0 = 35.74 + 0.6125 * t1 - 35.75 * v + 0.4275 * t1 * v;// 輸出System.out.println("The wind chill index is "+t0);} }如何處理
2.17代碼在某種意義上其實已經(jīng)比較健壯了,但缺了3.20(本題)要求的輸入值合法性判斷的過程,只需要在接收后進行處理即可
代碼
import java.util.Scanner;public class Test3_20 {public static void main(String[] args) {// 等式簡化為:t0 = 35.74 + 0.6125 * t1 - 35.75 * v + 0.4275 * t1 * v// 接收t1 vScanner input = new Scanner(System.in);System.out.println("Enter the temperature in Fahrenheit between -58°F and 41°F: ");double t1 = input.nextDouble();System.out.println("Enter the wind speed (>=2) in miles per hour: ");double v0 = input.nextDouble();// 判斷是否合法if(t1 > 41 || t1 < -51 || v0 < 2){System.out.println("溫度或風(fēng)速是不合法數(shù)據(jù)");System.exit(1);}double v = Math.pow(v0, 0.16);// 計算t0double t0 = 35.74 + 0.6125 * t1 - 35.75 * v + 0.4275 * t1 * v;// 輸出System.out.println("The wind chill index is "+t0);} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第3章:*3.20(科学:风寒温度)编写一个程序,提示用户输入一个温度值和一个风速值。如果输入值合法,那么显示风寒温度,否则显示温度或风速是不合法数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第3章:**3.19
- 下一篇: Java黑皮书课后题第3章:**3.21