【问题标题】:Need help in logical for practical需要逻辑上的帮助以进行实际操作
【发布时间】:2020-07-05 08:55:08
【问题描述】:

我在做这部分时需要帮助

“确定有多少分数高于或等于平均值​​,有多少分数低于平均值。”

谁能提供一些想法如何根据需要获取输出

例如:输入要处理的测验分数数:5

分数 1:10

分数 2:8

分数 3:9

得分 4:2

分数 5:4

结果

=======

平均为 6.6

高于或等于平均分的数为3

低于平均分的分数为 2

public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);
  
  System.out.print("Enter number of quiz scores to process:");
  int totalScore=scanner.nextInt();

  int[]scoreNum= new int[totalScore];
  
  int i; 
  double total=0.0;
    
  for(i=0;i<totalScore;i++){
  System.out.printf("Score %d:", i+1 );
  scoreNum[i]=scanner.nextInt();
  total += scoreNum[i];
  }
  
  double average=total/totalScore;
  System.out.format("The Average is :%.1f\n",average); 
       

【问题讨论】:

  • 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 以了解如何提高问题的质量。然后查看help center,看看你可以问什么问题。请参阅:Why is “Can someone help me?” not an actual question?。您遇到的确切问题是什么?您可以使用简单的if()&lt;&gt; 运算符来查看一个值是更小还是更大的另一个值。
  • 看起来您只发布了一半的代码,或者是学校作业。无论如何,来到解决方案,我看到你已经有平均水平了。现在您已经有了平均值和输入的数字,只需遍历该数组 scoreNum 并将其与 average 进行比较。
  • 感谢您的建议,我认为我遇到的确切问题是让用户输入有点 if() 因为用户输入是循环的一部分,其中平均声明和计算发生在循环之后,所以我面临一个问题,我不能让用户输入成为 if() 语句的一部分。
  • 您的变量是在 for 循环之前声明的,因此它们在它之后也可用(在您当前打印出平均值的位置。您可能需要另一个循环来再次检查分数值在你计算出平均值之后。
  • @SooKinChun 看看我的回答

标签: java


【解决方案1】:

您的变量是在 for 循环之前声明的,因此它们也可以在它之后使用(在您当前打印出平均值的位置。您可能需要另一个循环在您计算后再次检查分数值平均值:cc @JuhaLahio

public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);
  
  System.out.print("Enter number of quiz scores to process:");
  int totalScore=scanner.nextInt();

  int[]scoreNum= new int[totalScore];
  
  int i; 
  double total=0.0;
    
  for(i=0;i<totalScore;i++){
  System.out.printf("Score %d:", i+1 );
  scoreNum[i]=scanner.nextInt();
  total += scoreNum[i];
  }
  
  double average=total/totalScore;
  System.out.format("The Average is :%.1f\n",average); 

  int countLesser = 0;
  int countGreater = 0;
  for (int i = 0; i < totalScore; i++) {
    if(scoreNum[i] > average) countGreater++;
    else if(scoreNum[i] < average) countLesser++;
  }
  System.out.println("Greater: " + countGreater + " Lesser: " + countGreater);

  }

【讨论】:

    【解决方案2】:

    试试这个代码:

    Scanner scanner = new Scanner(System.in);
    
        System.out.print("Enter number of quiz scores to process:");
        int totalScore = scanner.nextInt();
        float[] scores = new float[totalScore];
        float total = 0;
    
        for (int i = 0; i < totalScore; i++) {
            System.out.print("Score " + (i + 1) + ": \n");
            scores[i] = scanner.nextFloat();
            total += scores[i];
        }
    
        float average = total / totalScore;
        int scoresAbove = 0;
        int scoresBelow = 0;
    
        for (int a = 0; a < scores.length; a++) {
            if (scores[a] >= average) {
                scoresAbove++;
            } else {
                scoresBelow++;
            }
        }
    
        System.out.println("The average is: " + average + "\n Number of scores above or equal to the average is " + scoresAbove + "\n Number of scores below the average is " + scoresBelow);
    

    【讨论】:

    • 谢谢你,我拿了你的代码并尝试使用其他方法为我使用数组 scoreNum 并将其与 total/totalScore 进行比较。我得到这些错误
    • 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 5 在 Practical3Q1StackoverFlow.main(Practical3Q1StackoverFlow.java:25)
    • 也许你把分数写在分数的同一行,你必须把它写在分数 n 的下面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    相关资源
    最近更新 更多