执行不会停止java

执行不会停止java,java,Java,我稍微改变了一下我的程序。新的问题是执行不会停止 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package inplacesort; import java.util.

我稍微改变了一下我的程序。新的问题是执行不会停止

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package inplacesort;

import java.util.*;
/**
 *
 * @author ASUS
 */
public class InplaceSort 
{
    static Scanner console = new Scanner(System.in);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        Vector <Integer> intList = new Vector <Integer> ();

        //getting the numbers from the user
        char ans = 'y';

        while (ans == 'Y' || ans == 'y')
        {
            System.out.print("Enter a Number: ");
            intList.addElement(console.nextInt());

            System.out.print("Do You Want to Continue?(Y/N)");
            ans = console.next().charAt(0);
        }
        System.out.println(intList);
        for (int i = 1; i < intList.size(); i++)
        {
            //if (intList.elementAt(i) < intList.elementAt(i-1))
            //{
            int j = i - 1;
            while (j >= 0 && intList.elementAt(i) < intList.elementAt(j))
            {
                j--;
            }

            if (j == -1)
            {
                j = 0;
            }

            for (int k = intList.size() - 1; k >= j; k--)
            {
                intList.insertElementAt(intList.elementAt(k),k + 1);
            }

            intList.insertElementAt(intList.elementAt(i+1),j);
            intList.removeElementAt(i+1);
            //}
        }
        System.out.print(intList);
    }
}

如果没有堆栈跟踪,我不知道这是否是您遇到的特定问题,因为我看到了几个看起来有点不对劲的问题。但这绝对是错误的:

       while(intList.elementAt(i) < intList.elementAt(j))
            {
                if (j < 0)
                {
                    break;
                }
                j--;
            }
减小j,然后在j处提取元素,然后检查是否<0已经使用它。

内部while循环减少变量j,而不检查它是否为负值。负索引对于索引数组和向量无效

至少将j>=0添加到条件:

while(j >= 0 && intList.elementAt(i) < intList.elementAt(j))
            {
                if (j < 0)
                {
                    break;
                }
                j--;
            }

如果j小于1或等于0,则while循环应该中断。那么您的代码就可以正常工作了:

while(intList.elementAt(i) < intList.elementAt(j))
            {
                if (j < 1)
                {
                    break;
                }
                j--;
            }

这是由于int i=1;我这里有个错误 whilej>0&&intList.elementAti 您已经指定了j>=0,当进入while循环时,这将使j值为-1

纠正上述错误后,程序将执行,但您的排序有一些问题,因为它没有排序

修订代码: package com.example.poly

/* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */

导入java.util。; /* * *@作者华硕 */ 公共类插入排序 { 静态扫描仪控制台=新的ScannerSystem.in

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    Vector <Integer> intList = new Vector <Integer> ();

    //getting the numbers from the user
    char ans = 'y';

    while(ans == 'Y' || ans == 'y')
    {
        System.out.print("Enter a Number: ");
        intList.addElement(console.nextInt());

        System.out.print("Do You Want to Continue?(Y/N)");
        ans = console.next().charAt(0);
    }

    for (int i = 1; i <= (intList.size())-1; i++)
    {
        if (intList.elementAt(i) < intList.elementAt(i-1))
        {
            int j = i - 1;
            while(j>0 && intList.elementAt(i) < intList.elementAt(j))
            {
                j--;
            }
            intList.insertElementAt(intList.elementAt(i),j);
            intList.removeElementAt(i);
        }
    }
    System.out.print(intList);
}

}

请给我们一个stacktrace。我可以添加stacktrace和输入数据,以便我们可以尝试吗?我必须回去工作,但我相当确定您希望这一行上有一个-1:intList.removeElementAti;因为您刚刚在它前面插入了。很抱歉,最后一件事:这不是一个由您的类的名称所暗示的。如果您构建了一个新的向量,或者,理想情况下,通过循环第一个向量,将每个元素插入到正确的位置,那么这将是一种插入排序。因此,如果这是家庭作业,您可能需要再次检查作业:如果讲师想要插入排序,这可能不是他们想要的。在while循环的表达式中添加检查后,if完全是多余的,可以删除。尝试这种样式的答案没有用。说出你改变了什么,为什么。
package test;
import java.util.*;
public class InsertionSort 
{
    static Scanner console = new Scanner(System.in);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        Vector <Integer> intList = new Vector <Integer> ();

        //getting the numbers from the user
        char ans = 'y';

        while(ans == 'Y' || ans == 'y')
        {
            System.out.print("Enter a Number: ");
            intList.addElement(console.nextInt());

            System.out.print("Do You Want to Continue?(Y/N)");
            ans = console.next().charAt(0);
        }

        for (int i = 0; i <intList.size(); i++)
        {
            if (intList.elementAt(i) < intList.elementAt(i))
            {
                int j = i - 1;
                while(intList.elementAt(i) < intList.elementAt(j))
                {
                    if (j < 0)
                    {
                        break;
                    }
                    j--;
                }
                intList.insertElementAt(intList.elementAt(i),j);
                intList.removeElementAt(i);
            }
        }
        System.out.print(intList);
    }
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    Vector <Integer> intList = new Vector <Integer> ();

    //getting the numbers from the user
    char ans = 'y';

    while(ans == 'Y' || ans == 'y')
    {
        System.out.print("Enter a Number: ");
        intList.addElement(console.nextInt());

        System.out.print("Do You Want to Continue?(Y/N)");
        ans = console.next().charAt(0);
    }

    for (int i = 1; i <= (intList.size())-1; i++)
    {
        if (intList.elementAt(i) < intList.elementAt(i-1))
        {
            int j = i - 1;
            while(j>0 && intList.elementAt(i) < intList.elementAt(j))
            {
                j--;
            }
            intList.insertElementAt(intList.elementAt(i),j);
            intList.removeElementAt(i);
        }
    }
    System.out.print(intList);
}