我要創建需要用戶輸入的基礎上檔次的學生的名字,用戶輸入了代碼。如何解決我的櫃檯和平均計算器
輸入是停止時的數小於0被輸入和輸出應爲學生姓名,總的所有分數和平均得分。
出於某種原因,我不能讓我的學生班級平均或打印總,我的計數器顯示錯誤「刪除此令牌「++」
這裏是我的主類,和我學生類:
/**
* 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));
}
}
和我的學生類:
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;
}
}
任何幫助你們能夠給將不勝感激,感謝先進。
非常感謝。這是一個錯誤,我看了一百次,沒有注意到。 – theshu
'addQuiz'是'Student'的一種方法 –
@ScaryWombat是的它是;它包含一個錯誤(OP詢問)。在調用'average()'方法時,它也是OP主類中的雙數組...你想說什麼? – jonk