如何解决我的计数器和平均计算器 [英] How to fix my counter and average calculator

查看:168
本文介绍了如何解决我的计数器和平均计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个代码,根据用户输入的学生姓名输入成绩。



输入值应为学生姓名,所有分数的总和和平均分数。



由于某种原因,我无法获得平均值或总计打印,我的学生类中的计数器显示一个错误删除这个标记++



这是我的主类和我的学生类:

  / ** 
* COSC 210-001作业2
* Prog2.java
*
* description
*
* @author Tristan Shumaker
* /
import java.util.Scanner;

public class main {

public static void main(String [] args){
double [] addQuiz = new double [99]
int counter = 0;
//为输入创建新扫描器
扫描器in = new Scanner(System.in);

//提示用户输入学生姓名
System.out.print(输入学生姓名:);
String name = in.nextLine();

//请求第一个分数和primes循环
System.out.print(Enter Student Score:);
int ScoreInput = in.nextInt();

while(scoreInput> = 0){
System.out.print(Enter Student Score:);
scoreInput = in.nextInt();
counter ++;
}
System.out.println();
System.out.println(Student name:+ name);
System.out.printf(\\\
Average:%1.2f,total(addQuiz,counter));
System.out.printf(\\\
Average:%1.2f,average(addQuiz,counter));
}
}

和我的学生班级:

  public class Student {
private String name;
private int total;
private int counter;

public Student(){
super();
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public int getTotal(){
return total;
}

public void setTotal(int total){
this.total = total;
}

public void addQuiz(int scoreInput){
total + = scoreInput;
int counter ++;
}

public static double average(double [] addQuiz,int counter){
double sum = 0;
for(int t = 0; t sum + = addQuiz [t];
}
return(double)sum / counter;
}
}

任何帮助你们能够给予感谢高级。

解决方案

更改 int counter ++; addQuiz()方法只是 counter ++; ,否则你试图声明一个标识符 counter ++ 这不是一个有效的标识符。此外,由于您已经在 Student 类上声明 average()是静态方法,因此您需要调用它像这样:

  Student.average(addQuiz,counter); 

我没有看到 total()的定义

b

为了回答为什么 average()返回零,看起来你从来没有在 addQuiz double数组,所以它将包含所有的零,因此 sum 将为0.我认为你想要做的是在main方法中改变你的while循环,将计数器中的 scoreInput 值放在数组中 index like such:

  while(scoreInput> = 0){
System.out.print (输入学生成绩:);
scoreInput = in.nextInt();
addQuiz [counter] = scoreInput;
counter ++;
}


I have to create a code that takes user input for grades based on a students name that the user has inputted.

The input is to stop when a number less than 0 is inputted and the output should be the student name, the total of all the scores, and the average score.

For some reason I cannot get the average or the total to print, and my counter in my student class is showing an error "remove this token '++'"

Here is my main class, and my student class :

/**
* COSC 210-001 Assignment 2
* Prog2.java
* 
* description
* 
* @author Tristan Shumaker
*/
import java.util.Scanner;

public class main {

    public static void main( String[] args) {
        double[] addQuiz = new double[99];
        int counter = 0;
        //Creates new scanner for input
        Scanner in = new Scanner( System.in);

        //Prompts the user for the student name
        System.out.print("Enter Student Name: ");
        String name = in.nextLine();

        // requests first score and primes loop
        System.out.print("Enter Student Score: ");
        int scoreInput = in.nextInt();

        while( scoreInput >= 0 ) {
            System.out.print("Enter Student Score: ");
            scoreInput = in.nextInt();
            counter++;
        }
        System.out.println( );
        System.out.println("Student name: " + name);
        System.out.printf( "\nAverage: %1.2f", total(addQuiz, counter) );
        System.out.printf( "\nAverage: %1.2f", average(addQuiz, counter) );
    }
}

and my student class:

public class Student {
    private String name;
    private int total;
    private int counter;

    public Student() {
        super();
    }

    public String getName() {
        return name;
    }

     public void setName(String name) {
        this.name = name;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public void addQuiz( int scoreInput) {
        total += scoreInput;
        int counter++;
    }

    public static double average( double[] addQuiz, int counter ) {
        double sum = 0;
        for( int t = 0; t < counter; t++) {
            sum += addQuiz[t];
        }
        return (double) sum / counter;
    }
}

Any help you guys are able to give would be greatly appreciated, thanks in advanced.

解决方案

change int counter++; in the addQuiz() method to just counter++;, as otherwise you're trying to declare a variable with identifier counter++ which is not a valid identifier. Also since you've declared average() to be a static method on the Student class you'll need to call it like so:

Student.average(addQuiz, counter);

I'm not seeing a definition for total() in your code so I don't know if the same would apply to that.

EDIT

To answer why average() is returning zero, it looks like you never set any values in the addQuiz double array that you're passing in, so it will contain all zeros, and as a result sum will be 0. I think what you want to do is to change your while loop in the main method to put the scoreInput value in the array at the counter index like so:

while( scoreInput >= 0 ) {
    System.out.print("Enter Student Score: ");
    scoreInput = in.nextInt();
    addQuiz[counter] = scoreInput;
    counter++;
}

这篇关于如何解决我的计数器和平均计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