Java 在JOptionPane对话框之前使用默认构造函数会出错吗?
好吧,我对JOptionPane的技巧不太熟悉,但基本上我只是尝试调用一个对话框,但当在对话框之间或之前有构造函数语句时,它会一直搞得一团糟。看一看Java 在JOptionPane对话框之前使用默认构造函数会出错吗?,java,Java,好吧,我对JOptionPane的技巧不太熟悉,但基本上我只是尝试调用一个对话框,但当在对话框之间或之前有构造函数语句时,它会一直搞得一团糟。看一看 import java.util.Scanner; //imports Scanner class from util package; takes input from the user import javax.swing.JOptionPane; //imports JOptionPane dial
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.java
import 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
我已经弄明白了为什么没有调用新对话框。原因是在Advise的构造函数中,您遇到了一个无止境的while循环。在查看了Advise中调用的方法之后,我意识到这是因为setConcentration方法,原因有以下三个 (一) 这是将“This.concentration”的值设置为实例变量,而不是本地方法。但是,在while循环检查中,您仅比较局部方法变量。在将实例变量设置为局部方法变量时,可以通过仅比较局部方法变量来解决此问题 (二) 这里使用双等于来比较字符串。这不是Java中比较字符串的方式,所有这些检查都将返回false,while循环检查也将返回false。在Java中,可以使用
str1.equals(str2)
看到这个帖子了吗
(三)
在这里,即使其他两个问题都解决了,你也会被困在这个循环中,因为如果浓度不是IT,is或CS,你会继续在这个循环中。XX不是其中之一,因此循环将永远持续下去。相反,这应该是一个if循环
下面是最终的setConcentration方法,它将修复无限for循环。代码现在可以在我的计算机上完全工作
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添加一个建议,然后点击4查看所有建议,但它说没有输入任何建议。。。(应该是评论,但我不能评论)您可以为Advise添加一个代码链接,或者在这里发布吗?这些构造函数中可能存在不允许代码进入while true循环的情况。另外,你能在输入框上方的while-true循环中添加一个println,看看是否打印了任何内容吗?没错,你向我们展示了所有的代码,除了重要的代码,那是混乱的代码。为什么?但不要“链接”到建议。在这里发布所有内容。@Bucco我现在将测试println。@Bucco我刚刚尝试在你说的地方添加println。它没有打印任何内容。与主错误无关,但您应该使用.equals来比较字符串。看见
if(concentration == "IT" || concentration == "IS" || concentration == "CS")
str1.equals(str2)
while(!(concentration == "IT" || concentration == "IS" || concentration == "CS"))
{
this.concentration = "XX";
}//end while
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()