2012-08-29 61 views
0

這裏是我的代碼....爲什麼我的for循環跳過第一步

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

您可能需要格式化您的代碼!它看起來像一個可怕的混亂! – Sujay

+3

爲什麼在已經提供'nextInt()'的'Scanner'上面使用'StringTokenizer'? –

+0

你打算跳過'b = 0'嗎? –

回答

1

我只是猜測這裏,在格式化您的代碼幾乎是不可讀的。

數組從0開始,所以這一點:

int b=1; 

可能需要改變

int b = 0;