我是Java的新手。我了解了基础知识,但没有,但是仍然对基本代码有一些麻烦。无论如何,我编写了这段代码,但是由于不知道如何将其放入代码中而遇到了两个错误。有人可以向我解释如何getInteger
或如何在我的代码中应用它吗?
我知道如何进行用户输入的唯一方法是使用Java中的Scanner
。不是这种方式。这是教授要我使用的主要方法,所以我无法编辑主要方法。它可能很简单,但是我很迷茫,似乎无法正常工作。
class ArrayIns1
{
private long[] theArray;
private int nElems;
public int numCounts;
public ArrayIns1(int max) {
theArray = new long[max];
nElems = 0;
}
public void insert(long value) {
theArray[nElems] = value;
nElems++;
}
public void display() {
System.out.print("A= ");
for (int j = 0; j < nElems; j++)
System.out.print(theArray[j] + " ");
System.out.println("");
}
public void quickSort() {
recQuickSort(0, nElems - 1);
}
public void recQuickSort(int left, int right) {
int size = right - left + 1;
if (size <= 3)
manualSort(left, right);
else {
long median = medianOf3(left, right);
int partition = partitionIt(left, right, median);
recQuickSort(left, partition - 1);
recQuickSort(partition + 1, right);
}
}
public long callSelection(int ind) {
return recSelect(0, nElems - 1, ind - 1);
}
public long recSelect(int left, int right, int index) {
int size = right - left + 1;
if (size <= 3) {
manualSort(left, right);
return theArray[index];
}
long median = medianOf3(left, right);
int partition = partitionIt(left, right, median);
if (partition == index)
return theArray[index];
else if (index < partition)
return recSelect(left, partition - 1, index);
else
return recSelect(partition + 1, right, index);
}
public long medianOf3(int left, int right) {
int center = (left + right) / 2;
if (theArray[left] > theArray[center]) {
swap(left, center);
}
if (theArray[left] > theArray[right]) {
swap(left, right);
}
if (theArray[center] > theArray[right]) {
swap(center, right);
}
swap(center, right - 1);
return theArray[right - 1];
}
public void swap(int dex1, int dex2)
{
long temp = theArray[dex1];
theArray[dex1] = theArray[dex2];
theArray[dex2] = temp;
numCounts += 3;
}
/*
* This is the main partition function .
*/
public int partitionIt(int left, int right, long pivot)
{
int leftPtr = left;
int rightPtr = right - 1;
while (true)
{
while (theArray[++leftPtr] < pivot)
numCounts++;
while (theArray[--rightPtr] > pivot)
numCounts++;
if (leftPtr >= rightPtr)
{
numCounts++;
break;
}
else
swap(leftPtr, rightPtr);
}
swap(leftPtr, right - 1);
return leftPtr;
}
public void manualSort(int left, int right)
{
int size = right - left + 1;
if (size <= 1)
return;
if (size == 2)
{
if (theArray[left] > theArray[right])
{
numCounts++;
swap(left, right);
}
return;
}
else
{
if (theArray[left] > theArray[right - 1])
{
numCounts++;
swap(left, right - 1);
}
if (theArray[left] > theArray[right])
{
numCounts++;
swap(left, right);
}
if (theArray[right - 1] > theArray[right])
{
numCounts++;
swap(right - 1, right);
}
}
}
}
class pp74
{
public static void main(String[] args) {
int maxSize = 7; // array size
int k = 0; // arbitrary index
ArrayIns arr; // reference to array
arr = new ArrayIns(maxSize); // create array
for (int j = 0; j < maxSize; j++) // fill array with
{ // random numbers
long n = (int)(java.lang.Math.random() * 99);
arr.insert(n);
}
arr.display(); // display array
// get k from user
System.out.print("Enter k (smallest is 1): ");
k = getInteger();
// get value of k-th elem
long value = arr.recSelect(0, maxSize - 1, k - 1);
// print value
System.out.println("Value of " + k + "the element is " + value);
arr.display();
} // end main()
}
错误:
error: cannot find symbol
k = getInteger();
^
symbol: method getInteger()
location: class pp74
1 errors
最佳答案
您的代码注释显示您要从用户读取integer
的k
值,
如果是这样,则使用下面的代码
import java.util.Scanner;
..
..
// get k from user
System.out.print("Enter k (smallest is 1): ");
Scanner in = new Scanner(System.in);
int k = in.nextInt();
并创建
ArrayIns1
而不是ArrayIns
的对象