基本上,我试图做一个高分显示,比较名字和时间(越快越好)。我花了一些时间,并根据值(从最小到最大)对它们进行排序。然后我想要根据时间重新安排名字,这是我遇到的麻烦。根据长阵的顺序改变字符串数组的顺序
所以基本上,我想要的输入是这样的:
约翰 - 8
莎莉 - 5
詹姆斯 - 2
弗雷德 - 4
而输出为:
詹姆斯 - 2
佛瑞德 - 4
萨利 - 5
约翰 - 8
目前,输出为:
约翰 - 2
莎莉 - 4
詹姆斯 - 5
弗雷德 - 8
这里是我的代码(注意:你将需要有these text documents):
//not actually sure which of these import statements are really necessary, lol
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.*;
import java.util.Vector;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.io.*;
import java.util.Arrays;
class HallOfFame extends JFrame implements ActionListener
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 350;
int numberOfRows=20;
String[] name = new String[100];
String[] time = new String[100];
//long[] longArrayTime = new long[100];
String[] saneTime = new String[100];
/*it's been a */long[] longTime = new long[100];
long second;
long minute;
long hour;
long miliseconds;
//Declare your objects here...
public static void main(String[] args)
{
HallOfFame frame = new HallOfFame();
frame.setVisible(true); // Display the frame
}
@SuppressWarnings("unchecked") //without this line, a warning appears. i can find no need for it and it's annoying, so i got rid of it :P (It is in relation to the table)
public HallOfFame()
{
try
{
File inFile= new File("names.txt");
FileReader fileReader= new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);
for(int i=0; i<100; i++)
{
name[i]=bufReader.readLine();
}
bufReader.close();
}
catch(IOException tada)
{
JOptionPane.showMessageDialog(null, "Error loading high scores. Please ensure file system is accesible, and that 'name.txt' exists");
}
try
{
File inFile= new File("times.txt");
FileReader fileReader= new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);
for(int i=0; i<100; i++)
{
time[i]=bufReader.readLine();
}
bufReader.close();
}
catch(IOException tada)
{
JOptionPane.showMessageDialog(null, "Error loading high scores. Please ensure file system is accesible, and that 'name.txt' exists");
}
//finished reading scores
for(int i=0;i<100;i++)
{
if(time[i]==null||time[i].equals(("0")))
{
time[i]="3699989"; //a very large value so that non-existant values are at the end of a sorted array
}
longTime[i] = Long.parseLong(time[i]);
}
Arrays.sort(longTime);
for(int i=0;i<100;i++)
{
second = (longTime[i]/1000) % 60;
minute = (longTime[i]/(1000 * 60)) % 60;
hour = (longTime[i]/(1000 * 60 * 60)) % 24;
miliseconds = Long.parseLong((""+longTime[i]).substring(0,2));
saneTime[i] = String.format("%02d:%02d:%d", minute, second, miliseconds);
}
// set the frame properties
setTitle("High Scores");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocationRelativeTo(null);
setResizable(false);
Image icon = new ImageIcon("trophy.png").getImage(); //why does this not work????
setIconImage(icon);
Object columns[] = { "Rank", "Time", "Name" };
DefaultTableModel model = new DefaultTableModel(columns, 0);
for(int i=0;i<numberOfRows;i++)
{
if(time[i]=="3699989")//stop adding scores when array is empty (this large number is "empty" as 0 is defined to it above to make sorting the array work)
{
break;
}
Vector row = new Vector(numberOfRows);
row.add((i+1));
//row.add(time[i]);
row.add(saneTime[i]);
row.add(name[i]);
model.addRow(row);
}
// set the content pane properties
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout());
contentPane.setBackground(Color.white);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
contentPane.add(scrollPane, BorderLayout.CENTER);
for(int i=0;i<numberOfRows;i++)
{
table.setRowHeight(i, 30);
}
//create and place items frame's content pane
// register 'Exit' upon closing as default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) // Actions
{
JButton clickedButton = (JButton) event.getSource();
}
}
基本上,为作业使用正确的数据类型:1)创建一个类来捕获名称/时间/ etc; 2)不要使用字符串来表示时间 - 理想情况下使用java.time中的某些东西,但可能使用整数毫秒或其他值。然后,您可以轻松地对所有数组或项目集合进行排序。 –
请勿使用并行阵列/列表。 Java是一种面向对象的语言。 *使用它!*用你的值创建一个类,并有一个数组/该类的对象列表。现在,你可以分类到你的内心,价值观将永远保持在一起。 – Andreas
@JonSkeet我使用了一个以毫秒为单位的long值来表示时间。它只需要首先从文件中读取字符串,然后将其转换为long。另一个课程也将捕获名称和时间,它只是没有显示在这里,因为其他课程将它保存到这个文件 – Blaine