简体   繁体   中英

Need Help in Java (Average Program)

I am just a beginner programmer and I am stuck on a part of my code; Is there a way to tell the compiler that if the user types in 7, it will go through 1 2 3 4 5 6 and 7 instead of just using 7?

import java.util.Scanner;
public class Average {
    int tests;
    int grade1, grade2, grade3, grade4, grade5, grade6, grade7, grade8, grade9, grade10;
    int total;
    double average;
    public void Average1() {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter how many tests you will use. (Minimum 3, Maximum 10.) ");
    tests =  input.nextInt();
    System.out.println("Enter the grade of your first test. (Do not include %)");
    grade1 = input.nextInt();
    System.out.println("Enter the grade of your second test. (Do not include %)");
    grade2 = input.nextInt();
    System.out.println("Enter the grade of your third test. (Do not include %)");
    grade3 = input.nextInt();
    switch (tests) {

    case 4 : System.out.println("Enter the grade of your fourth test. (Do not include %)");
    grade4 = input.nextInt();
    break;

    case 5 : System.out.println("Enter the grade of your fifth test. (Do not include %)");
    grade5 = input.nextInt();
    break;

    case 6 : System.out.println("Enter the grade of your sixth test. (Do not include %)");
    grade6 = input.nextInt();
    break;

    case 7 : System.out.println("Enter the grade of your seventh test. (Do not include %)");
    grade7 = input.nextInt();
    break;

    case 8 : System.out.println("Enter the grade of your eighth test. (Do not include %)");
    grade8 = input.nextInt();
    break;

    case 9 :System.out.println("Enter the grade of your ninth test. (Do not include %)");
    grade9 = input.nextInt();
    break;

    case 10 : System.out.println("Enter the grade of your tenth test. (Do not include %)");
    grade10 = input.nextInt();
    break;
    }
    total = grade1 + grade2 + grade3;
    average = total/tests; 
}

}

In this example, I am trying to make a basic average calculator. (This is only one class, other code i in separate classes.) I need to ask the user how many tests he will enter. (It will automatically go through 1 - 3) So if the user types in 6 tests, how do I tell the compiler not just to go directly to 6. I want it to go through 1-6. People say I need to use a for loop, but where would I insert the loop?

(Please keep in mind im just a beginner.)

Create a simple loop based on the user input, like:

if the user input was 7 , then:

for(int i = 1; i <= userInput; i++) {
    // then pass the i value to your method, it will go through >> 1 2 3 4 5 6 7.
}

Use for Loop

for(int i = 0; i <=7 i++){
   //Code

}

在此输入图像描述

You only need to use a loop to keep a running total and use a loop to collect the number of entries that they specify for the first question. You'll likely want to add some error checking to the following to handle bad inputs (say entering zero for the number of tests).

import java.util.Scanner;
public class Average {
    int tests;
    int total = 0;
    double average;
    public void Average1() {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter how many tests you will use. (Minimum 3, Maximum 10.) ");
        tests =  input.nextInt();
        for (int i = 1; i <= tests; ++i) {
            System.out.println("Enter the grade of for test " + i + ". (Do not include %)");
            total += input.nextInt();
        }

        average = total/tests; 
    }
}

Here is how I would approach the problem,

// Note the method naming convention. Also, a return type.
// takes an input Scanner and the number of tests.
public static double average1(Scanner input,
    int tests) {
  // An array to store the grades.
  int[] testGrades = new int[tests];
  long total = 0;
  for (int i = 1; i <= tests; ++i) {
    System.out.printf(
        "Enter the grade for test %d.\n", i);
    System.out.flush();
    if (input.hasNextInt()) {
      testGrades[i - 1] = input.nextInt();
    }
  }
  System.out.println("The array contains: "
      + java.util.Arrays.toString(testGrades));
  System.out.flush();
  // Add up the grades for the total. This addition could occur at input.
  for (int i : testGrades) {
    total += i;
  }
  return total / tests;
}

public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("Please enter the # of tests to average: ");
  System.out.flush();
  int testCount = input.nextInt();
  System.out.println("The average is: "
      + average1(input, testCount));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM