需要Java帮助(平均程序)

时间:2014-02-23 16:02:03

标签: java average

我只是一名初学程序员,而且我的代码部分被困在一边;有没有办法告诉编译器,如果用户键入7,​​它将通过1 2 3 4 5 6和7而不是仅使用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; 
}

}

在这个例子中,我试图制作一个基本的平均计算器。 (这只是一个类,其他代码我在不同的类中。)我需要询问用户他将输入多少个测试。 (它将自动通过1 - 3)因此,如果用户键入6个测试,我如何告诉编译器不要直接转到6.我希望它通过1-6。人们说我需要使用for循环,但是我会在哪里插入循环?

(请记住我只是一个初学者。)

4 个答案:

答案 0 :(得分:1)

根据用户输入创建一个简单的循环,例如:

如果用户输入为7,则为:

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.
}

答案 1 :(得分:1)

使用for循环

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

}

enter image description here

答案 2 :(得分:1)

您只需要使用循环来保持运行总计并使用循环来收集他们为第一个问题指定的条目数。您可能希望向以下内容添加一些错误检查以处理错误的输入(例如,为测试次数输入零)。

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; 
    }
}

答案 3 :(得分:0)

以下是我将如何解决问题,

// 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));
}