編寫一個程序,使用一種方法將千克轉換為磅。在循環中,調用循環體內的方法 kgTolb(kg)。方法頭定義如下:
方法頭
public static double kgToLb(double kg)
/* returns the converted pounds value where = 2.2 * kg */
創建第二個方法來打印細節行並在上面的算法中實現它。這是方法標題:
public static void print (kg, lb)
/* prints a formatted detail line*/
編寫一個程序,使用一種方法將千克轉換為磅。像我們在實驗室 9 中所做的那樣實施。在循環中,調用循環體內的方法 kgTolb(kg)。方法頭定義如下。
方法頭
public static double kgToLb(double kg)
returns the converted pounds value where lb = 2.2 * kg
示例執行
Enter the beginning value: 1
Conversion Table From KG(s) to LB(s)
Kilograms Pounds
---------------------
1 2.2
2 4.4
3 6.6
4 8.8
5 11.0
6 13.2
7 15.4
8 17.6
9 19.8
10 22.0
---------------------
End of Program
算法
Open keyboard for input
Prompt user for beginning value and assign to bVal
Print conversion table headings
eVal = bVal + 9
kg = bVal
while (kg <= eVal)
lb = kgToLb(kg)
print kg, lb to terminal //detail line
kg = kg + 1
Print conversion table footer
Close input
第 3 節:
創建第二個方法來打印細節行並在上面的算法中實現。這是方法標題:
public static void print(kg,lb)
prints a formatted detail line as in the sample output
算法V2
Open keyboard for input
Prompt user for beginning value and assign to bVal
Print conversion table headings
eVal = bVal + 9
kg = bVal
while (kg <= eVal)
lb = kgToLb(kg)
print(kg,lb) //detail line
kg = kg + 1
Print conversion table footer
Close input
第 4 節:
創建第三個方法,確保起始值大於零。這是方法標題:
public static boolean isValidBVal(bVal)
returns true if bVal is greater than 0, otherwise false
算法V3
Open keyboard for input
Prompt user for beginning value and assign to bVal
while (!isValidBVal(bVal)) //makes the method call
Print “invalid bVal, must be greater than 0.”
Prompt user for beginning value and assign to bVal
Print conversion table headings
eVal = bVal + 9
kg = bVal
while (kg <= eVal)
lb = kgToLb(kg)
print(kg,lb) //detail line
kg = kg + 1
Print conversion table footer
Close input
我的完整程序
*/import java.util.Scanner;
public class KgTolb {
public static void main(String[] args) {
//Open keyboard for input
Scanner input = new Scanner(System.in);
//Prompt user for beginning value and assign to bVal
System.out.println("Enter beginning value ===> ");
double bVal = input.nextDouble();
//Print conversion table headings
System.out.println("\n Kilograms\t\t Pounds");
double eVal = bVal + 9;
double kg = bVal;
System.out.println("----------------------");
}
public static double kgToLb(double kg) {
double lb = kgToLb(kg);
double kgToLbs = kg * 2.21;
for(double i = bVal; i <= bVal + 10; i++) {
System.out.printf("%d\t%.2f\n", i , (i * LB_PER_KG));
kg = kg + 1;
System.out.println("----------------------");
}
}
//Close input
您創建的方法中是否缺少任何內容?不,相反,裡面有太多。您被要求創建兩種方法,kgToLb()
即轉換 1 kg 值並print()
打印格式化輸出。相反,您的kgToLb
方法不僅轉換一個 kg 值,它還嘗試轉換 11 個連續的 kg 值,並且還進行格式化輸出,這是print()
.
在我看來,您對程序的總體目的和預期的示例執行非常忠誠。你缺少的是遵循你老師的設計。設計意味著什麼進入哪個方法,包括應該在main
方法(“程序”)中的內容。
從你的練習:
public static double kgToLb(double kg)
/* returns the converted pounds value where = 2.2 * kg */
所以該kgToLb
方法應該將公斤轉換為磅並返迴轉換後的值。沒有其他的。對於方法來說,這是一項非常簡單的任務。在我看來,對您來說困難的部分是編寫一個相應簡單且不包含任何其他內容的方法。它實際上是抽象和設計的培訓,而不是編程培訓。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句