繁体   English   中英

计算Java项目中的比较次数

[英]Counting the number of comparisons in Java project

我希望在“插入”,“删除”,“搜索”和“打印”功能中输入一些有关如何计算比较值的信息。 该程序需要执行的操作是,可以将“学生”添加到列表中,从列表中删除他们,打印出列表,以及其他功能。 但是,它需要能够计算在插入,删除,搜索和打印时进行了多少次比较。 我知道这需要一个for循环,但是我不能完全正确。 有什么建议吗?

以下是我的代码(警告,它很大):

import java.io.*;
import java.util.*;

import javax.swing.JOptionPane;

public class Assignment3{
static String students = ""; //Records of matching students
static int matches = 0; //Number of matching students
static Set <Student> names = new HashSet<Student>(); //HashSet to store Students

public static void main(String[] args) throws FileNotFoundException{ 

    int ID = 0;                 
    String lastName;        
    double GPA;             
    boolean run = true;

    while (run == true){
        String menu = JOptionPane.showInputDialog(null, "1) Loading students" +
                "\n2) Adding new student" +
                "\n3) Removing student " +
                "\n4) Searching students" +
                "\n5) Printing students " +
                "\n6) Quit");

        //Loading students from a text file
        if (menu.equals("1")){
            String inputFileName = JOptionPane.showInputDialog(null, "Input file:");
            File inputFile = new File(inputFileName);
            Scanner in = new Scanner(inputFile);

            while (in.hasNext()){ //Loading names from text file to array!
                ID = in.nextInt();
                lastName = in.next();
                GPA = in.nextDouble();
                Student student = new Student(ID,lastName,GPA);
                names.add(student);
            }
            in.close(); //Done loading from file, closing input!
        }

        //Adding more students to your list
        if (menu.equals("2")){
            String idString = JOptionPane.showInputDialog(null, "Please Enter an ID for student");
            ID = Integer.parseInt(idString);
            lastName = JOptionPane.showInputDialog(null, "Please enter a last name for student");
            String gpaString = JOptionPane.showInputDialog(null, "Please enter a GPA for student");
            GPA = Double.parseDouble(gpaString);
            Student student = new Student(ID,lastName,GPA);
            names.add(student);
        }

        //Remove student
        if (menu.equals("3")){
            String idString = JOptionPane.showInputDialog(null, "Please enter an ID for student to remove");
            deleteStudentByID(Integer.parseInt(idString),names);
        }

        //Search
        if (menu.equals("4")){
            String searchMenu = JOptionPane.showInputDialog(null, "1) Search by ID" +
                    "\n2) Search by name" +
                    "\n3) Search by GPA");


            if (searchMenu.equals("1")){


                int count = 0;
                for (int n=1; n<ID; n++)  
                    count++;   // insert a[n] into a[0..(n-1)]



                String idSearch = JOptionPane.showInputDialog(null, "Please enter ID number");
                JOptionPane.showMessageDialog(null, "Found " + (findStudentByID(Integer.parseInt(idSearch),names) +  " Student\n" + students));
                System.out.println("The number of comparisons is " + count );

            }
            if (searchMenu.equals("2")){
                String nameSearch = JOptionPane.showInputDialog(null, "Enter last name");
                JOptionPane.showMessageDialog(null, "Found: " + findStudentByName(nameSearch,names) + "\n" + matches + " student(s) with last name of " + nameSearch);
            }
            if (searchMenu.equals("3")){



                String gpaSearch = JOptionPane.showInputDialog(null, "Enter GPA");
                JOptionPane.showMessageDialog(null, "Found " + (findStudentByGPA(Double.parseDouble(gpaSearch),names) +  " Student(s)\n" + students));
            }
        }

        //Print
        if (menu.equals("5")){
            String printMenu = JOptionPane.showInputDialog(null, "1) Print to console" +
                    "\n2) Print to file");
            //Console
            if (printMenu.equals("1")){
                Iterator <Student> iter = names.iterator();
                while (iter.hasNext()){
                    Student tempStudent = iter.next();
                    System.out.println("ID: " + tempStudent.getID() + " Name: " + tempStudent.getName() + " GPA: " + tempStudent.getGPA() + " Hash Code: " + tempStudent.hashCode());    
                }
                JOptionPane.showMessageDialog(null,"Output to console complete");
            }
            //Text file
            if (printMenu.equals("2")){
                String outputFileName = JOptionPane.showInputDialog(null, "Output file:");
                PrintWriter out = new PrintWriter(outputFileName);
                Iterator <Student> iter = names.iterator();
                while (iter.hasNext()){
                    Student tempStudent = iter.next();
                    out.println("ID: " + tempStudent.getID() + " Name: " + tempStudent.getName() + " GPA: " + tempStudent.getGPA() + " Hash Code: " + tempStudent.hashCode());    
                }
                out.close(); //Done writing to file, close output
                JOptionPane.showMessageDialog(null,"Output saved to " + outputFileName);
            }
        }

        //Quit
        if (menu.equals("6")){
            System.exit(0);
        }
    }
}

//Delete a student
static void deleteStudentByID(int id, Set <Student> list){ 
    matches = 0;
    students = "";
    Iterator<Student> iterator = names.iterator();
    while (iterator.hasNext()) {
        Student student = iterator.next();
        if (student.getIDHash() == Integer.toString(id).hashCode()){
            matches++;
            students = "ID: " + student.getID() + " Name: " + student.getName() + " GPA: " + student.getGPA() + "\n";
            list.remove(student);
            JOptionPane.showMessageDialog(null, "Deleted " + matches +  " Student\n" + students);
            break;
        }   
    }

    if (matches == 0){
        JOptionPane.showMessageDialog(null,"Student not found");
    }
}
//Search by ID
static int findStudentByID(int id,Set <Student> list){
    matches = 0;
    students = "";
    for (Student student : list){
        if (student.getIDHash() == Integer.toString(id).hashCode()){
            matches++;
            students = "ID: " + student.getID() + " Name: " + student.getName() + " GPA: " + student.getGPA() + "\n";
        }
    }
    return matches; 
}
//Search by name
static String findStudentByName(String name,Set <Student> list){
    matches = 0;
    students = "";
    for (Student student : list) {
        if (student.getNameHash() == name.hashCode()){
            matches++;
        }
    }
    if (matches > 0){ 
        return "YES";
    }
    return "NO";
}
//Search by GPA
static double findStudentByGPA(double gpa,Set <Student> list){
    matches = 0;
    students = "";
    for (Student student : list){
        if (student.getGPAHash() == Double.toString(gpa).hashCode()){
            matches++;
            students += "ID: " + student.getID() + " Name: " + student.getName() + " GPA: " + student.getGPA() + "\n";
        }
    }
    return matches; 
}

}

只要有一个计数器变量,说:

int count;

并在每次程序进行比较时将其递增。

我建议将责任链模式与抽象方法结合使用。

换句话说,您的示例根本不是面向对象的。 您正在进行函数式编程,这就是为什么这种计数将意味着严重的代码重复的原因(在许多地方您必须执行count++ )。 介绍Comparison类和一系列AbstractComparison实现。

暂无
暂无

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

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