Using Java, how can I use a loop that runs 7 times (using the final variable) to
ID: 3787011 • Letter: U
Question
Using Java, how can I use a loop that runs 7 times (using the final variable) to ask and get how many steps the user walks each day of the seven days in a week (Monday, Tuesday, etc until Sunday). Calculate the daily average distance walked by the user. (one mile = 5280 steps)? I know how to ask and get users name, college, age and declare the constant assign value 7 to it. But, I can't come up with a good loop without it seeming repetitive. I think that switch and/or while loop would do the trick but idk. Ex "How many steps did you walk Monday?" (asking for user input) "How many steps did you walk Tuesday?" (asking for user input) etc..
Explanation / Answer
DistanceAverageTest.java
import java.util.Scanner;
public class DistanceAverageTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int TIMES = 7;
String days[] = {"Monday", "Tuessday", "Wednesday", "Thrusday", "Friday", "Saturday", "Sunday"};
int total = 0;
for(int i=0; i<TIMES; i++){
System.out.print("How many steps did you walk "+days[i]+"? ");
int miles = scan.nextInt();
total = total + miles;
}
double dialyAverage = total/(double)TIMES;
System.out.println("The daily average distance walked by the user in steps: "+dialyAverage);
double miles = dialyAverage/5280 ;
System.out.println("The daily average distance walked by the user in miles: "+miles);
}
}
Output:
How many steps did you walk Monday? 10000
How many steps did you walk Tuessday? 20000
How many steps did you walk Wednesday? 30000
How many steps did you walk Thrusday? 40000
How many steps did you walk Friday? 50000
How many steps did you walk Saturday? 60000
How many steps did you walk Sunday? 70000
The daily average distance walked by the user in steps: 40000.0
The daily average distance walked by the user in miles: 7.575757575757576
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.