0

这就是我所拥有的:

import java.util.*;
import java.text.*;

public class Proj4 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String again = "y";
        final int MAX_STUDENTS = 100;
        final int MIN_EXAM = 0;
        final int MAX_EXAM = 50;
        final int MIN_FINAL = 0;
        final int MAX_FINAL = 100;

        String[] names = new String[MAX_STUDENTS];
        int[] exams = new int[MAX_STUDENTS * 4];
        int student = 1;

        DecimalFormat df = new DecimalFormat("#0.0");

        do {
            System.out.print("Please enter the name of student " + student
                    + ": ");
            String line;
            line = s.nextLine().toUpperCase();
            names = line.split(" ");
            for (int i = 0; i < 4; i++) {
                if (i == 3) {
                    System.out.print("Please enter score for Final Exam: ");
                    exams[i] = s.nextInt();
                }

                else {
                    System.out.print("Please enter score for Exam " + (i + 1)
                            + ": ");
                    exams[i] = s.nextInt();
                    if (student == 1) {
                        if ((exams[0] < MIN_EXAM || exams[0] > MAX_EXAM)
                                || (exams[1] < MIN_EXAM || exams[1] > MAX_EXAM)
                                || (exams[2] < MIN_EXAM || exams[2] > MAX_EXAM)) {
                            System.out.println("Invalid enter 0-50 only...");
                            System.out.print("Please re-enter score: ");
                            exams[i] = s.nextInt();
                        } else if (exams[3] < MIN_FINAL || exams[3] > MAX_FINAL) {
                            System.out.println("Invalid enter 0-100 only...");
                            System.out.print("Please re-enter score: ");
                            exams[i] = s.nextInt();
                        }
                    } else if (student == 2) {
                        if ((exams[0] < MIN_EXAM || exams[0] > MAX_EXAM)
                                || (exams[1] < MIN_EXAM || exams[1] > MAX_EXAM)
                                || (exams[2] < MIN_EXAM || exams[2] > MAX_EXAM)) {
                            System.out.println("Invalid enter 0-50 only...");
                            System.out.print("Please re-enter score: ");
                            exams[i + 4] = s.nextInt();
                        } else if (exams[3] < MIN_FINAL || exams[3] > MAX_FINAL) {
                            System.out.println("Invalid enter 0-100 only...");
                            System.out.print("Please re-enter score: ");
                            exams[i + 4] = s.nextInt();
                        }
                    }
                }
            }
            System.out.print("do you wish to enter another? (y or n) ");
            again = s.next();
            if (again.equalsIgnoreCase("y"))
                student++;
        } while (again.equalsIgnoreCase("y"));

        System.out.println("***Class Results***");
        System.out
                .println(names[1]
                        + ","
                        + names[0]
                        + "   "
                        + "Exam Percentage: "
                        + ((float) (exams[0] + exams[1] + exams[2] + exams[3]) / (MAX_EXAM * 3 + MAX_FINAL))
                        * 100 + "%");
        if (student == 2)
            ;
        System.out
                .println(names[3]
                        + ","
                        + names[2]
                        + "   "
                        + "Exam Percentage: "
                        + ((float) (exams[4] + exams[5] + exams[6] + exams[7]) / (MAX_EXAM * 3 + MAX_FINAL))
                        * 100 + "%");
        if (student == 3)
            ;
        System.out
                .println(names[5]
                        + ","
                        + names[4]
                        + "   "
                        + "Exam Percentage: "
                        + ((float) (exams[8] + exams[9] + exams[10] + exams[11]) / (MAX_EXAM * 3 + MAX_FINAL))
                        * 100 + "%");
        if (student == 4)
            ;
        System.out
                .println(names[7]
                        + ","
                        + names[6]
                        + "   "
                        + "Exam Percentage: "
                        + ((float) (exams[12] + exams[13] + exams[14] + exams[15]) / (MAX_EXAM * 3 + MAX_FINAL))
                        * 100 + "%");
    }
}

我的程序似乎完全按照我想要/需要的方式运行,唯一的问题是,当我允许程序再次运行时,它在同一行输出两个字符串,如下所示:

Please enter the name of student 2: Please enter score for Exam 1:

我不知道该怎么做才能解决这个问题。我的代码中是否有一些东西只在第二次,可能是第三次和第四次出现混乱?

4

2 回答 2

1

去掉 if 后面的分号

if (student == 3)
            ; // <- remove it
    System.out.println(//...

因为现在 Java 将其理解为

if (student == 3){}
System.out.println(//...

改变

System.out.print("do you wish to enter another? (y or n) ");
again = s.next();

System.out.print("do you wish to enter another? (y or n) ");
again = s.nextLine();

next不会消耗新的行标记,所以当你使用nextLine之后next它只会消耗这个新的行标记并转到另一个指令。同样的规则适用nextInt


要存储学生姓名数组,您可以使用二维字符串数组

String[][] names = new String[MAX_STUDENTS][];

并根据学号在每一行存储学生姓名

names[student] = line.split(" ");

要获得第一个学生的名字,您必须使用此表格

names[0][0]; //you probably know that indexes in array starts from zero

要获取所有学生的姓名,您可以遍历每一行,然后遍历列

for(int stId=0; stId<student; stId++){
   for(int nameNumber=0; nameNumber<names[stId].length; nameNumber++){
      // print names[stId][nameNumber]`
于 2013-03-01T23:35:39.093 回答
0

如果您希望字符串在新行上打印,请使用 println 而不是 print,或者在字符串中包含换行符。

于 2013-03-01T23:39:54.773 回答