我试图制作一个可以接收三种不同数据输入的程序。第一个学生编号,第二个A标记,第三个B标记。输入数据后,程序将再次循环并询问第一个输入(学号),依此类推。一旦用户输入了所需的数据集,他们就可以通过在第一个输入(学号)上输入0来打破循环,因为这标志着一组新数据的开始。
我如何打印出用户输入的多个数据集?
这是输入和输出的外观:
节目输入:
Student Number: --> 12345678
A Mark: --> 30
B Mark: --> 60
Student Number: --> 62445677
A Mark: --> 40
B Mark: --> 70
Student Number: --> 0
预期产出:
sn = 12345678 B = 60 A = 30 mark = 33.0
sn = 62445677 B = 70 A = 40 mark = 33.0
到目前为止,只能打印出1个数据集:
import java.util.Scanner;
import java.util.*;
public class test
{
public static void main (String[] args) {
Scanner reader = new Scanner(System.in);
// 0 To end the loop and run output! Repeats until 0 is entered!
// Arraylist for each of the Vars!
// Validation
// Have to initialise - 1 So it doesn't terminate
int studentNumber = 1;
int studentNumberPassed = 12;
int aMark = 0;
int bMark = 0;
int markTotal;
double markAverage;
boolean terminate = false;
while (terminate == false) {
studentNumber = reader.nextInt();
if (studentNumber == 0) {
terminate = true;
break;
}
// Once 0 is inputted to terminate it overwrites.
studentNumberPassed = studentNumber;
aMark = reader.nextInt();
bMark = reader.nextInt();
}
markTotal = aMark + bMark;
// Cast to double before calculating
markAverage = (double) markTotal / 2;
//Can also use out.format
System.out.printf("sn = %d ex = %d cw = %d mark = %.1f",
studentNumberPassed, aMark, bMark, markAverage);
}
}
答案 0 :(得分:0)
目前,您在每次迭代中覆盖字段studentNumberPassed
,examMark
和cwMark
。这也是为什么最后只保存最后一个阅读学生的数据,因此你只输出最后一个学生。
您需要将数据存储在某种集合中。或者,您可以直接输出数据,同时迭代。
对于集合,Java中有Collection
(documentation)接口,子类别为List
(documentation)和Set
({{3} })(等等)。 List
可以满足您的需求,例如LinkedList
(documentation)。
这样的集合包含对象。您需要一些容器对象来保存学生的所有数据。我们先设计一下这个容器类:
public class StudentData {
// Member variables for all data
public int mStudentNumber;
public int mCwMark;
public int mExamMark;
// A constructor setting all data
public StudentData(int studentNumber, int cwMark, int examMark) {
// Pass the data to the members variables
this.mStudentNumber = studentNumber;
this.mCwMark = cwMark;
this.mExamMark = examMark;
}
}
请注意,对于良好的设计,我们会使用成员变量private
并使用getter
方法进行访问。但为了简单起见,我们就放弃了。
现在我们构建LinkedList
并收集所有数据:
// Create an empty list
final LinkedList<StudentData> dataList = new LinkedList<>();
// Iterate and collect
while (terminate == false) {
studentNumber = reader.nextInt();
if (studentNumber == 0) {
terminate = true;
break;
}
studentNumberPassed = studentNumber;
cwMark = reader.nextInt();
examMark = reader.nextInt();
// Create container and pass values
final StudentData studentData = new StudentData(studentNumberPassed, cwMark, examMark);
// Add the container to the list
dataList.add(studentData);
}
现在剩下的就是迭代容器并输出数据:
// A "for-each" loop which elegantly iterates all elements in a collection
for (final StudentData studentData : dataList) {
markTotal = studentData.mExamMark + studentData.mCwMark;
markAverage = (double) markTotal / 2;
System.out.printf("sn = %d ex = %d cw = %d mark = %.1f",
studentData.mStudentNumber,
studentData.mExamMark,
studentData.mCwMark,
markAverage);
}
答案 1 :(得分:0)
输入多个数据集(输入),然后打印
它满足您的输入和输出结果,但是有很多方法可以实现多个数据集(输入)然后打印。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
private static List<String> storeResult;
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// 0 To end the loop and run output! Repeats until 0 is entered!
// Arraylist for each of the Vars!
// Validation
int studentNumber = 1; //Have to initialise - 1 So it doesn't terminate
int studentNumberPassed = 12;
int cwMark = 0;
int examMark = 0;
boolean terminate = false;
storeResult = new ArrayList<String>();
while (terminate == false) {
System.out.print("Student Number: --> ");
studentNumber = reader.nextInt();
if (studentNumber == 0) { // Once 0 is inputted to terminate it overwrites.
terminate = true;
displayResult();
return;
}
studentNumberPassed = studentNumber;
System.out.print("CW Mark: --> ");
cwMark = reader.nextInt();
System.out.print("EX Mark: --> ");
examMark = reader.nextInt();
result(studentNumberPassed, cwMark, examMark);
}
}
private static void result(int studentNumberPassed, int cwMark, int examMark) {
int markTotal;
double markAverage;
markTotal = examMark + cwMark;
// Cast to double before calculating
markAverage = (double) markTotal / 2;
String str = "sn = " + studentNumberPassed + " ex = " + examMark + " cw = " + cwMark + " mark = " + markAverage;
storeResult.add(str);
}
private static void displayResult() {
for(int data=0;data<storeResult.size();data++){
System.out.println(""+storeResult.get(data));
}
}
}