简体   繁体   English

我的程序中的 if 语句由于某种未知原因停止工作

[英]The if statement in my program stopped working for some unknown reason

So I have been trying to make a program to solve process scheduling problems in java but an if statement isn't working and is in the switch statement case 2 in the below code.所以我一直在尝试编写一个程序来解决 java 中的进程调度问题,但是if statement不起作用,并且在下面的代码中的switch 语句案例 2中。

What is expected to happen is when case 2 of switch statement is called the if statement has to sort the arrays in ascending order.预期发生的情况是,当switch 语句的情况 2被称为 if 语句时,必须按升序对 arrays 进行排序。 But if the if statement isn't executing at all as if getting ignored.但是如果 if 语句根本没有执行,就好像被忽略了一样。 the same if statement was in the method GTable() before and doesn't work anymore.之前的方法GTable()中存在相同的 if 语句,并且不再起作用。

Any help please?请问有什么帮助吗?

import java.util.Scanner;
class OS
{
 public static void GTable(int [] gnt, String [] Processes, int n)
 {
  System.out.println();
  System.out.println("Gant Table:-");
  for(int i=0;i<=n-1;i++)
  { 
   System.out.print("| "+Processes[i]+" ");
  }
  System.out.println("|");
  System.out.print("0    ");
  for(int i=0;i<=n-1;i++)
  {
   System.out.print(gnt[i]+"   ");
  }
  System.out.println("\n");  
 } 
 public static int[] GT_calc(int [] BT, int [] CT, int n)
 {
  int gant=0;
  for(int i=0;i<=n-1;i++)
  { 
   gant=gant+BT[i];
   CT[i]=CT[i]+gant;
  }
 return CT;
 }
 public static void main(String args[])
 {
  int n,sw;
  int Flag=0;
  float Total_TAT=0,Total_WT=0,avg_TAT=0,avg_WT=0;
  OS o=new OS();
  Scanner s=new Scanner(System.in);
  System.out.println("Enter the number of processes: ");
  n=s.nextInt();
  String [] Processes=new String[n];
  int [] CT=new int[n];
  int [] TAT=new int[n];
  int [] WT= new int[n];
  int [] gnt=new int[n];
  System.out.println("Enter the processes ordered according to arrival time:");
  for(int i=0;i<=n-1;i++)
  {
   Processes[i]=s.next();
  }
  int [] Burst_Times=new int[n];
  System.out.println("Enter the Burst Times:");
  for(int i=0;i<=n-1;i++)
  {
   Burst_Times[i]=s.nextInt();
  }
  int BT[]=Burst_Times.clone();
  System.out.println("Enter the type of scheduling to be used(number):-");
  System.out.println("1. First In First Out Scheduling\n2. Shortest Job First Scheduling\n3. Priority Scheduling\n4. Round Robin Scheduling");
  sw=s.nextInt();
  switch(sw)
  {
   case 1:
    System.out.println("First In First Out Scheduling:-");
    o.GT_calc(BT, CT, n);
    o.GTable(gnt, Processes, n);
    break;
   case 2:
    System.out.println("Shortest Job First Scheduling:-");
    o.GT_calc(BT, CT, n);
    int temp;
    String temp1;
    for(int i=1;i<n-1;i++)
    {
     for(int j=i+1;j<n-1;j++)
     {
          if(Burst_Times[i]>Burst_Times[j]) 
          { 
       temp=BT[i];
           BT[i]=BT[j];
           BT[j]=temp;
       temp1=Processes[i];
       Processes[i]=Processes[j];
       Processes[j]=temp1;
       temp=CT[i];
       CT[i]=CT[j];
       CT[j]=temp;
      }   
     }
    }
    for(int i=0;i<=n-1;i++)
    {
     gnt[i]=gnt[i]+CT[i];
    }
        o.GTable(gnt, Processes, n);
    break;
   case 3:
    System.out.println("Coming Soon ;)");
    break;
   case 4:
    System.out.println("Coming Soon ;)");
    break;
    
  }
  for(int i=0;i<=n-1;i++)
  {
   TAT[i]=CT[i]-i;
   WT[i]=TAT[i]-Burst_Times[i];
   Total_TAT=Total_TAT+TAT[i];
   Total_WT=Total_WT+WT[i];
  }
  int ttat=(int)Total_TAT;
  int twt=(int)Total_WT;
  System.out.println("Arival Time    Burst Time    Completion Time    Turn Around Time    Wating Time");
  for(int i=0;i<=n-1;i++)
  {
   System.out.println("    "+i+"    \t"+Burst_Times[i]+"\t\t"+CT[i]+"\t\t\t"+TAT[i]+"   \t\t"+WT[i]);
  }
  System.out.println("Total Turn around Time is: "+ttat);
  System.out.println("Total Waiting Time is: "+twt);
  avg_TAT=Total_TAT/n;
  avg_WT=Total_WT/n;
  System.out.println("Average Turn around Time is: "+avg_TAT);  
  System.out.println("Average Waiting Time is: "+avg_WT);
 }
}

