简体   繁体   English

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

[英]How can I prevent the user from entering the same text twice

This program basically calculates the gpa, by letting the user enter the number of courses and course code, with the relevant credit and marks. 该程序基本上通过让用户输入课程和课程代码的数量以及相关的学分和分数来计算gpa。 If the course code is entered twice, a message will show (the course is already registered), and it will keep looping until the user has entered all courses with a different course code 如果输入两次课程代码,将显示一条消息(课程已经注册),并且它将一直循环,直到用户输入了具有不同课程代码的所有课程

I have created two methods. 我创建了两种方法。 One to check if the code is already registered and the other for calculating the gpa, the first method that checks the user input, I'm not sure about it. 一个用于检查代码是否已经注册,另一个用于计算gpa,第一个检查用户输入的方法,我不确定。 Because if I entered the course code twice it will only show the message and would allow me to calculate the rest 因为如果我两次输入课程代码,它只会显示信息,并允许我计算其余部分

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));
}

Use a set kind of structure to store all the course code visited, It will avoid unnecessary iteration on your course array 使用一组结构来存储所访问的所有课程代码,这将避免在课程数组上进行不必要的迭代

this method can be enhanced to 这种方法可以增强到

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

Initialise the hashset courses and if the checkCourses method returns false add the course code in courses. 初始化hashset课程,如果checkCourses方法返回false,则在课程中添加课程代码。

Initialize before loop like this 像这样在循环之前初始化

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

your if condition inside loop 你的if条件在循环中

 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]);
}

I made some changes in your code and now it works. 我对您的代码进行了一些更改,现在它可以正常工作。 Changes are described in below code. 更改在以下代码中描述。 There was 3 important changes. 有3个重要的变化。 I tried to make as less changes as possible to make your code work as expected 我尝试尽可能减少更改,以使代码按预期工作

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));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM