简体   繁体   English

如何让我的程序只输入特定的单词,如果输入的不是所说的单词,请重试输入

[英]How can I make my program only input specific words, if anything other than said word is entered, retry input

I want to make a registration program.我想做一个注册程序。 So there is 5 enrollments possbiel, lets say music, English, history, sports, photography.所以有 5 个注册 possbiel,比如说音乐、英语、历史、体育、摄影。 I want to make it so it will ask the user to enter to enroll into only 3 units out of the 5 options listed above.我想这样做,它会要求用户输入以注册上面列出的 5 个选项中的 3 个单元。 only those 5 are the options, if anything else is entered say like "math" or "jdwhagdwa" then it will ask the user to re enter it.只有这 5 个是选项,如果输入任何其他内容,例如“数学”或“jdwhagdwa”,则它会要求用户重新输入。 Once the student enters the unit it should tell them the classes they have enrolled into and the fee for each class一旦学生进入单元,它应该告诉他们他们注册的课程和每门课程的费用

import java.util.*;
        
    public class CheckMarkEnroll
       {    
         public void Enroll()
           {    
             Scanner in = new Scanner(System.in);
             Scanner sc = new Scanner(System.in);
         
             int i = 0;
             String studentlogin = "u123";
             //Unit name
             String english="Math";
             String history="History";
             String sports="Sports";
             String photography="Photography";
             String music="Music";             
           
             String musicfee ="$1320";
             String englishfee="$1890";
             String historyfee="$1890";
             String sportsfee="$1600";
             String photographyfee="$1500";
            
             String studentId="";
             String course1="";
             String course2="";
             String course3="";

        
             //============================================START OF 
             PROGRAM==================================================================//
             System.out.println("Welcome to student enrollment");
             System.out.println("Please enter your student ID");
             
             studentId = in.nextLine();
             
             System.out.println("Please enter name of units you would like to enroll into. You may enroll 
             into only 4 units. Upon entering please enter if this is your first time or you are 
             repeating this unit. At the end your fees required will be shown"); 
             System.out.println("List of units available to enroll:");
             System.out.println("music");
             System.out.println("english");
             System.out.println("history");
             System.out.println("sports");
             System.out.println("photography");
             
                               
             System.out.println("Please enter name of unit #1 : ");
             course1= sc.nextLine();
             
             System.out.println("Please enter name of unit #2 : ");
             course2= sc.nextLine();
             
             System.out.println("Please enter name of unit #3 : ");
             course3= sc.nextLine();
             
             
             
             
             
            }
        }
             
             
             

You could work with Sets (or any Collection).您可以使用集合(或任何集合)。 One containing all your possible courses and one that saves your entered courses.一个包含您所有可能的课程,另一个保存您输入的课程。

Set<String> courses = new HashSet<>();
Set<String> possibleCourses = new HashSet<>();
possibleCourses.add(english);
possibleCourses.add(history);
possibleCourses.add(sports);
possibleCourses.add(photography);
possibleCourses.add(music);

System.out.println("List of units available to enroll: " + possibleCourses);

while (courses.size() < 3) {
    System.out.println("Please enter name of unit #" + (courses.size() + 1) + " : ");
    String course = sc.nextLine();
    if (!possibleCourses.contains(course)) {
        System.out.println("Please enter a valid course name! Your options are: " + possibleCourses);
        continue;
    }
    if (!courses.contains(course)) {
        courses.add(course);
    } else {
        System.out.println("You have already taken course: " + course);
    }
}

System.out.println("You have taken the courses: " + courses);

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

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