The output screen image has been linked here output 屏幕图像已在此处链接

To sort an array you must compare the values to be sorted.要对数组进行排序,您必须比较要排序的值。 Your current code decides how to sort by looking at the wrong values and therefore cannot sort the arrays.您当前的代码通过查看错误值来决定如何排序,因此无法对 arrays 进行排序。

From my comment:从我的评论:

The condition in the if statement that decides on swapping elements depends on the Burst_Times array, but the actual swapping happens on the BT array (in this answer I left out the processes and the CT arrays because for the explanation they are not needed.) if 语句中决定交换元素的条件取决于Burst_Times数组,但实际交换发生在BT数组上(在这个答案中,我省略了processesCT arrays,因为不需要它们的解释。)

After the first swap operation the Burst_Times and the BT array have diverged, making the complete sorting process unreliable.在第一次交换操作之后, Burst_TimesBT数组已经发散,使得完整的排序过程不可靠。

Lets show this with a simple example (I named the arrays a and b for convenience and extracted the sort into a method):让我们用一个简单的例子来展示这一点(为了方便起见,我将 arrays 命名为ab并将排序提取到一个方法中):

public static void sort() {
    int[] a = { 1, 7, 2, 4, 5 };
    int[] b = a.clone();
    int n = a.length;
    for (int i = 1; i < n - 1; i++) {
        for (int j = i + 1; j < n - 1; j++) {
            if (a[i] > a[j]) {
                int temp = b[i];
                b[i] = b[j];
                b[j] = temp;
            }
        }
    }
    System.out.println(Arrays.toString(a));
    System.out.println(Arrays.toString(b));
}

When the if statement is reached the first time, we have i=1, j=2 and当第一次到达if语句时,我们有 i=1,j=2 和

a: 1, 7, 2, 4, 5
b: 1, 7, 2, 4, 5

Since a[i] > a[j] which is a[1] > a[2] or 7 > 2 is true, the corresponding elements in b are swapped:由于a[i] > a[j]a[1] > a[2]7 > 2为真,因此b中的相应元素被交换:

a: 1, 7, 2, 4, 5
b: 1, 2, 7, 4, 5

The inner loop executes once again (with i=1, j=3) and now a[i] > a[j] is a[1] > a[3] or 7 > 4 (since the element a[1] was not touched) which is also true.内部循环再次执行(i=1,j=3),现在a[i] > a[j]a[1] > a[3]7 > 4 (因为元素a[1]是未触及)这也是正确的。 Therefore b[1] and b[3] are swapped:因此b[1]b[3]被交换:

a: 1, 7, 2, 4, 5
b: 1, 4, 7, 2, 5

The inner loop is now terminated (j = 4 is not smaller than n-1 ).内部循环现在终止(j = 4 不小于n-1 )。

We have one pass left in the outer loop (i=2, j=3).我们在外循环中还剩一趟(i=2,j=3)。 The comparison there is a[2] > a[3] which is 2 > 4 which is false and therefore the elements in b are not changed.比较有a[2] > a[3]这是2 > 4这是错误的,因此b中的元素不会改变。

In the end your algorithm "sorts" the b array as 1, 4, 7, 2, 5 .最后,您的算法将b数组“排序”为1, 4, 7, 2, 5


How can you fix it你怎么能解决它

As stated in the first sentence of this answer: To sort an array you must compare the values to be sorted如该答案的第一句话所述:要对数组进行排序,您必须比较要排序的值

In my example could this means to compare the values of the b array in the if statement.在我的示例中,这是否意味着在if语句中比较b数组的值。

In your code this means to compare the values of the BT array.在您的代码中,这意味着比较BT数组的值。


Just to be complete: your algorithm starts with i = 1 and therefore never sorts the first element (which, according to your comment, is desired).为了完整起见:您的算法从i = 1开始,因此永远不会对第一个元素进行排序(根据您的评论,这是需要的)。

It also never sorts the last element since the inner loop terminates before j reaches the last element.它也永远不会对最后一个元素进行排序,因为内部循环在j到达最后一个元素之前终止。 Is is unclear whether this is also desired.目前还不清楚这是否也是需要的。

If you want to sort including the last element then the inner loop must be如果要排序包括最后一个元素,则内部循环必须是

    for (int j = i + 1; j < n; j++)

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

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