如何使我的程序仅输入特定的单词,如果输入了除上述单词以外的任何内容,请重试输入

问题描述

我想制作一个注册程序。因此,有5名学生possbiel,可以说音乐,英语,历史,体育,摄影。我要这样做,它将要求用户输入以注册上面列出的5个选项中的3个单元。只有这5个是选项,如果输入了其他任何内容(例如“ math”或“ jdwhagdwa”),它将要求用户重新输入。学生进入单位后,应该告诉他们他们已经注册的课程以及每个课程的费用

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

解决方法

您可以使用集合(或任何集合)。一种包含您所有可能的课程,另一种保存您输入的课程。

nzDropdownRender