好的,所以我不太熟悉JOptionPane的噱头,但基本上我只是想在一个对话框中调用,但是当中间或之前有构造函数语句时,它会继续严重搞乱。看看......
import java.util.Scanner; //imports Scanner class from util package; takes input from the user
import javax.swing.JOptionPane; //imports JOptionPane dialog box class package
public class Proj5
{
public static void main(String[] args)
{
String sName, sInput;
int iMenuChoice;
JOptionPane.showMessageDialog(null, "\t\tWelcome to the Advising Manager!"
+ "\n---------- Created by Dr. Bailey and Sanford Gabrielle ----------"
, "Message", 1);
sName = JOptionPane.showInputDialog(null, "What is the advisor's name?", "Input", 3);
Advisee a1 = new Advisee();
Advisee a2 = new Advisee();
Advisee a3 = new Advisee();
while(true)
{
sInput = JOptionPane.showInputDialog(null, "~~ Please make a selection from the menu below ~~\n\n"
+ "1. Add a new advisee" + "\n2. Update an advisee's information"
+ "\n\n3. Display all advisees" + "\n4. Display advisees who are cleared to graduate"
+ "\n5. Exit" + "\n\n\nWhat is your selection?", "Input", 3);
sInput = sInput.trim();
iMenuChoice = Integer.parseInt(sInput);
int iAdviseeCounter = 0;
switch(iMenuChoice)
当它进入菜单时它会混乱,甚至不显示对话框。我不知道为什么默认的构造函数创建语句是干扰的。
似乎失败了 Advisee a1 = new Advisee();
Advisee a2 = new Advisee();
Advisee a3 = new Advisee();
仅供参考...我的问题没有错误消息。问题很简单:构造函数声明预防了即将出现的程序中的下一个对话框。
哇,哇。抱歉,伙计们继承人Advisee.javaimport java.util.Scanner; //imports directory needed to take input from the user.
import javax.swing.JOptionPane; //imports directory needed to use JOptionPane dialog boxes.
public class Advisee
{
String name; //declares and sets default to the student's name entered by the advisee.
String studentId; //declares and sets default to the student's ID number.
String concentration; //declares and sets default to the student's concentration(will be IT,IS, or CS).
String advisor; //declares and sets default to the student's advisor's name.
String studentInfo; //declares the String that will return a Advisee object's "student information".
int hoursCompleted = 0; //declares and sets default to the number of hours that the student has completed.
boolean majorSheet; //declares the student's major sheet.
boolean intentToGraduate; //declares the student's intent to graduate.
public Advisee() //creates a default constructor for Advisee's student.
{
setName("XXX XXXXX");
setStudentId("XXXXXXXXX");
setConcentration("XX");
setAdvisor("XXX XXXXX");
setHoursCompleted(0);
setMajorSheet(false);
setIntentToGraduate(false);
}//end default constructor
public Advisee(String name,String studentId,String concentration,String advisor,int hoursCompleted,boolean majorSheet,boolean intentToGraduate)
{
this.name = name; //sets the objects attributes equal to the attributes of the Advisee classes attributes.
this.studentId = studentId; //sets the objects attributes equal to the attributes of the Advisee classes attributes.
this.concentration = concentration; //sets the objects attributes equal to the attributes of the Advisee classes attributes.
this.advisor = advisor; //sets the objects attributes equal to the attributes of the Advisee classes attributes.
this.hoursCompleted = hoursCompleted; //sets the objects attributes equal to the attributes of the Advisee classes attributes.
this.majorSheet = majorSheet; //sets the objects attributes equal to the attributes of the Advisee classes attributes.
this.intentToGraduate = intentToGraduate; //sets the objects attributes equal to the attributes of the Advisee classes attributes.
}//end Advisee constructor including all attributes of a the advisee class.
public Advisee(Advisee advisee)
{
this.name = advisee.name; //copies the attributes of an object passed in as a parameter.
this.studentId = advisee.studentId; //copies the attributes of an object passed in as a parameter.
this.concentration = advisee.concentration; //copies the attributes of an object passed in as a parameter.
this.advisor = advisee.advisor; //copies the attributes of an object passed in as a parameter.
this.hoursCompleted = advisee.hoursCompleted; //copies the attributes of an object passed in as a parameter.
this.majorSheet = advisee.majorSheet; //copies the attributes of an object passed in as a parameter.
this.intentToGraduate = advisee.intentToGraduate; //copies the attributes of an object passed in as a parameter.
}//end copy constructor
public void setName(String name)
{
this.name = name;
}//end method setName()
public void setStudentId(String studentId)
{
this.studentId = studentId;
}//end method setStudentId()
public void setConcentration(String concentration)
{
concentration = concentration.trim(); //trims the empty spaces of the string passed into setConcentration().
concentration = concentration.toUpperCase(); //puts the string variable passed into setConcentration() to all upper case.
//validation check.
while(!(concentration == "IT" || concentration == "IS" || concentration == "CS"))
{
this.concentration = "XX";
}//end while
if(concentration == "IT" || concentration == "IS" || concentration == "CS")
{
if(concentration == "IT")
{
this.concentration = "IT"; //sets the concentration of the object that this method is called on to IT.
}//end if
else if(concentration == "IS")
{
this.concentration = "IS"; //sets the concentration of the object that this method is called on to IS.
}//end else if
else
{
this.concentration = "CS"; //sets the concentration of the object that this method is called on to CS.
}//end if else if
}//end if
}//end method setConcentration()
public void setAdvisor(String advisor)
{
this.advisor = advisor;
}//end method setAdvisor
public void setHoursCompleted(int hoursComleted)
{
this.hoursCompleted = hoursCompleted;
}//end method hoursCompleted()
public void setMajorSheet(boolean majorSheet)
{
this.majorSheet = majorSheet;
}//end method setMajorSheet
public void setIntentToGraduate(boolean intentToGraduate)
{
this.intentToGraduate = intentToGraduate;
}//end method setIntentToGraduate()
public String getName()
{
return name;
}//end method getName()
public String getStudentId()
{
return studentId;
}//end getStudentId()
public String getConcentration()
{
return concentration;
}//end getConcentration()
public String getAdvisor()
{
return advisor;
}//end method getAdvisor()
public int getHoursCompleted()
{
return hoursCompleted;
}//end method getHoursCompleted()
public boolean getMajorSheet()
{
return majorSheet;
}//end method getMajorSheet()
public boolean getIntentToGraduate()
{
return intentToGraduate;
}//end method getIntentToGraduate()
public String classification()
{
String studentClassification;
if(hoursCompleted < 30)
{
studentClassification = "Freshman";
}//end if
else if(hoursCompleted >= 30 && hoursCompleted < 60)
{
studentClassification = "Sophomore";
}//end else if
else if(hoursCompleted >= 60 && hoursCompleted < 90)
{
studentClassification = "Junior";
}//end else if
else
{
studentClassification = "Senior";
}//end if else if
//return the String variable that holds the string of the students classification.
return studentClassification;
}//end method classification()
public boolean metGraduationRequirements()
{
if(hoursCompleted >= 120 && majorSheet == true && intentToGraduate == true)
{
return true;
}//end if
else
{
return false;
}//end else
}//end method metGraduationRequirements()
public boolean equals(Advisee student1, Advisee student2)
{
if(student1.getName() == student2.getName())
{
return true;
}//end if
else
{
return false;
}//end else
}//end method equals()
public boolean equivalent(Advisee student)
{
if(this.concentration == student.concentration &&
this.advisor == student.advisor &&
this.hoursCompleted == student.hoursCompleted &&
this.majorSheet == student.majorSheet &&
this.intentToGraduate == student.intentToGraduate)
{
return true;
}//end if
else
{
return false;
}//end else
}//end method equivalent()
public String toString()
{
String strInfo;
String sClassification = classification();
String sCleared = clearedToGraduateMsg();
strInfo = ("Id:" + studentId
+ "Advisor: " + advisor
+ "Concentration: " + concentration
+ "Completed Hours: " + hoursCompleted
+ "Classification: " + sClassification
+ "Cleared for graduation: " + sCleared );
return strInfo;
}//end method toString()
public String clearedToGraduateMsg()
{
String strDisplay;
if(hoursCompleted >= 120 && majorSheet == true && intentToGraduate == true)
{
strDisplay = "Yes, all requirements have been met.";
}//end if
else
{
if(hoursCompleted >= 120)
{
strDisplay = "Yes, hours requirements have been met.";
}
else
{
strDisplay = "Not completed hours.";
}
if(majorSheet == true)
{
strDisplay = "Has filled out major sheet.";
}
else
{
strDisplay = "Has not filed a major sheet.";
}
if(intentToGraduate == true)
{
strDisplay = "Yes, has filed an intent to graduate.";
}
else
{
strDisplay = "Has not filed an intent to graduate.";
}
}//end else
return strDisplay;
}//end method clearedToGraduateMsg()
public void assign(String name,String studentId,String concentration,String advisor,int hoursCompleted,boolean majorSheet,boolean intentToGraduate)
{
name = this.name;
studentId = this.studentId;
concentration = this.concentration;
advisor = this.advisor;
hoursCompleted = this.hoursCompleted;
majorSheet = this.majorSheet;
intentToGraduate = this.intentToGraduate;
}//end method assign()
}//end Advisee class
这是完整的驱动程序类“Proj5.java”
import java.util.Scanner; //imports Scanner class from util package; takes input from the user
import javax.swing.JOptionPane; //imports JOptionPane dialog box class package
public class Proj5
{
public static void main(String[] args)
{
String sName, sInput;
int iMenuChoice;
JOptionPane.showMessageDialog(null, "\t\tWelcome to the Advising Manager!"
+ "\n---------- Created by Dr. Bailey and Sanford Gabrielle ----------"
, "Message", 1);
sName = JOptionPane.showInputDialog(null, "What is the advisor's name?", "Input", 3);
while(true)
{
sInput = JOptionPane.showInputDialog(null, "~~ Please make a selection from the menu below ~~\n\n"
+ "1. Add a new advisee" + "\n2. Update an advisee's information"
+ "\n\n3. Display all advisees" + "\n4. Display advisees who are cleared to graduate"
+ "\n5. Exit" + "\n\n\nWhat is your selection?", "Input", 3);
sInput = sInput.trim();
iMenuChoice = Integer.parseInt(sInput);
int iAdviseeCounter = 0;
switch(iMenuChoice)
{
//case 1: "Add a new advisee"
case 1:
if(iAdviseeCounter == 0)
{
String s1,s2,s3,s4,s5;
int iVal;
s1 = JOptionPane.showInputDialog(null, "What is the advisee's name?", "Input", 3);
s1 = s1.trim();
s2 = JOptionPane.showInputDialog(null, "What is the advisee's student Id?", "Input", 3);
s2 = s2.trim();
s3 = JOptionPane.showInputDialog(null, "What is the student's concentration?", "Input", 3);
s3 = s3.trim();
s4 = JOptionPane.showInputDialog(null, "Who is the student's advisor", "Input", 3);
s4 = s4.trim();
s5 = JOptionPane.showInputDialog(null, "How many hours have they completed?", "Input", 3);
s5 = s5.trim();
iVal = Integer.parseInt(s5);
a1 = new Advisee();
a1.assign(s1,s2,s3,s4,iVal,false,false);
iAdviseeCounter++;
}//end if
else if(iAdviseeCounter == 1)
{
String st1,st2,st3,st4,st5;
int iVal2;
st1 = JOptionPane.showInputDialog(null, "What is the advisee's name?", "Input", 3);
st1 = st1.trim();
st2 = JOptionPane.showInputDialog(null, "What is the advisee's student Id?", "Input", 3);
st2 = st2.trim();
st3 = JOptionPane.showInputDialog(null, "What is the student's concentration?", "Input", 3);
st3 = st3.trim();
st4 = JOptionPane.showInputDialog(null, "Who is the student's advisor", "Input", 3);
st4 = st4.trim();
st5 = JOptionPane.showInputDialog(null, "How many hours have they completed?", "Input", 3);
st5 = st5.trim();
iVal2 = Integer.parseInt(st5);
a2 = new Advisee();
a2.assign(st1,st2,st3,st4,iVal2,false,false);
iAdviseeCounter++;
}//end else if
else if(iAdviseeCounter == 2)
{
String str1,str2,str3,str4,str5;
int iVal3;
str1 = JOptionPane.showInputDialog(null, "What is the advisee's name?", "Input", 3);
str1 = str1.trim();
str2 = JOptionPane.showInputDialog(null, "What is the advisee's student Id?", "Input", 3);
str2 = str2.trim();
str3 = JOptionPane.showInputDialog(null, "What is the student's concentration?", "Input", 3);
str3 = str3.trim();
str4 = JOptionPane.showInputDialog(null, "Who is the student's advisor", "Input", 3);
str4 = str4.trim();
str5 = JOptionPane.showInputDialog(null, "How many hours have they completed?", "Input", 3);
str5 = str5.trim();
iVal3 = Integer.parseInt(str5);
a3 = new Advisee();
a3.assign(str1,str2,str3,str4,iVal3,false,false);
iAdviseeCounter++;
}//end else if
else
{
JOptionPane.showMessageDialog(null, "You have reached your maximum number of advisees!", "Message", 1);
}//end else
break; //break from case 1.
//case 2: "Update and advisee's information"
case 2:
String sName1 = a1.getName();
String sName2 = a2.getName();
String sName3 = a3.getName();
if(iAdviseeCounter == 0)
{
JOptionPane.showMessageDialog(null, "There are no advisees in the system yet", "Message", 1);
}
else
{
switch(iAdviseeCounter)
{
case 1:
JOptionPane.showInputDialog(null, "~~ Please select which advisee's information you need to update ~~\n\n"
+ "\t1. " + sName1 + "\n\n\nWhat is your selection?" , "Input", 3);
break;
case 2:
JOptionPane.showInputDialog(null, "~~ Please select which advisee's information you need to update ~~\n\n"
+ "\t1. " + sName1 + "\n\t2. " + sName2 + "\n\n\nWhat is your selection?" , "Input", 3);
break;
case 3:
JOptionPane.showInputDialog(null, "~~ Please select which advisee's information you need to update ~~\n\n"
+ "\t1. " + sName1 + "\n\t2. " + sName2 + "\n\t3. " + sName3 + "\n\n\nWhat is your selection?", "Input", 3);
break;
}
}
break;
//case 3: "Display all advisees"
case 3:
String sTemp = a1.toString();
String sTemp1 = a2.toString();
String sTemp2 = a3.toString();
if(iAdviseeCounter == 0)
{
JOptionPane.showMessageDialog(null, "There are no advisees in the system yet", "Message", 1);
}//end if
else if(iAdviseeCounter == 1)
{
JOptionPane.showMessageDialog(null, "Advisee Information" + "\n-------------------------------", sTemp, 1);
}//end else if
else if(iAdviseeCounter == 2)
{
JOptionPane.showMessageDialog(null, "Advisee Information" + "\n-------------------------------", sTemp + "\n\n" + sTemp1, 1);
}//end else if
else
{
JOptionPane.showMessageDialog(null, "Advisee Information" + "\n-------------------------------", sTemp + "\n\n" + sTemp1 + "\n\n" + sTemp2, 1);
}//end if else if
break;
//case 4: "Display advisees who are cleared to graduate"
case 4:
if(iAdviseeCounter == 0)
{
JOptionPane.showMessageDialog(null, "There are no advisees in the system yet", "Message", 1);
}//end if
else
{
}//end else
break;
//case 5: "Exit"
case 5:
JOptionPane.showMessageDialog(null, "Goodbye!", "System closing", 1);
System.exit(0);
break;
}//end switch(iMenuChoice)
}//end while loop
}//end method main
}//end class Proj5
答案 0 :(得分:0)
我已经弄清楚为什么没有调用新的对话框。原因是在Advisee的构造函数中,你正在进行无休止的循环。在查看Advisee中调用的方法之后,我意识到这是因为setConcentration方法,原因如下。
1)
while(!(concentration == "IT" || concentration == "IS" || concentration == "CS"))
{
this.concentration = "XX";
}//end while
这是将“this.concentration”的值设置为实例变量,而不是本地方法。但是在while循环检查中,您只是比较本地方法变量。当您将实例变量设置为本地方法变量时,可以通过仅比较本地方法变量直到结束来修复此问题。
2)
if(concentration == "IT" || concentration == "IS" || concentration == "CS")
这里使用双等于比较字符串。这不是你在Java中比较字符串的方式,所有这些检查都将返回false,while循环检查也是如此。在Java中,您可以使用
比较两个字符串str1.equals(str2)
请参阅此帖子Java String.equals versus ==
3)
while(!(concentration == "IT" || concentration == "IS" || concentration == "CS"))
{
this.concentration = "XX";
}//end while
在这里,即使其他两个问题得到修复,你也会陷入这个循环,因为如果集中的不是IT,IS或CS,你将继续循环。 XX不是其中之一,所以循环将永远持续下去。相反,这应该是一个if循环。
下面将修复无限for循环的最终setConcentration方法。代码现在可以在我的计算机上完全运行。
public void setConcentration(String concentration)
{
concentration = concentration.trim(); //trims the empty spaces of the string passed into setConcentration().
concentration = concentration.toUpperCase(); //puts the string variable passed into setConcentration() to all upper case.
//validation check.
if(!(concentration.equals("IT") || concentration.equals("IS") || concentration.equals("CS")))
{
concentration = "XX";
}//end validation
if(concentration.equals("IT") || concentration.equals("IS") || concentration.equals("CS"))
{
if(concentration.equals("IT"))
{
concentration = "IT"; //sets the concentration of the object that this method is called on to IT.
}//end if
else if(concentration.equals("IS"))
{
concentration = "IS"; //sets the concentration of the object that this method is called on to IS.
}//end else if
else
{
concentration = "CS"; //sets the concentration of the object that this method is called on to CS.
}//end if else if
}//end if
this.concentration = concentration;
}//end method setConcentration()
答案 1 :(得分:0)
如果您进行了他提到的更改,它将会运行,但不会存储任何信息。我点击1添加一个建议者,然后我点击4看所有的建议但是它没有输入...(应该是评论,但我无法发表评论)