尝试运行鸡尾酒代码java [重复]时超出范围异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试运行鸡尾酒代码java [重复]时超出范围异常相关的知识,希望对你有一定的参考价值。

我正在尝试制作鸡尾酒,但我在if (a[i] > a[i + 1])线上得到一个出界的例外,我不知道为什么。

这是完整的代码。对不起,如果这是完全错误的。

import java.util.Arrays;
import java.util.Scanner;

public class Cocktail 
{

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    int count = 0;
    boolean switched = true;
    int[]a = new int[10];
    for (int i = 0; i < a.length; i++)
    {
        int value = input.nextInt();
        a[i] = value;
    }
    System.out.println(a[0]);
    while (switched == true)
    {
        switched = false;
        for (int i = 0; i < a.length; i++)
        {
            if (a[i] > a[i + 1]) 
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                count++;
                switched = true;
            }
        }
        for (int i = a.length; i >= 0; i++)
        {
            if (a[i] > a[i + 1]) 
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                count++;
                switched = true;
            }
        }
        if (switched == false)
        {
            System.out.println(count);
        }
    }

}

}

答案

你需要改变

if (a[i] > a[i + 1])to是这样的 - > if (i < a.length-1 && a[i] > a[i + 1])

问题是它试图达到第11个元素;)

如果可以,这是您的代码的编辑版本:

Scanner input = new Scanner(System.in);
    int count = 0;
    boolean switched = true;


    int[]a = new int[10];
    System.out.println("enter 10 Integers: ");// # Added to make code clearer
    for (int i = 0; i < a.length; i++)
    {

        int value = input.nextInt();
        a[i] = value;
    }
        System.out.println("thankyou, Sorting now!");//# also this one 
    while(switched == true)
    {
        switched = false;
        for (int i = 0; i < a.length; i++)
        {
            if (i < a.length-1 && a[i] > a[i + 1]) // <-- # here was the problem
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                count++;
                switched = true;
            }
        }
        for (int i = a.length; i >= 0; i++)
        {
            if (i < a.length-1 && a[i] > a[i + 1]) //<-- # Also Here 
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                count++;
                switched = true;
            }
        }
        if (switched == false)
        {
            System.out.println("count is "+ count);
        }
    }
    // # added part to print array for testing 
        System.out.println("Sorted Array:");
        for (int i = 0; i <a.length ; i++) {


            System.out.print(a[i]+", ");
        }


    }//main 

}//class

这是输出:

OUTPUT

复制并粘贴它,运行和快乐编码= D.

以上是关于尝试运行鸡尾酒代码java [重复]时超出范围异常的主要内容,如果未能解决你的问题,请参考以下文章

线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:7

Java JDBC SQLException:参数索引超出范围[重复]

for循环中双精度数组的索引超出范围[重复]

发生索引超出范围异常[重复]

为啥此代码显示参数索引超出范围异常? [复制]

SonarQube 扫描仪因线路超出范围而失败