如何防止用户两次输入相同的文本

时间:2019-04-08 10:44:13

标签: java arrays

该程序基本上是通过让用户输入课程数量和课程代码以及相关的学分和分数来计算gpa的。如果两次输入课程代码,则会显示一条消息(该课程已注册),并且它将一直循环播放,直到用户使用不同的课程代码输入了所有课程

我创建了两种方法。一个我不确定代码是否已经注册,另一个用于计算gpa,这是第一种检查用户输入的方法,我不确定。因为如果我输入了两次课程代码,它只会显示该消息,并允许我计算剩余的

public static boolean checkCourse(String[] courseList, String code){
    boolean check = false;
    for(int i=0 ; i < courseList.length; i++){
        if(code.equals(courseList[i])) 
            check = true;
        else
            check = false;
     }
     return check;
}


public static double gradeValue(double marks){
     double grade = 1.0;
     if(marks >=95){ grade = 5.0;}
     else if (marks >= 90) { grade = 4.75;}
     else if (marks>=85) { grade = 4.5;}
     else if (marks >= 80) { grade = 4.0;}
     else if (marks >= 75) { grade = 3.5; }
     else if (marks >= 70) { grade = 3.0;}
     else if (marks >= 65) {grade = 2.5 ;}
     else if (marks >= 60) { grade = 2;}
     else if (marks < 60) { grade =1 ;}
      return grade;
}

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter number of courses: ");
    int n = input.nextInt();
    String[] Courses = new String[n];
    int sumOfcreadit=0;
    int sumOfmarks =0;

    for(int i =0; i<Courses.length;i++){
        System.out.print("Enter a course code: ");
        Courses[i] = input.next();
        if(checkCourse(Courses,Courses[i])){
            System.out.println("the course already registered");
            i--;

        }

        System.out.print("Enter a credit: ");
        int credit = input.nextInt();
        System.out.print(" Enter a marks: ");
        int marks = input.nextInt();

        sumOfcreadit += credit;
        sumOfmarks +=marks * credit;


    } 
    double TotalMarks;
    TotalMarks = sumOfmarks /sumOfcreadit;

    System.out.println("The GPA is: "+gradeValue(TotalMarks));
}

2 个答案:

答案 0 :(得分:0)

使用一种类型的结构来存储所有访问过的课程代码,这将避免在您的课程数组上进行不必要的迭代

此方法可以增强为

public static boolean checkCourse(HashSet<String> courses, String code){
     boolean check = false;
     if(courses.contains(code)){
         check = true;
     else
         check = false;
     }
     return check;
}

初始化哈希集课程,如果checkCourses方法返回false,则在课程中添加课程代码。

像这样初始化before循环

 HashSet<String> courseSet = new HashSet<String>();

如果条件在循环内

 if(checkCourse(courseSet,courses[i])){ // check for variable name , name should always start with lower case letter
    System.out.println("the course already regestered ");
    i--;
    // You can use continue if you don't want processing for it
    // it will skip the loop iteration and it will go next iteration
}else{
    courseSet.add(courses[i]);
}

答案 1 :(得分:0)

我对您的代码进行了一些更改,现在可以使用了。更改在以下代码中描述。有3个重要更改。 我试图进行尽可能少的更改以使您的代码按预期工作

public static boolean checkCourse(String[] courseList, String code) {
    boolean check = false;
    for (int i = 0; i < courseList.length; i++) {
        if (code.equals(courseList[i])) {  // equals instead of == to compare strings
            check = true;
            break; // you have to break loop if it is true because else statement before returned false even if there was the same course code due to null values in next array elements which was not filled yet
        }

    }
    return check;
}

public static double gradeValue(double marks) {
    double grade = 1.0;
    if (marks >= 95) {
        grade = 5.0;
    } else if (marks >= 90) {
        grade = 4.75;
    } else if (marks >= 85) {
        grade = 4.5;
    } else if (marks >= 80) {
        grade = 4.0;
    } else if (marks >= 75) {
        grade = 3.5;
    } else if (marks >= 70) {
        grade = 3.0;
    } else if (marks >= 65) {
        grade = 2.5;
    } else if (marks >= 60) {
        grade = 2;
    } else if (marks < 60) {
        grade = 1;
    }
    return grade;
}

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter number of courses: ");
    int n = input.nextInt();
    String[] Courses = new String[n];
    int sumOfcreadit = 0;
    int sumOfmarks = 0;

    for (int i = 0; i < Courses.length; i++) {
        System.out.print("Enter a course code: ");
        String code = input.next();
        if (checkCourse(Courses, code)){
            System.out.println("the course already regestered ");
            i--;
            continue; // continue is neccessary to let user write value again if it already exists
        }
        Courses[i] = code;
        System.out.print("Enter a credit: ");
        int credit = input.nextInt();
        System.out.print(" Enter a marks: ");
        int marks = input.nextInt();

        sumOfcreadit += credit;
        sumOfmarks += marks * credit;

    }
    double TotalMarks;
    TotalMarks = sumOfmarks / sumOfcreadit;

    System.out.println("The GPA is: " + gradeValue(TotalMarks));

}