gpt4 book ai didi

java - 生成累积频率数但从 0 开始

转载 作者:行者123 更新时间:2023-11-30 10:19:14 24 4
gpt4 key购买 nike

最近有任务,卡在了这里。问题是“等待时间”。它已生成,但以 0 值开始。我只知道如何计算累积频率,但它不是从 0 开始的。我真的需要帮助。提前致谢。 :)~图1是我的任务。 (我必须像这样做表格)

package simplearray;
import java.util.*;
public class SimpleArray {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int numberOfProcesses;
System.out.print("Processes: ");
numberOfProcesses = input.nextInt();

String[] process = new String[numberOfProcesses];
int[] CPUburst = new int[numberOfProcesses];
int[] priority = new int[numberOfProcesses];

int[] waitingTime = new int[numberOfProcesses];

for (int i = 0; i < numberOfProcesses; i++) {
System.out.print("Process>> ");
process[i] = input.next();

System.out.print("Enter the CPU Burst>> ");
CPUburst[i] = input.nextInt();

System.out.print("Enter the Priority>> ");
priority[i] = input.nextInt();
}

System.out.println(" | Process | CPU Burst | Priority | Waiting Time | ");

int temp = 0;
for (int k = 0; k < numberOfProcesses; k++) {

waitingTime[k] = waitingTime[k] + CPUburst[k];
temp = waitingTime[k];

System.out.println(" | " + process[k] + " | " + CPUburst[k] + " | " + priority[k] + " | " + waitingTime[k] + " | ");

}
}

Figure 1

最佳答案

您正在尝试计算 [k] 处的累积等待时间,这是进程 [k-1] 处的等待时间 + 进程 [k-1] 处的 CPU 突发时间,其中 k > 0。

代码如下:

public class SimpleArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int numberOfProcesses;
System.out.print("Processes: ");
numberOfProcesses = input.nextInt();

String[] process = new String[numberOfProcesses];
int[] CPUburst = new int[numberOfProcesses];
int[] priority = new int[numberOfProcesses];

int[] waitingTime = new int[numberOfProcesses];

for (int i = 0; i < numberOfProcesses; i++) {
System.out.print("Process>> ");
process[i] = input.next();

System.out.print("Enter the CPU Burst>> ");
CPUburst[i] = input.nextInt();

System.out.print("Enter the Priority>> ");
priority[i] = input.nextInt();
}

System.out.println(" | Process | CPU Burst | Priority | Waiting Time | ");

int temp = 0;
for (int k = 0; k < numberOfProcesses; k++) {

if (k > 0)
waitingTime[k] = waitingTime[k-1] + CPUburst[k-1];

System.out.println(" | " + process[k] + " | " + CPUburst[k] + " | " + priority[k] + " | " + waitingTime[k] + " | ");

}
double averageWaitTime = 0;
if (numberOfProcesses > 0)
averageWaitTime = (double)waitingTime[numberOfProcesses-1] / (numberOfProcesses);
System.out.println("Average wait time = "+averageWaitTime);
}
}

产生结果:

| Process | CPU Burst | Priority | Waiting Time | 
| A | 7 | 3 | 0 |
| B | 2 | 5 | 7 |
| C | 3 | 1 | 9 |
| D | 6 | 4 | 12 |
| E | 4 | 2 | 18 |
Average wait time = 3.6

编辑:在运行算法之前按 CPU 突发排序

public class SimpleArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int numberOfProcesses;
System.out.print("Processes: ");
numberOfProcesses = input.nextInt();

String[] process = new String[numberOfProcesses];
int[] CPUburst = new int[numberOfProcesses];
int[] priority = new int[numberOfProcesses];

int[] waitingTime = new int[numberOfProcesses];

for (int i = 0; i < numberOfProcesses; i++) {
System.out.print("Process>> ");
process[i] = input.next();

System.out.print("Enter the CPU Burst>> ");
CPUburst[i] = input.nextInt();

System.out.print("Enter the Priority>> ");
priority[i] = input.nextInt();
}

int tempo;
String s;
for (int i = 0; i < numberOfProcesses; i++) {
for (int j = i + 1; j < numberOfProcesses; j++) {
if (CPUburst[i] > CPUburst[j]) {
tempo = CPUburst[i];
CPUburst[i] = CPUburst[j];
CPUburst[j] = tempo;
tempo = priority[i];
priority[i] = priority[j];
priority[j] = tempo;
s = process[i];
process[i] = process[j];
process[j] = s;
}
}
}

System.out.println(" | Process | CPU Burst | Priority | Waiting Time | ");

int temp = 0;
for (int k = 0; k < numberOfProcesses; k++) {

if (k > 0)
waitingTime[k] = waitingTime[k - 1] + CPUburst[k - 1];

System.out.println(" | " + process[k] + " | " + CPUburst[k] + " | " + priority[k]
+ " | " + waitingTime[k] + " | ");

}
double averageWaitTime = 0;
if (numberOfProcesses > 0)
averageWaitTime = (double) waitingTime[numberOfProcesses - 1] / (numberOfProcesses);
System.out.println("Average wait time = " + averageWaitTime);
}
}

现在产生:

 | Process | CPU Burst | Priority | Waiting Time | 
| B | 2 | 5 | 0 |
| C | 3 | 1 | 2 |
| E | 4 | 2 | 5 |
| D | 6 | 4 | 9 |
| A | 7 | 3 | 15 |
Average wait time = 3.0

编辑 2:显示如何确保输入正确

    int numberOfProcesses = 0;
boolean success = true;
do {
try {
System.out.print("Processes: ");
String s = input.nextLine();
numberOfProcesses = Integer.parseInt(s);
success = true;
} catch (NumberFormatException e) {
success = false;
System.out.println("Wrong Input. Please put integer number.");
}
} while (!success);

关于java - 生成累积频率数但从 0 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48732565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com