java代码中的GUI

时间:2013-11-16 15:18:56

标签: java swing

我有一个菜单,所以用户可以从中选择,但是当我输入我选择的号码时,我看不到结果,而是再次显示菜单!谁能告诉我问题在哪里?提前谢谢

import javax.swing.JOptionPane;

public class StudentsMarkComputerProject 
{

    public static void main(String args[]) 
    {
        JOptionPane.showMessageDialog(null, "Welcome");
        JOptionPane.showMessageDialog(null, "Students mark Input Project");
        JOptionPane.showMessageDialog(null, "In this program the user is going to enter the students name, class and marks.Then the program is going to output several information about each student.");
        JOptionPane.showMessageDialog(null, "Class Information must be entered as the name of the class, the subject and the number of students");
        String nameofclass = JOptionPane.showInputDialog("Enter the name of the class");
        String nameofsubject = JOptionPane.showInputDialog("Enter the subject"); 
        int numberofstudents =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students"));

        JOptionPane.showMessageDialog(null,"Class Students Information");
        JOptionPane.showMessageDialog(null,"You must enter the name, surname and " + nameofsubject + " marks for each and every student in class "+ nameofclass);
        StLuciaClassrooms theclass =new StLuciaClassrooms(nameofclass, numberofstudents);
        theclass.enterInformation(numberofstudents);


        int option = 0;
        do
         {

        String ques = "<html><body><b>-----------------Menu Options--------------</b><br/>"
            + "Here you need to enter a number between 1 and 6 according your choice.<br/>"
            + "----------------------------------------------------------------------------<br/>"
            + "1.Search for a particular student.<br/>"
            + "2.Show students names, marks and grades.<br/>"
            + "3.Display passes.<br/>"
            + "4.Display failures.<br/>"
            + "5.Show statistics.<br/>"
            + "6.Exit.<br/>"
            + "Enter you choice<br/></body></html>";
     int opt = Integer.parseInt(JOptionPane.showInputDialog(ques));
     System.out.println(opt);
            switch (option)
            {
                case 1:
                String name = JOptionPane.showInputDialog("Enter the name of the student you want to search for");
                String surname = JOptionPane.showInputDialog("Enter the surname of the class");
                theclass.searchStudents(numberofstudents,name,surname);
                break;
                case 2://show students names, marks and grades
                theclass.showInformation(numberofstudents);
                break;
                case 3://display passes
                theclass.displayPasses(numberofstudents); 
                break;
                case 4://display failures
                theclass.displayFailures(numberofstudents);
                break;
                case 5 :
                theclass.showStatistics(numberofstudents);
                break ;
                case 6 ://exit
                System.out.println("Thanks for using this program , Hope to see you again ! . Have a nice day ");
                break;
                default :
                System.out.println("Error ! ");
            }
        }while(option !=6);

      }
  }

import javax.swing.JOptionPane;
 class StLuciaClassrooms 
{
    String nameofclass;
    StLuciaStudents[] students;//array declaration is done only with square brackets "[]"

    public StLuciaClassrooms (String nameofclass, int numberofstudents)
    {
        this.nameofclass = nameofclass;
        students = new StLuciaStudents[numberofstudents];
    }
    public void enterInformation (int numberofstudents)
    {

        System.out.println("*********************************Please enter the student's information**********************");
        for (int i=0;i < numberofstudents; i++)
        {
            String names=JOptionPane.showInputDialog("Enter sudents names");
            String surnames=JOptionPane.showInputDialog("Enter students surnames"); 
            int marks= Integer.parseInt(JOptionPane.showInputDialog("Enter the students marks"));
            StLuciaStudents kid = new StLuciaStudents(names, surnames, marks);
            students[i]=kid; 
        }
    }

    private String generateGrade (int marks)
    {
        String grade = "a";
          if (marks < 50)
          {
            grade = ("F");
          }else if (marks < 65)
          {
            grade = ("D");
          }else if (marks<75)
          {
            grade = ("C");
          }else if (marks <85)
          {
            grade = ("B");
          }else if (marks < 90)
          { 
            grade = ("A");
          }else if (marks >90&&marks<100)
          {
            grade = ("A+");
          }else if (marks == 100)
          {
            grade = ("A++");
          }else;
          {
            grade = "This is an invalid mark";
          }
          return grade ;



    }
    public void showInformation (int numberofstudents)
    {
        for (int i =0; i < numberofstudents ; i++)
        {
            System.out.println("Name : " + students[i].nameofstudents);
            System.out.println("Surname:"+students[i].surnameofstudents);
            System.out.println("Mark:"+ students[i].markofstudents);
            System.out.println("Grade : " + generateGrade(students[i].markofstudents));
        }
    }

    public void searchStudents (int numberofstudents, String nameofstudents, String surnameofstudents)
    {
        boolean flag = false;
        for (int i=0; i<numberofstudents;i++)
        {
            if (students[i].nameofstudents.equalsIgnoreCase(nameofstudents) && students[i].surnameofstudents.equalsIgnoreCase(surnameofstudents))
            {
                flag = true ;
                System.out.println(students[i].nameofstudents + students[i].surnameofstudents +" is found ! ");
            }
        }

    }   
        public void displayFailures(int numberofstudents)
    {
        for (int i=0;i < numberofstudents;i++)
        {
            if (students[i].markofstudents < 50)
            {
                System.out.println(students[i].nameofstudents + students[i].surnameofstudents);
            }
        }
    }
            public void displayPasses(int numberofstudents)
        {
        for (int i=0;i < numberofstudents;i++)
          {
            if (students[i].markofstudents >=50)
            {
                System.out.println(students[i].nameofstudents + students[i].surnameofstudents);
            }
          }
        }

           public void showStatistics(int numberofstudents)
           {

           int total = 0;
           int maxmark = students[0].markofstudents, first_stud=0;
           int minmark = students[0].markofstudents, last_stud=0;
           String maxname = " ",maxsurname = " ";
           String minname = " ",minsurname = " ";

        for (int i =1; i<numberofstudents; i++)
        {
            if (students[i].markofstudents > maxmark )//continue this as staistics are show in a particular case 
            {
                maxmark = students[i].markofstudents;
                maxname = students[i].nameofstudents;
                maxsurname = students[i].surnameofstudents;
            }
        }
        for (int i=1;i>numberofstudents;i++)
        {
            if (students[i].markofstudents < minmark)
            {
                minmark = students[0].markofstudents;
                minname = students[i].nameofstudents;
                minsurname=students[i].surnameofstudents;
            }
        }

        double average = total/numberofstudents;
        System.out.println("The maximum mark of" + maxname + maxsurname + " is " + maxmark);
        System.out.println("The minimum mark of" + minname + minsurname + " is " + minmark);
        System.out.println("The average is " + average);
           }

}

1 个答案:

答案 0 :(得分:1)

注意: int opt = Integer.parseInt(JOptionPane.showInputDialog(ques));
因此,您的选项存储在 opt 中。但你在switch case中使用选项。所以像这样改变

switch(opt){

}

可选

  while(opt > 6);

所以你只有一次出现菜单并做你的行动。 如果只想出现一次,菜单会删除do while循环。