[英]Calling other methods to main in java
我在主方法中格式化返回的方法时遇到了一些问题。 我已经创建了方法并完成了计算,但是我的问题是是否要正确地将其他两个方法调用为主方法。 我在格式化各列中的方法时也遇到了问题。 我是否需要使用受人尊敬的方法制作列? 还是我需要在main方法中创建它们?
编写一个程序,分析掉落10秒钟的物体。 它应包含主要方法和两个附加方法。 其他方法之一应返回当前对象作为参数传递时对象落入的距离(以米为单位)。 请参阅下面的公式。 第三种方法应将米转换为英尺。 您可以在线查找所需的转换系数。 main方法应使用一个循环调用其他方法并生成一个表,如下所示。 该表应显示在带小数点的格式化列中,如图所示。 我相信我在
SEC METERS FEET
1 4.9 16.1
2 19.6 64.3
3 44.1 144.7
4 78.4 257.2
5 122.5 401.9
6 176.4 578.7
7 240.1 787.7
8 313.6 1028.9
9 396.9 1302.2
10 490.0 1607.6
我的密码
package week4.yedkois;
public class project3 {
public static void main(String[] args) {
System.out.printf("SEC" + "\n");
meters();
feet();
for (int time = 1; time <= 10; time++) {
System.out.println(time);
}
}
public static void meters() {
double Meters;
double G = 9.8; // meters = .5(9.8)(seconds) ^2
for (int time = 1; time <= 10; time++) {
Meters = (.5 * 9.8 * Math.pow(time, 2));
System.out.printf("%.1f\n", Meters);
}
return;
}
public static void feet() {
double Feet;
double G = 9.8; // meters = .5(9.8)(seconds) ^2
for (int time = 1; time <= 10; time++) {
Feet = (.5 * 9.8 * Math.pow(time, 2) * 3.28084);
System.out.printf("%.1f\n", Feet);
}
return;
}
}
这是我的解决方案。 我使用制表符(“ \\ t”)在不同值之间实现相同的空间。 然后我不得不重新设计您的代码。 我仅直接在主方法中使用一个if循环,并将当前时间值作为参数传递给meter()和foot()这两种方法。 这样一来,就可以更轻松地获得一轮的所有值。
以下是一些其他说明:
希望这对您有所帮助。
public class Project3 {
public static void main(String[] args){
System.out.printf("%3s\t%6s\t%6s\n", "SEC", "METERS", "FEET");
for(int time = 1; time <= 10; time++)
{
System.out.print(time + "\t");
meters(time);
feet(time);
System.out.println();
}
}
public static void meters(int time){
double meters;
double g = 9.8; // meters = .5(9.8)(seconds) ^2
meters = (.5 * 9.8 * Math.pow(time, 2));
// the longer the expected maximum length of a result gets
// the higher your reserved number of digits has
// to be, to gain the wanted right bound effect!
System.out.printf("%6.1f\t", meters);
}
public static void feet(int time){
double feet;
double g = 9.8; // meters = .5(9.8)(seconds) ^2
feet = (.5 * 9.8 * Math.pow(time, 2) * 3.28084);
// the longer the expected maximum length of a result gets
// the higher your reserved number of digits has
// to be, to gain the wanted right bound effect!
System.out.printf("%6.1f", feet);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.