这是我的代码......
import java.util.*;
public class lozanosjfver21 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the processes");
int nop=sc.nextInt();
int z=nop;
int table1[][] = new int[z][6];
int table2[][] = new int[z][6];
int total = 0;
boolean con[] = new boolean[z];
boolean fin[] = new boolean[z];
boolean lowest[] = new boolean[z];
for(int a=0;a<z;a++) {
System.out.print("Input ARRIVAL TIME and BURST TIME for process"+(a+1)+":");
StringTokenizer st = new StringTokenizer(sc.nextLine());
int b=1;
while(st.hasMoreTokens()) {
table1[a][b] = Integer.parseInt(""+st.nextToken());
table2[a][b] = table1[a][b];
b++;
}
table1[a][0] = (a+1);
table2[a][0] = table1[a][0];
total += table1[a][2];
con[a] = false;
fin[a] = false;
lowest[a] = false;
}
//System.out.println(""+total);
String str[] = new String[total];
for(int c=0;c<total;c++) {
/*
* determines if a row should be considered
* (! done and within the current time)
*/
for(int d=0;d<z;d++) {
if (table1[d][1]<=c&&!fin[d]) {
con[d] = true;
}
}
/*
* determines the first low value
* (the basis for the determining the
* lowest value in each loop)
*/
int low=0;
for(int j=0;j<z;j++) {
if(con[j]) {
low = table1[j][2];
break;
}
}
/*
* determines the lowest value among
* the values considered
*/
for(int k=0;k<z;k++) {
if(table1[k][2]<low&&con[k]) {
low = table1[k][2];
}
}
/*
* marks the lowest
*/
for(int l=0;l<z;l++) {
if(table1[l][2]==low) {
lowest[l] = true;
break;
}
}
//this part does all the actions
for(int f=0;f<z;f++) {
if(lowest[f]) {
table1[f][2] -= 1;
if(table1[f][2]==0) {
fin[f] = true;
str[c] = ""+(f+1);
break;
}
}
}
if(str[c]==null) str[c] = "0";
//clearing
for(int p=0;p<z;p++) {
lowest[p] = false;
con[p] = false;
}
}
for(int n=0;n<total;n++) {
if(str[n]!="0") {
table2[Integer.parseInt(str[n])-1][5] = n+1;
}
}
for(int t=0;t<nop;t++) {
table2[t][4] = table2[t][5] - table2[t][1];
table2[t][3] = table2[t][4] - table2[t][2];
}
System.out.println("\nP\tAT\tBT\tWT\tTT");
for(int x=0;x<z;x++) {
System.out.println(""+table2[x][0]+"\t"+table2[x][1]+"\t"+table2[x][2]+"\t"+table2[x][3]+"\t"+table2[x][4]+"\t");
}
}
这是输出:
enter the processes
4
Input ARRIVAL TIME and BURST TIME for process1:Input ARRIVAL TIME and BURST TIME for process2:0 2
Input ARRIVAL TIME and BURST TIME for process3:2 6
Input ARRIVAL TIME and BURST TIME for process4:6 5
P AT BT WT TT
1 0 0 0 0
2 0 2 -2 0
3 2 6 -8 -2
4 6 5 -11 -6
Process completed.
答案 0 :(得分:1)
我只是猜测,你的代码格式几乎不可读。
数组从0开始,所以这个:
int b=1;
可能需要更改为
int b = 0;