我已经讨论了一段时间了,我变得更加困惑。我想完成一些事情:
向JTextField添加2个标签1.第一个要求他们输入电子邮件。第二个表示已经为他们输入了@ address.com。
我很困惑如何传递String变量,也许我应该只有一个方法?
import javax.swing.*;
import java.awt.*;
public class Email {
public static void main(String[] args) { //main method
//get user input
JPanel panel = new JPanel(); //create new JPanel
JTextField field1 = new JTextField(20); //create a field for email and show 20 characters
panel.add(field1);
JTextField field2 = new JTextField(6); //create a field for grade and show to 6 characters
panel.add(field2);
int iValue = JOptionPane.showConfirmDialog (
null,
panel,
"Please enter students email adress and test grade",
JOptionPane.OK_OPTION); //create button in panel
if (iValue == JOptionPane.OK_OPTION) {
//capture email address as String
String sEmail = field1.getText() + "@address.com";
//capture numerical grade input and convert it to a float
float fGrade = Float.parseFloat(field2.getText());
}
} //end main
public void convertGrades(float fGrade) {
/***"shortcut if" grade conversion***********
String sLetterGrade = (fGrade >= 97) ? "A+" :
(fGrade >= 94) ? "A" :
(fGrade >= 90) ? "A-" :
(fGrade >= 87) ? "B+" :
(fGrade >= 84) ? "B" :
(fGrade >= 80) ? "B-" :
(fGrade >= 77) ? "C+" :
(fGrade >= 74) ? "C" :
(fGrade >= 70) ? "C-" :
(fGrade >= 67) ? "D+" :
(fGrade >= 64) ? "D" :
(fGrade >= 60) ? "D-" : "F";
*************************************************************************/
//experimental grade conversion
// Determine letter grade
String sAlphaGrade = (fGrade >= 90) ? "A" :
(fGrade >= 80) ? "B" :
(fGrade >= 70) ? "C" :
(fGrade >= 60) ? "D" : "F";
//determine + or -
if (fGrade >= 60) {
float fGradeModifier = (fGrade % 10);
String sAlphaModifier = (fGradeModifier >= 7) ? "+" :
(fGradeModifier >= 4)? "" : "-";
//concatenate letter grade and modifier
String sLetterGrade = sAlphaGrade + sAlphaModifier;
} // end if
String sResults = getInput.sEmail + "\n" +
sMessage + "\n" +
"your test score is: " + fGrade + "\n" +
"your letter grade is: " + sLetterGrade;
JOptionPane.showMessageDialog(null, sOutput,"Email and Grade",
JOptionPane.INFORMATION_MESSAGE);
} //end convertGrades
} //end class Email
答案 0 :(得分:2)
您的代码在声明和访问变量的位置存在一些问题。 sMessage
和sOutput
永远不会在任何地方声明或赋值。此外,当您在sEmail
中声明convertGrades
时,尝试访问main
方法中的sLetterGrade
,并尝试在if
阻止之外访问sEmail
被宣布进入。
如果您想在convertGrades
方法中使用sLetterGrade
,则需要在调用时将其传递给方法。 if
必须在convertGrades
块之外声明,以便稍后使用。此外,您实际上从未通过main
方法调用JTextField
,因此不会执行任何代码。
要标记 import javax.swing.*;
import java.awt.*;
public class Email {
public static void main(String[] args) { //main method
//get user input
JPanel panel = new JPanel(); //create new JPanel
Box field1Box = Box.createHorizontalBox(); // Box to hold field1 and its labels
field1Box.add(new JLabel("Enter email: ")); // JLabels can be added directly; we don't need to keep a reference
JTextField field1 = new JTextField(20); //create a field for email and show 20 characters
field1Box.add(field1);
field1Box.add(new JLabel("@address.com"));
panel.add(field1Box); // Adds the Box containing field1 and the two labels to panel
JTextField field2 = new JTextField(6); //create a field for grade and show to 6 characters
panel.add(field2);
int iValue = JOptionPane.showConfirmDialog (
null,
panel,
"Please enter student's email adress and test grade",
JOptionPane.OK_OPTION); //create button in panel
if (iValue == JOptionPane.OK_OPTION) {
//capture email address as String
String sEmail = field1.getText() + "@address.com";
//capture numerical grade input and convert it to a float
float fGrade = Float.parseFloat(field2.getText());
//call convertGrades to display the results
convertGrades(fGrade, sEmail);
}
} //end main
// convertGrades must be static to be called from main
public static void convertGrades(float fGrade, String sEmail) {
/***"shortcut if" grade conversion***********
String sLetterGrade = (fGrade >= 97) ? "A+" :
(fGrade >= 94) ? "A" :
(fGrade >= 90) ? "A-" :
(fGrade >= 87) ? "B+" :
(fGrade >= 84) ? "B" :
(fGrade >= 80) ? "B-" :
(fGrade >= 77) ? "C+" :
(fGrade >= 74) ? "C" :
(fGrade >= 70) ? "C-" :
(fGrade >= 67) ? "D+" :
(fGrade >= 64) ? "D" :
(fGrade >= 60) ? "D-" : "F";
*************************************************************************/
//experimental grade conversion
// Determine letter grade
String sAlphaGrade = (fGrade >= 90) ? "A" :
(fGrade >= 80) ? "B" :
(fGrade >= 70) ? "C" :
(fGrade >= 60) ? "D" : "F";
String sLetterGrade; // This needs to be visible outside of the if
//determine + or -
if (fGrade >= 60) {
float fGradeModifier = (fGrade % 10);
String sAlphaModifier = (fGradeModifier >= 7) ? "+" :
(fGradeModifier >= 4)? "" : "-";
//concatenate letter grade and modifier
sLetterGrade = sAlphaGrade + sAlphaModifier;
} // end if
else {
// sLetterGrade still needs to get a value
sLetterGrade = sAlphaGrade;
}
String sResults = sEmail + "\n" +
// sMessage has been commented out so the program will compile and run
// Uncomment it if you declare it and give it a value somewhere.
// sMessage + "\n" +
"your test score is: " + fGrade + "\n" +
"your letter grade is: " + sLetterGrade;
// Assuming sOutput was meant to mean sResults
JOptionPane.showMessageDialog(null, sResults,"Email and Grade",
JOptionPane.INFORMATION_MESSAGE);
} //end convertGrades
} //end class Email
,我建议将所有标签和字段放在Box
中。这将确保他们都以正确的顺序在一起。
以下是您编辑的代码,其中包含所有这些更改:
{{1}}