Java 生成累积频率编号,但以0开头

Java 生成累积频率编号,但以0开头,java,arrays,Java,Arrays,最近我有一个任务,但我坚持在这里。问题在于“等待时间”。它已生成,但以0值开始。我只知道如何做累积频率,但它不是从0开始。我真的需要帮助。 提前感谢。:) ~图1是我的任务。(我必须像这样做桌子) package-simplearray; 导入java.util.*; 公共类SimpleArray{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); 整数进程; 系统输出打印(“进程:”); numberofprocesss=input.nextIn

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

package-simplearray;
导入java.util.*;
公共类SimpleArray{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
整数进程;
系统输出打印(“进程:”);
numberofprocesss=input.nextInt();
String[]进程=新字符串[NumberOfProcess];
int[]CPUburst=新的int[numberofprocesss];
int[]优先级=新的int[numberofprocesss];
int[]waitingTime=新的int[NumberOfProcess];
for(int i=0;i>”;
进程[i]=input.next();
System.out.print(“输入CPU突发>>”;
CPUburst[i]=input.nextInt();
系统输出打印(“输入优先级>>”;
优先级[i]=input.nextInt();
}
System.out.println(“|进程| CPU突发|优先级|等待时间|”);
内部温度=0;
for(int k=0;k
}


您试图计算[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);
}
}
编辑:在运行算法之前按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);
}
}
编辑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);

@伊恩麦克,你好!好久不见了。现在我正在尝试制作一个应用程序。但是我在登录时遇到问题。
studentFullName=editTextStudentFullName.getText().toString();studentID=Long.parseLong(editTextStudentID.getText().toString());如果(studentID<999999999 l|studentID>999999999 l){editTextStudentID.setError(“错误ID”);editTextStudentID.requestFocus()}或者{email=editTextEmail.getText().toString();campus=spinnerCampus.getSelectedItem().toString();createFirebaseAuth(editTextEmail.getText().toString(),editTextStudentID.getText().toString())}/code>,我已经把发生的事记下来了。我不知道它为什么不流行。
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
    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);