[英]Java using HashMap userinput loop
I saw a student registration sample and wanted to write it using HashMap
in my own way to learn more about HashMap
. 我看到了一个学生注册样本,并想以自己的方式使用
HashMap
编写它,以了解有关HashMap
更多信息。
About the program: 1)You can add student name and student id -class Student 关于程序: 1)您可以添加学生姓名和学生ID -class Student
import java.util.*;
public class Student {
Scanner userInput = new Scanner(System.in);
public String sName(){
System.out.print("Enter student name: ");
String sName = userInput.next();
return sName;
}
public char sID(){
System.out.print("Enter student ID: ");
char sID= userInput.next().charAt(0);
return sID;
}
}
2)A class where I have declared 2 HashMap variables, one for student name and id and the second one for GPA and id -class AddInfo 2)我声明了2个HashMap变量的类,一个用于学生的姓名和ID,第二个用于GPA和ID -class AddInfo
import java.util.*;
public class AddInfo {
public HashMap<String,Character> studentInfo= new HashMap<String,Character>();
public HashMap<Double,Character> studentGPAInfo= new HashMap<Double,Character>();
}
3)You can add student GPA and student id – class StudentGPA 3)您可以添加学生GPA和学生ID –班级StudentGPA
import java.util.*;
public class StudentGPA {
Scanner userInput = new Scanner(System.in);
public double sGPA(){
System.out.print("Enter student GPA: ");
double sGPA = userInput.nextDouble();
return sGPA;
}
public char sID(){
System.out.print("Enter student ID: ");
char sID= userInput.next().charAt(0);
return sID;
}
}
4)The last one where all the information from the above classes are getting utilized using .put - class GetInfo 4)使用.put-class GetInfo利用上述类的所有信息的最后一个
import java.util.*;
public class GetInfo {
public HashMap<String,Double> myStudent=new HashMap<String,Double>();
public void addName(){
Student s= new Student();
StudentGPA sG= new StudentGPA();
AddInfo ai= new AddInfo();
ai.studentInfo.put(s.sName(), s.sID());
ai.studentGPAInfo.put(sG.sGPA(),sG.sID());
if(s.sID()== sG.sID()){
myStudent.put(s.sName(), sG.sGPA());
System.out.println("For student id "+ s.sID()+ "you have " + myStudent);
}
}
}
5)Main class 5)主班
public class Main {
public static void main (String []args){
GetInfo g= new GetInfo();
g.addName();
}
}
My Problem: I think my issue is in the 4th point (GetInfo class) where I am trying to evaluate if the 2 student IDs are same or not. 我的问题:我认为我的问题是在第四点(GetInfo类),我试图评估两个学生ID是否相同。 If they are same then put them in a different
HashMap
variable. 如果它们相同,则将它们放在不同的
HashMap
变量中。
if(s.sID()== sG.sID())
When I execute the program it keeps on asking for Enter Student ID. 当我执行程序时,它会不断询问输入学生ID。
Any direction/advice will be helpful. 任何方向/建议都会有所帮助。 Thank You for your valuable time and input.
感谢您的宝贵时间和投入。
Each time you do 每次你做
if(s.sID()== sG.sID())
You are calling the sID()
method of the class StudentGPA
, which reads: 您呼叫的
sID()
之类的方法StudentGPA
,其内容如下:
public char sID(){
System.out.print("Enter student ID: ");
char sID= userInput.next().charAt(0);
return sID;
}
So of course you are being prompted for input. 因此,当然会提示您输入。
You should write a setSid()
that sets the value in a instance variable, and then a getSid()
method that only returns the sID
value without prompting for input. 您应该编写一个
setSid()
来设置实例变量中的值,然后getSid()
一个getSid()
方法,该方法仅返回sID
值而不提示输入。
Then you if statement will look like this: 然后,如果if语句将如下所示:
if(s.getSid()== sG.getSid())
You have to one time init values and then use getter to get value 您必须一次性初始化值,然后使用getter获取值
public class StudentGPA {
private double sGPA;
private char sID;
Scanner userInput = new Scanner(System.in);
public void initSGPA(){
System.out.print("Enter student GPA: ");
sGPA = userInput.nextDouble();
}
public void initsID(){
System.out.print("Enter student ID: ");
sID= userInput.next().charAt(0);
}
public double getSGPA()
{
return sGPA;
}
public char getSID()
{
return sID;
}
}
In my opinion, the better way is using bean 我认为更好的方法是使用bean
public class StudentGPA {
private double sGPA;
private char sID;
Scanner userInput = new Scanner(System.in);
public void setSGPA(double sGPA){
this.sGPA = sGPA;
}
public void setSID(char sID){
this.sID= sID;
}
public double getSGPA()
{
return sGPA;
}
public char getSID()
{
return sID;
}
}
..... move user intput into addName
or separate procedure in GetInfo
class .....将用户输入移到
addName
或GetInfo
类中的单独过程中
....
System.out.print("Enter student GPA: ");
sG.setSGPA(userInput.nextDouble());
System.out.print("Enter student ID: ");
sG.setSID(userInput.next().charAt(0));
.....
AddInfo ai= new AddInfo();
ai.studentInfo.put(s.getSName(), s.getSID());
ai.studentGPAInfo.put(sG.getSGPA(),sG.getSID());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.