线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException帮助pease [英] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException help pease
问题描述
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author sony
*/
public class CpuSh {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int [] p;
Scanner sc=new Scanner(System.in);
while (true){
System.out.println("Menu");
System.out.println("1.Getting BurstTime");
System.out.println("2.FirstComeFirstServed");
System.out.println("3.ShortestJobFirst");
System.out.println("4.RoundRobin");
System.out.println("5.Priority");
System.out.println("6.Exit");
System.out.println("Enter your choice");
int choice=sc.nextInt();
if (choice==6){
break;
}
System.out.println("Enter the number of processes:");
int n=sc.nextInt();
int [] bt = new int[n];
for(int i=1;i<=n;i++){
System.out.println("Enter the burst time for process p"+i+"=");
bt[i]=sc.nextInt();
}
if (choice==1){
for(int i=1;i<=n;i++){
System.out.println("the Burst time for process p"+i+" is "+ bt[i]);
}
}
int wt[]=new int[n];
wt[1]=0;
if (choice==2){
System.out.println("the wating time for first process p1 is "+wt[1]+" because of FIFS");
for(int i=2;i<=n;i++){
System.out.println("compute the watining time from process p"+i);
wt[i]=bt[i-1]+wt[i-1];
}
int twt=0;
int awt=0;
for(int i=1;i<=n;i++){
twt+=wt[i];
awt=twt/n;
}
System.out.println("the total waiting time is"+twt);
System.out.println("the average weighting time is"+awt);
}
}
}
}
问题
for(int i = 2; i& lt; = n; i ++){
System.out.println(& quot;计算来自流程p& quot; + i的观察时间);
wt [i] = bt [i-1] + wt [i -1];
}
这里也是
for(int i = 1; i& lt; = n; i ++){
System.out.println(& quot;输入流程p& quot的突发时间) ; + i +& quot; =& quot;);
bt [i] = sc.nextInt();
}
the problem in
for(int i=2;i<=n;i++){
System.out.println("compute the watining time from process p"+i);
wt[i]=bt[i-1]+wt[i-1];
}
and here too
for(int i=1;i<=n;i++){
System.out.println("Enter the burst time for process p"+i+"=");
bt[i]=sc.nextInt();
}
推荐答案
for(int i=2;i<=n;i++){
应该是
should be
for(int i = 2; i < n; i++){
数组从 0
计算到(n - 1)
,其中 n
是元素的数量。
arrays are counted from 0
to (n - 1)
, where n
is the number of elements.
这篇关于线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException帮助pease的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!