简体   繁体   English

Java代码中的GUI

[英]GUI in java code

I have a menu so the user can choose from its choices, but when I enter the number of my choice I can't see the result and again the menu will appear instead ! 我有一个菜单,以便用户可以从其选择中进行选择,但是当我输入选择的编号时,我看不到结果,菜单将再次出现! Can someone tell me where is the problem pls? 有人可以告诉我问题出在哪里吗? thanks in advance 提前致谢

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);
           }

}

note: int opt = Integer.parseInt(JOptionPane.showInputDialog(ques)); 注意: int opt = Integer.parseInt(JOptionPane.showInputDialog(ques));
so your option stored in opt . 所以您的选择存储在opt中 but you use option in switch case. 但是您在切换的情况下使用选项 so change like this 所以像这样改变

switch(opt){

}

Optional 可选的

  while(opt > 6);

so you get only one time appear menu and do your action. 因此您只会出现一个出现菜单并执行操作。 if you want only one time appear the menu remove the do while loop. 如果只想出现一次菜单,请删除do while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM