gpt4 book ai didi

java - 由于某种原因无法调用方法

转载 作者:行者123 更新时间:2023-12-02 09:29:41 24 4
gpt4 key购买 nike

我尝试进行冒泡排序,但也测量处理所需的时间,我遇到的第一个问题是,即使知道一切对我来说都是正确的,我也无法调用 bubbleSort 方法。我的第二个问题是 long startTime = System.nanoTime();显示为未声明,但我确实在我的代码之上声明了。

import java.util.Random;
import java.util.Scanner;
//import java.util.concurrent.TimeUnit;

// Program to calculate execution time or elapsed time in Java
class Main
{
private static Scanner scan;

public static void main(String[] args) throws InterruptedException {

long startTime = System.nanoTime();

// ... the code being measured starts ...

scan = new Scanner(System.in);
Random rand = new Random();
int size;
int num;
int values[];

System.out.println("What is the size of the array?");
size = scan.nextInt();

values = new int[size];
System.out.println("The " + size + " random numbers are:");
for(int c = 0; c < size; c++)
{
num = rand.nextInt(100);
System.out.print((values[c] = num) + " ");
}
System.out.println("In order:");
for (int count = 0; count < values.length; count++)
System.out.println(count + " = " + bubbleSort(values[count]));
}

public static void bubbleSort(int[] arr)
{
boolean swap;
do
{
swap = false;
int temp;
for (int count = 0; count < arr.length - 1; count++)
if (arr[count] > arr[count+1])
{
temp = arr[count];
arr[count] = arr[count+1];
arr[count+1] = temp;
swap = true;


}
} while (swap);
System.out.println("In order:");
for (int count = 0; count < arr.length; count++)
System.out.print(arr[count] + " ");
}

// ... the code being measured ends ...
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
}

最佳答案

您的变量是在主函数内声明的,这意味着它仅在主函数内可见。尝试像这样声明它:

class Main
{
private static Scanner scan;
long startTime;

public static void main(String[] args) throws InterruptedException {

startTime = System.nanoTime();

// ... the code being measured starts ...
[...]

第二个问题:您试图将 bubble-Function 打印为字符串,但它只是一个 void 函数。

编辑:我稍微清理了你的代码:

import java.util.Random;
import java.util.Scanner;
//import java.util.concurrent.TimeUnit;

// Program to calculate execution time or elapsed time in Java
class Main {
private static Scanner scan;
static long startTime;

public static void main(String[] args) throws InterruptedException {
startTime = System.nanoTime();
// ... the code being measured starts ...
scan = new Scanner(System.in);
Random rand = new Random();
int size;
int num;
int values[];
System.out.println("What is the size of the array?");
size = scan.nextInt();
values = new int[size];
for (int c = 0; c < size; c++) {
values[c] = rand.nextInt(100);
}
// print the random values
System.out.println("The " + size + " random numbers are:");
printValues(values);
//Sort the array here:
bubbleSort(values);
//print the sorted values
System.out.println("The " + size + " random numbers in order are:");
printValues(values);
// ... the code being measured ends ...
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
System.out.print("The algorithm took " + timeElapsed + " ns to terminate.");
}

public static void printValues(int[] values) {
for (int count = 0; count < values.length; count++) {
System.out.print(values[count] + " ");
}
System.out.println();
}

public static void bubbleSort(int[] arr) {
boolean swap;
do {
swap = false;
int temp;
for (int count = 0; count < arr.length - 1; count++)
if (arr[count] > arr[count + 1]) {
temp = arr[count];
arr[count] = arr[count + 1];
arr[count + 1] = temp;
swap = true;
}
} while (swap);
}
}

你看,我添加了一个 printValues() 函数,可以更轻松地打印数组。请看一下这段代码并尝试理解它,如果您有疑问,您该怎么办! :D

关于java - 由于某种原因无法调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com