执行不会停止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
/**
* @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);
}