2

私はこれをしばらくの間回っていました、そして私はもっと混乱しています。私が達成したいことがいくつかあります:

  1. JTextField1に2つのラベルを追加します。最初に電子メールの入力を求めます。2つ目は、@address.comがすでに入力されていることを示します。

  2. String変数を渡す方法がわかりません。たぶん、1つのメソッドだけが必要ですか?

    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
    
4

1 に答える 1

2

コードには、変数を宣言してアクセスする場所に問題があります。どこでも宣言されたり、値が与えられたりすることはありませんsMessagesOutputまた、 で宣言されているメソッドsEmail内でアクセスしようとしたり、宣言されたブロックの外でアクセスしようとしたりします。convertGradesmainsLetterGradeif

メソッドで使用する場合sEmailconvertGrades、呼び出すときにメソッドに渡す必要があります。後で使用するにsLetterGradeは、ブロックの外で宣言する必要があります。また、メソッドからif実際に呼び出すことはないため、そのコードは実行されません。convertGradesmain

にラベルを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
于 2013-03-18T02:16:16.993 に答える