如何将额外的变量组合到数组/方法中?

用户名

我创建了一个简单的程序,可以输出学生的详细信息。理想情况下,我想为用户提供添加两个课程的选项。这会需要很多有关更改代码的麻烦吗?如果没有,可以帮忙吗?

class Student
{
    public static void main(String[]args)//The main method; starting point of the program.
    {
        Scanner input = new Scanner(System.in);

        String surName, foreName, courseName;
        int age,telephone;

        System.out.println("\t\t\t***********************************************");    
        System.out.println("\t\t\tWelcome to the Mobile College's Student Records");
        System.out.println("\t\t\t***********************************************");
        System.out.println("\nHow many students would you like to add?");
        int noStudents = input.nextInt();

        Stu [] TheStu = new Stu [noStudents];

        for (int x = 0; x < noStudents; x++)
        {
            System.out.println("Please enter Surname: ");
            surName = input.next();
            System.out.println("Please enter Forename: ");
            foreName = input.next();
            System.out.println("Please enter Age: ");
            age = input.nextInt();
            System.out.println("Please enter Telephone No. ");
            telephone = input.nextInt();
            System.out.println("Which course do you want to add the student to? .......Literacy or Numeracy  ");
            courseName = input.next();

            TheStu [x] = new Stu (surName, foreName, age, telephone, courseName);
        }

        for (int y = 0; y < noStudents; y++)
        {
            System.out.println("\t\t\t***********************************************");
            System.out.println ("\t\t\tName: " + TheStu[y].getfName() + " " + TheStu[y].getsName());
            System.out.println ("\t\t\tAge: " + TheStu[y].getstuAge());
            System.out.println ("\t\t\tTelephone No. 0" + TheStu[y].getphone());
            System.out.println ("\t\t\tEnrolled on the " + TheStu[y].getcourseType() + " Course.");
            System.out.println("\t\t\t***********************************************");
        }

    }// end of class
}

class Stu
{
    private String sName;
    private String fName;
    private int stuAge;
    private int phone;
    private String courseType;

    Stu (String s, String f, int a, int p, String c)
    {
        sName = s;
        fName = f;
        stuAge = a;
        phone = p;
        courseType = c;
    }

    String getsName()
    {
        return sName;
    }   

    String getfName()
    {
        return fName;
    }   

    int getstuAge()
    {
        return stuAge; 
    }

    int getphone()
    {
        return phone; 
    }

    String getcourseType()
    {
        return courseType;
    }
}

如果您想要一些基本的东西,可以将您的课程更改为String数组,这样就可以存储这两个课程。同样,当要求用户输入课程名称时,您可以选择“全部”,这样您就可以自动为所有课程注册学生。请参见下面的代码的稍微修改的版本:

import java.util.Scanner;

class Student
{
public static void main(String[] args)//The main method; starting point of the program.
{
    Scanner input = new Scanner(System.in);

    String surName, foreName, courseName;
    String[] courses = new String[]{};
    int age,telephone;

    System.out.println("\t\t\t***********************************************");    
    System.out.println("\t\t\tWelcome to the Mobile College's Student Records");
    System.out.println("\t\t\t***********************************************");
    System.out.println("\nHow many students would you like to add?");
    int noStudents = input.nextInt();

    Stu [] TheStu = new Stu [noStudents];

    for (int x = 0; x < noStudents; x++)
    {
        System.out.println("Please enter Surname: ");
        surName = input.next();
        System.out.println("Please enter Forename: ");
        foreName = input.next();
        System.out.println("Please enter Age: ");
        age = input.nextInt();
        System.out.println("Please enter Telephone No. ");
        telephone = input.nextInt();
        System.out.println("Which courses do you want to add the student to? .......Literacy or Numeracy or both ");

        courseName = input.nextLine();
        // change to either upper case or lower case for easy treatment
        courseName = courseName.toUpperCase();
        // Also verify that the user entered a valid course name
        if(courseName.equals("LITERACY")){
            courses = new String[]{"LITERACY"};
        } else if(courseName.equals("NUMERACY")){
            courses = new String[]{"NUMERACY"};
        } else if(courseName.equals("BOTH")){
            courses = new String[]{"LITERACY", "NUMERACY"};
        } else{
            System.out.println("Error : You entered an invalid option.... \n This student won't be registered for any courses");
        }

        TheStu [x] = new Stu (surName, foreName, age, telephone, courses);
    }

    for (int y = 0; y < noStudents; y++)
    {
        System.out.println("\t\t\t***********************************************");
        System.out.println ("\t\t\tName: " + TheStu[y].getfName() + " " + TheStu[y].getsName());
        System.out.println ("\t\t\tAge: " + TheStu[y].getstuAge());
        System.out.println ("\t\t\tTelephone No. 0" + TheStu[y].getphone());
        System.out.println ("\t\t\tEnrolled in the following courses : ");
        courses = TheStu[y].getcourseTypes();
        for(int i = 0; i < courses.length; i++){
            System.out.println(courses[i]);
        }
        if(courses.length < 1){
            System.out.println("No Courses");
        }
        System.out.println("\t\t\t***********************************************");
    }

}// end of class
}

class Stu
{
private String sName;
private String fName;
private int stuAge;
private int phone;
private String[] courseTypes;

Stu (String s, String f, int a, int p, String[] c)
{
    sName = s;
    fName = f;
    stuAge = a;
    phone = p;
    courseTypes = c;
}

String getsName()
{
    return sName;
}   

String getfName()
{
    return fName;
}   

int getstuAge()
{
    return stuAge; 
}

int getphone()
{
    return phone; 
}

String[] getcourseTypes()
{
    return courseTypes;
}
}

我以为您是初学者,尝试了尽可能不对代码进行任何重大更改的方法,但是您可以进行很多更改来改进您的代码。快乐编码:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将映射值组合到可观察数组?

如何建立一个使用MuxComp将单个变量组合到数组中的OpenMDAO组?

如何编写一个将方法的输出组合到单个变量中的异步函数?

在Ansible中,如何将来自单独文件的变量组合到一个数组中?

如何基于dict变量名称的正则表达式将多个dict变量组合到数组中

如何将两个值合并/组合到同一数组中的单个键中

如何将数组方法应用于在 javascript 中组成二维数组的单个数组?

如何将2个数组数组组合到对象数组

如何将具有相同键的对象数组组合到一个对象中?

R:如何将任意数量的函数和参数组合到一个计算中

如何将列聚合到JSON数组中?

我如何将数据库中的三个varchars变量组合到嵌入经典ASP页面顶部的SQL Command中的单个变量中

如何将一些数组方法应用于反应中的过滤器搜索

将功能组合到方法中

如何将 2 个 NSDictionary Contain 数组组合到一个 NSMutableArray

如何将可观察的循环结果组合到RxSwift中的数组?

如何将具有相同列名的值组合到 R 中的新数据框中?

如何将列表中的字典集中的每个元素组合到其他字典列表中?

如何将类似的列名称组合到Pandas中的单独行中

如何将列表中的连续数字组合到Haskell的范围中?

如何将多个属性组合到SlickGrid的一个单元格中?

如何将两个维度列表组合到 python 中的点列表

如何将特征的行和列成对组合到keras中的特征矩阵?

如何将30,000张图像组合到定时拍摄的电影中?

如何将JUnit批注组合到自定义批注中?

如何将许多javascript类的功能组合到一个对象中

如何将所有子列表元素组合到一个列表中

如何将 3 个流中的数据聚合到一个组合对象?

如何将这个简单的javascript正确组合到这个小的php代码段中?