如何存储值供以后使用?

时间:2016-02-27 18:04:54

标签: java

我是新用户,我有一个“noob”问题。我们正在学校学习Java,我对我们的一项活动有疑问。一个要求是接受学生信息(例如课程)并将它们转换为单个字母(我假设使用.charAt ??),然后计算有多少学生注册该课程。我在这里有学生信息:

import java.util.*;
import java.lang.*;
public class CourseTallier 
{
public static void main (String[] args) 
{
    String student = inputStudInfo();
}
public static String inputStudInfo () 
{
    Scanner kbd = new Scanner(System.in);
    int limit = 0, idnum = 0;
    String college = "";
    System.out.println("Please input a valid ID number:");
    idnum = Integer.parseInt(kbd.nextLine());
    if (idnum == 0)
    {
        System.out.println("Thank you for using this program.");
        System.exit(0);
    }
    while (idnum < limit) {
        System.out.println("Invalid ID number. Please enter a positive integer:");
        idnum = Integer.parseInt(kbd.nextLine());
    }
    System.out.println("Please enter a valid course (BLIS, BSCS, BSIS, or BSIT");
    college = kbd.nextLine();
    while(!college.equalsIgnoreCase("BLIS") && !college.equalsIgnoreCase("BSCS") && !college.equalsIgnoreCase("BSIS") && !college.equalsIgnoreCase("BSIT"))
    {
        System.out.println("Invalid course. Please enter either BLIS, BSCS, BSIS, or BSIT");
        college = kbd.nextLine();
    }
    return college;
}
public static Character convertCourse (String college) 
{
}

正如您所看到的,我被困在“转换课程”方法中(需要模块化)。我想知道如何将“BLIS”之类的内容转换为单个字符“L”,然后创建另一种方法来计算该课程中注册的学生数量。

我不是要求有人为我完成这个程序,因为这会欺骗。我只是想让某人朝着正确的方向努力。非常感谢您的帮助。

编辑:如下所述,具体要求如下: Program

6 个答案:

答案 0 :(得分:2)

要存储未来的值,您知道实例变量是什么吗?除非我误解了这个问题,否则似乎有必要制作四个(静态)实例变量来保存每个课程中注册的用户数。

答案 1 :(得分:0)

您可以使用.charAt方法或使用“switch”语句。 charAt方法的问题在于,您可能无法使用相同的索引字母为每个课程找到不同的字母。(这将再次带您到switch语句)

要计算在该课程中注册的学生人数,您应该有一个计数变量,并在每次将课程转换为单个字符时增加它。

答案 2 :(得分:0)

如果您想从字符串中取出每个字母,请按照以下方式进行:

    Map<String, Character> courseMap = new HashMap<String, Character>();
    courseMap.put("BLIS", 'L');
    courseMap.put("BSCS", 'C');
    courseMap.put("BSIS", 'S');
    courseMap.put("BSIT", 'T');

    for(String courseStr: courseMap.keySet()) {
        System.out.println(courseStr + " > " + courseMap.get(courseStr));
    }

如果要将课程字符串映射到单个字符,可以采用以下方式:

private accountObservable = null;

constructor(private _http: Http) {
}

getAccount () {
    // If we have account cached, use it instead
    if (this.accountObservable === null) {
        this.accountObservable = this._http.get('http://localhost/api/account')
            .map(res => <Account> res.json().data)
            .catch(this.handleError);
    }

    return this.accountObservable;
}

答案 3 :(得分:0)

一种方法是使用switch语句

switch(college)
    {
        case "BLIS":
           return("a");
    }

不确定这是否真的是你的意思,如果你打算存储学生数据,那么实施数据结构的地图将会是开始的

答案 4 :(得分:0)

首先,您需要使代码更加模块化。如何将其划分为多个部分,例如,获取用户输入,验证用户输入,存储用户输入。

要存储用户数据,您可以使用HashMap之类的东西。保持课程成为关键(例如BLIS),并将学生视为价值。在开始时使用0初始化它。

Map<String, Integer> studentCourseHashMap = new HashMap<String, Integer>();
studentCourseHashMap.put("BLIS", 0);

因此,每当用户注册特定课程时,您需要做的就是找到课程并将其增加1.因此,例如,如果学生注册了BLIS课程。然后,

if(studentCourseHashMap.containsKey("BLIS")){
//Checking if BLIS course is available
    Integer noOfStudents = studentCourseHashMap.get("BLIS");
//Increment no of students for each enrollment
    noOfStudents++;
//Saving the updated value in hashmap
    studentCourseHashMap.put("BLIS", noOfStudents);
}

希望这会有所帮助,请在评论中提及您的疑虑。 :)

答案 5 :(得分:0)

为什么不为每门课程使用一个计数器,并在用户输入时递增它。

switch(college)
case BLIS:
    blisCounter+=1;
break;
case BSCS:
    bscsCounter+=1;
break; 
case BSIS:
    bsisCounter+=1;
break;
case BSIT:
    bsitCounter+=1;
break;
相关问题