我创建的方法中是否缺少任何内容?我对方法创建感到困惑
Is there anything I'm missing in the method I created? I'm confused with method creations
编写一个程序,使用一种方法将千克转换为磅。在循环中,在循环body内调用方法kgTolb(kg)。方法 header 定义如下:
方法headers
public static double kgToLb(double kg)
/* returns the converted pounds value where = 2.2 * kg */
创建第二个方法来打印明细行并在上面的算法中实现它。这是方法 header:
public static void print (kg, lb)
/* prints a formatted detail line*/
编写一个程序,使用一种方法将千克转换为磅。像我们在 inn lab9 中那样实现。在循环中,在循环body内调用方法kgTolb(kg)。方法header定义如下。
方法headers
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 部分:
创建第二个方法来打印明细行并在上面的算法中实现。这是方法 header:
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 节:
创建第三种方法,确保起始值大于零。这是方法 header:
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()
转换一公斤值,print()
打印格式化输出。相反,您 kgToLb
方法不仅转换一个千克值,它还尝试转换 11 个连续的千克值并且还进行格式化输出,这是 print()
.
的工作
在我看来,您非常忠于程序的总体目的和预期的样本执行。您缺少的是遵循老师的设计。设计意味着什么进入哪个方法,包括什么应该在 main
方法(“程序”)中。
来自你的练习:
public static double kgToLb(double kg)
/* returns the converted pounds value where = 2.2 * kg */
因此 kgToLb
方法应将公斤转换为磅,并 return 转换后的值。没有其他的。对于方法来说,这是一项非常简单的任务。在我看来,对您来说最困难的部分是编写一个相对简单且不包含任何其他内容的方法。与其说是编程训练,不如说是训练抽象和设计。
编写一个程序,使用一种方法将千克转换为磅。在循环中,在循环body内调用方法kgTolb(kg)。方法 header 定义如下:
方法headers
public static double kgToLb(double kg)
/* returns the converted pounds value where = 2.2 * kg */
创建第二个方法来打印明细行并在上面的算法中实现它。这是方法 header:
public static void print (kg, lb)
/* prints a formatted detail line*/
编写一个程序,使用一种方法将千克转换为磅。像我们在 inn lab9 中那样实现。在循环中,在循环body内调用方法kgTolb(kg)。方法header定义如下。
方法headers
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 部分:
创建第二个方法来打印明细行并在上面的算法中实现。这是方法 header:
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 节:
创建第三种方法,确保起始值大于零。这是方法 header:
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()
转换一公斤值,print()
打印格式化输出。相反,您 kgToLb
方法不仅转换一个千克值,它还尝试转换 11 个连续的千克值并且还进行格式化输出,这是 print()
.
在我看来,您非常忠于程序的总体目的和预期的样本执行。您缺少的是遵循老师的设计。设计意味着什么进入哪个方法,包括什么应该在 main
方法(“程序”)中。
来自你的练习:
public static double kgToLb(double kg)
/* returns the converted pounds value where = 2.2 * kg */
因此 kgToLb
方法应将公斤转换为磅,并 return 转换后的值。没有其他的。对于方法来说,这是一项非常简单的任务。在我看来,对您来说最困难的部分是编写一个相对简单且不包含任何其他内容的方法。与其说是编程训练,不如说是训练抽象和设计。