我已经解决了一段时间了,我变得更加困惑。我要完成几件事:


向JTextField 1添加2个标签。第一个要求他们输入电子邮件。第二个指示已为他们输入@ 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

最佳答案

您的代码在声明和访问变量的位置存在一些问题。 sMessagesOutput从未在任何地方声明或给定值。另外,当在sEmail中声明了convertGrades时,您尝试在main方法中访问sLetterGrade,并尝试在声明其的if块之外访问sEmail

如果要在convertGrades方法中使用sLetterGrade,则需要在调用它时将其传递给该方法。必须在if块之外声明convertGrades以便以后使用。另外,您实际上从未从main方法调用JTextField,因此这些代码都不会执行。

要标记您的Box,我建议将所有标签和字段放在中。这将确保他们都以正确的顺序在一起。

这是您的代码经过编辑以包括所有这些更改:

    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

10-04 18:14