[英]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.