下面这段程序主要实现:计算员工7天的工作时间然后根据总工作时间降序输出数组.在下面程序中可以任意确定员工数目,然后分别把员工的工作时间输入相应的二维数组中排序,然后打印出来.
import javax.swing.JOptionPane;
/*
* Created on 2006-3-11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author eagleshao710
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class workTime1 extends JOptionPane { public static void main(String[] args) {
String numOfEmpString = JOptionPane.showInputDialog(null,"how many employer do you have?","input dialog",JOptionPane.INFORMATION_MESSAGE);
int numOfEmp = Integer.parseInt(numOfEmpString);
int[][] timeTable = new int[numOfEmp][8];
timeTable = InputTime(timeTable);
PrintTable(timeTable);
}
//生成二维数组
static int[][] InputTime(int[][] array){
for (int i = 0;i < array.length;i++){
int sum = 0;//储存员工一周总工作时间
JOptionPane.showMessageDialog(null,"input the time of employer"+i+"from monday to sunday:","show meassage dialog",JOptionPane.INFORMATION_MESSAGE);
for(int j = 0;j <array[i].length;j++){
if(j==7)
array[i][j]= sum;
else{
String timeString = JOptionPane.showInputDialog(null,"input xinqi"+i+"time","input dialog",JOptionPane.QUESTION_MESSAGE);
array[i][j] = Integer.parseInt(timeString);
sum += array[i][j];
}
}
}
return array;
}
/*排序并打印数组*/
static void PrintTable(int[][] array){
int[] empID = new int[array.length];//储存员工号
int[] sign = new int[array.length];//储存标记位
for(int i = 0; i< array.length; i++){
empID[i]= 0;//record the sort
sign[i] = i;
}
/*select the sort*/
/*排序的主要算法是:为了让数组中的数据保持及位置保持不变,用empID记录数组的顺序,用sign储存标记位找到最大数变将标记位置位-1当找到比当前更大的数字时,利用empID还原数字置当前位置为-1*/
int j = 0;
while(j < array.length){
for(int i = 0; i < array.length;i++){
//for(int j = i; j < array.length;j++)
if(sign[i] != -1){
if(array[empID[j]][array[i].length-1] < array[i][array[i].length-1]){
sign[empID[j]] = empID[j];
empID[j] = sign[i];
sign[i] = -1;
}
/*else if(array[empID[j]][array[i].length-1] == array[i][array[i].length-1]){
empID[j] =
}*/
//else empID[j] = sign[i];
}
else continue;
}
j++;
}
/*测试标记位*/
String output1 = "the sort";
for(int e=0;e<array.length;e++)
output1 += sign[e]+" ";
for(int e=0;e<array.length;e++)
output1 +=empID[e]+" ";
JOptionPane.showMessageDialog(null,output1,"output",JOptionPane.INFORMATION_MESSAGE);
/*print the table */
String output = " sun mon tue wen thu fri sat totaltime";
for(int i = 0; i<array.length;i++){
output += "\n employer"+empID[i];
for(int q = 0; q<array[empID[i]].length;q++)
output += " "+array[empID[i]][q];
}
JOptionPane.showMessageDialog(null,output,"output dialog",JOptionPane.INFORMATION_MESSAGE);
}
}
在我调试该程序时发现第一次调试时还能成功,第二次就不能排序了。清各位前辈调试一下,我不知道什么地方错了,不知道还有不有更好的算法,请大家指教!
import javax.swing.JOptionPane;
/*
* Created on 2006-3-11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author eagleshao710
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class workTime1 extends JOptionPane { public static void main(String[] args) {
String numOfEmpString = JOptionPane.showInputDialog(null,"how many employer do you have?","input dialog",JOptionPane.INFORMATION_MESSAGE);
int numOfEmp = Integer.parseInt(numOfEmpString);
int[][] timeTable = new int[numOfEmp][8];
timeTable = InputTime(timeTable);
PrintTable(timeTable);
}
//生成二维数组
static int[][] InputTime(int[][] array){
for (int i = 0;i < array.length;i++){
int sum = 0;//储存员工一周总工作时间
JOptionPane.showMessageDialog(null,"input the time of employer"+i+"from monday to sunday:","show meassage dialog",JOptionPane.INFORMATION_MESSAGE);
for(int j = 0;j <array[i].length;j++){
if(j==7)
array[i][j]= sum;
else{
String timeString = JOptionPane.showInputDialog(null,"input xinqi"+i+"time","input dialog",JOptionPane.QUESTION_MESSAGE);
array[i][j] = Integer.parseInt(timeString);
sum += array[i][j];
}
}
}
return array;
}
/*排序并打印数组*/
static void PrintTable(int[][] array){
int[] empID = new int[array.length];//储存员工号
int[] sign = new int[array.length];//储存标记位
for(int i = 0; i< array.length; i++){
empID[i]= 0;//record the sort
sign[i] = i;
}
/*select the sort*/
/*排序的主要算法是:为了让数组中的数据保持及位置保持不变,用empID记录数组的顺序,用sign储存标记位找到最大数变将标记位置位-1当找到比当前更大的数字时,利用empID还原数字置当前位置为-1*/
int j = 0;
while(j < array.length){
for(int i = 0; i < array.length;i++){
//for(int j = i; j < array.length;j++)
if(sign[i] != -1){
if(array[empID[j]][array[i].length-1] < array[i][array[i].length-1]){
sign[empID[j]] = empID[j];
empID[j] = sign[i];
sign[i] = -1;
}
/*else if(array[empID[j]][array[i].length-1] == array[i][array[i].length-1]){
empID[j] =
}*/
//else empID[j] = sign[i];
}
else continue;
}
j++;
}
/*测试标记位*/
String output1 = "the sort";
for(int e=0;e<array.length;e++)
output1 += sign[e]+" ";
for(int e=0;e<array.length;e++)
output1 +=empID[e]+" ";
JOptionPane.showMessageDialog(null,output1,"output",JOptionPane.INFORMATION_MESSAGE);
/*print the table */
String output = " sun mon tue wen thu fri sat totaltime";
for(int i = 0; i<array.length;i++){
output += "\n employer"+empID[i];
for(int q = 0; q<array[empID[i]].length;q++)
output += " "+array[empID[i]][q];
}
JOptionPane.showMessageDialog(null,output,"output dialog",JOptionPane.INFORMATION_MESSAGE);
}
}
在我调试该程序时发现第一次调试时还能成功,第二次就不能排序了。清各位前辈调试一下,我不知道什么地方错了,不知道还有不有更好的算法,请大家指教!
但是现在你的代码是核心排序、打印用的是static method,而且JOptionPane.
showInputDialog()会导致进程等待,那么如果这时第一个程序没有正常结束
(或者是还没结束,你以为结束了),那么这个时候又启动了第二个或更多同样的进程
的话,肯定会出问题的
改成下面这样试试:
static void PrintTable(int[][] array)
{
int[] empID = new int[array.length];//储存员工号
int[] sign = new int[array.length];//储存标记位
for(int i = 0; i< array.length; i++)
{
empID[i]= i;//record the sort
sign[i] = i;
}
/*select the sort*/
/*排序的主要算法是:为了让数组中的数据保持及位置保持不变,用empID记录数组的顺序,用sign储存标记位找到最大数变将标记位置位-1当找到比当前更大的数字时,利用empID还原数字置当前位置为-1*/
for(int j = 0; j < array.length; j++)
{
for(int i = j+1; i < array.length;i++)
{
if(array[empID[j]][array[0].length-1] < array[empID[i]][array[0].length-1])
{
int temp = empID[j];
empID[j] = empID[i];
empID[i] = temp;
}
}
}
...