Diese Präsentation wurde erfolgreich gemeldet.

# Write a function that uses a for loop to print five consecutive number (1).docx

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige  ×

1 von 2 Anzeige

# Write a function that uses a for loop to print five consecutive number (1).docx

Write a function that uses a for loop to print five consecutive numbers, and prints their sum. At the end. it would print the average. The output should have the following format:
Solution
ForTest.java
public class ForTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
displayValues();
}
public static void displayValues(){
int sum = 0;
System.out.println(\"Number\\tSum\");
for(int i=4; i<=8; i++){
sum = sum + i;
System.out.println(i+\"\\t\"+sum);
}
System.out.println(\"Average = \"+(sum/5));
}
}
Output:
NumberÂ Â  Sum
4Â Â  4
5Â Â  9
6Â Â  15
7Â Â  22
8Â Â  30
Average = 6
.

Write a function that uses a for loop to print five consecutive numbers, and prints their sum. At the end. it would print the average. The output should have the following format:
Solution
ForTest.java
public class ForTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
displayValues();
}
public static void displayValues(){
int sum = 0;
System.out.println(\"Number\\tSum\");
for(int i=4; i<=8; i++){
sum = sum + i;
System.out.println(i+\"\\t\"+sum);
}
System.out.println(\"Average = \"+(sum/5));
}
}
Output:
NumberÂ Â  Sum
4Â Â  4
5Â Â  9
6Â Â  15
7Â Â  22
8Â Â  30
Average = 6
.

Anzeige
Anzeige

### Write a function that uses a for loop to print five consecutive number (1).docx

1. 1. Write a function that uses a for loop to print five consecutive numbers, and prints their sum. At the end. it would print the average. The output should have the following format: Solution ForTest.java public class ForTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub displayValues(); } public static void displayValues(){ int sum = 0; System.out.println("NumbertSum"); for(int i=4; i<=8; i++){ sum = sum + i; System.out.println(i+"t"+sum); } System.out.println("Average = "+(sum/5)); } } Output: NumberÂ Â Sum 4Â Â 4 5Â Â 9 6Â Â 15 7Â Â 22
2. 2. 8Â Â 30 Average = 6