java - 我需要数组方面的帮助,将用户输入分配给数组,以及 do-while 循环重复

标签 java arrays for-loop constants do-while

我的代码遇到一些问题,这是该程序的总体目标。

One of your professors hears of your emerging programming expertise and asks you to write a SINGLE program that can be used to help them with their grading. The professor gives three 50-point exams and a single 100-point final exam. Your program will prompt the user for the student’s name, entered as Firstname Lastname (i.e. Bob Smith), the student’s 3-exam scores and 1-final exam score (all whole numbers). Class size varies from semester to semester, but 100 is the limit (declare as a constant).

Read in information for ALL students before doing any calculations or displaying any output. Verify that the 3 exam scores are between 0-50 points and that the final is between 0-100 as they are entered. Declared minimums and maximums as constant so that they can easily be updated, as needed. If invalid, display an error message and allow the user to re-enter that invalid score. Once all student info is read in, display each student’s name in the format LASTNAME, FIRSTNAME (all uppercase), the student’s exam percentage (total of all exams plus final / total possible) to 1 decimal and the student’s final grade.

这就是我所拥有的:

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


public class Proj4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
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;

do
{
        System.out.print("PLease enter the name of student " + student + ": " );
        for (int k = 0; k < 1; k++) {
            names[k] = s.nextLine().toUpperCase();
        }
        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((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();
                }
            }
        }
        System.out.print("do you wish to enter another? (y or n) ");
        again = s.next();
        if(again!="y")
            student++;
}while (again.equalsIgnoreCase ("y"));

System.out.println("***Class Results***");
System.out.println(names[1] + "," + names[0] + "   " + "Exam Percentage: "+ ((exams[0]+exams[1]+exams[2]+exams[3])/(MAX_EXAM*3+MAX_FINAL)));

}
}

我遇到的问题是:

  • 弄清楚如何分配用户输入的测试分数,而不仅仅是第一个学生,我相信我已经为其中一个学生设置了正确的分数,但当我转向第二个学生时,它遇到了问题。
  • 由于某种原因,我无法弄清楚该行

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

    不允许我输入任何内容,不是 y 不是 n 不是任何内容,所以我的程序实际上到此结束,这对我来说没有意义,因为我之前已经这样做过并且它有效。

  • 除此之外,如果您发现我的代码中存在任何其他问题,指出这些问题将会非常有帮助。

谢谢

编辑-

更改为后我遇到的新问题

if(!again.equalsIgnoreCase("y"))
            student++;
}while (again.equalsIgnoreCase ("y"));

它让我现在输入内容,但在我输入 y 后,它会将下一行打印为

请输入学生 1 的姓名: 请输入考试 1 的成绩:

我不知道为什么或需要更改什么来解决它,有什么建议吗?

最佳答案

`if(again!="y")` is the culprit here 

您应该使用 equals() 方法来检查字符串是否相等。

if(!again.equals("y"))

关于java - 我需要数组方面的帮助,将用户输入分配给数组,以及 do-while 循环重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15162717/

相关文章:

java - 将来自 REST 调用的 @PathParam 值存储在列表或数组中

arrays - 如何返回包含从临时数组添加的数据的链迭代器?

java - 为什么更改整个数组行会产生奇怪的行为?

c# - 如何在C#中实现类似于Python的for-else和while-else的for-else和foreach-else?

c - 我试图识别在 do-while 中输入的学生人数,并在循环外显示该数字

java - 我应该如何记录参数 "says"?

java - 在 java 中将值传递给返回类型为 void 的构造函数时出现错误?

java - 如何解析字符串并将其获取到另一个 Jframe - JAVA

javascript - 如何将这个json对象做成一个数组?

function - 评估 R 循环中的变量