我正在为学校作业创建一个小程序,人们可以在其中输入自己的跑步历史(跑步里程,经过的时间),并查看他们潜在的10K速度和马拉松速度。显示速度后,将显示不同速度的表。
我有一个全局变量(pace),使用它运行的大多数计算都需要在for循环内更改,但不会为计算更新。
edit:我知道该程序的编写不佳,并且有很多内容可以压缩。但是,它可以工作并且将满足分配参数。在for循环中,循环的每次迭代都需要将变量“ pace”增加30。
public class MarathonTime {
public static void main(String[] args) {
String name;
double distance;
int hours, minutes, seconds;
double pace;
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.print("What is your first name? ");
name = scanner.next();
System.out.print("How many miles did you run today? ");
distance = scanner.nextDouble();
System.out.print("How long did it take? Hours: ");
hours = scanner.nextInt();
System.out.print("Minutes: ");
minutes = scanner.nextInt();
System.out.print("Seconds: ");
seconds = scanner.nextInt();
pace = ((hours * 3600) + (minutes * 60) + seconds) / distance;
double marathonPace = pace * 26.2;
double tenKPace = pace * 6.2;
int paceMinutes, paceSeconds;
paceMinutes = (int) pace / 60;
paceSeconds = (int) pace % 60;
int marathonHours, marathonMinutes, marathonSeconds;
marathonHours = (int) marathonPace / 3600;
marathonMinutes = (int) (marathonPace % 3600) / 60;
marathonSeconds = (int) marathonPace % 60;
int tenKMinutes, tenKSeconds;
tenKMinutes = (int) tenKPace / 60;
tenKSeconds = (int) tenKPace % 60;
System.out.println("Hello " + name);
System.out.print("Your pace is "); timeFormatPace(paceMinutes, paceSeconds);
System.out.println();
System.out.print("At this rate your marathon time would be "); timeFormatMarathon(marathonHours, marathonMinutes, marathonSeconds);
System.out.println();
System.out.print("and your 10K time would be "); timeFormat10K(tenKMinutes, tenKSeconds);
System.out.println();
System.out.println("Good luck with your training!");
System.out.println();
for(int i = 1; i <= 10; i++){
if(i == 1) {
System.out.println("Pace \t\t 10K Time \t\t Marathon Time");
i++;
}
if(i == 2) {
System.out.println("------------------------------------------------------");
i++;
}
// input the tablePaces into the calcs and print them as the format
pace = (281 + (i*30));
if (i >= 3 && i <= 10) {
timeFormatPace(paceMinutes, paceSeconds); System.out.print("\t\t "); timeFormat10K(tenKMinutes, tenKSeconds); System.out.print("\t\t\t ");
timeFormatMarathon(marathonHours, marathonMinutes, marathonSeconds);
System.out.println();
}
}
}
答案 0 :(得分:0)
我认为这是您想要做的(非常简洁):
public static void main(String[] args) {
int pace = 100; // or initialise to whatever
for(int i = 0; i < 10; i++) {
// do something
pace = pace + 30; // or equivalently, pace += 30
}
}
将来,请尝试将问题减少到minimal, complete and verifiable example。这样做的过程通常可以帮助您自己解决问题,但至少可以使其他人更容易解决问题。
此外,如果您知道程序编写不正确,请修复它!写得好,简洁的模块化程序更容易推论。凌乱,复杂的代码很难。您将以更少的调试时间来感谢自己,并且也会获得更好的成绩。