Java 为什么main不能接收我返回的int值
因此,当我编译这个Eclipse时,Insertionct和Shakerct的数字是0,并打印出一个平局。我知道这两种方法都正确排序,但出于某种原因,它不会返回它们对main进行的比较量,以决定哪一种方法对数组排序更快。提前谢谢你的帮助Java 为什么main不能接收我返回的int值,java,return,Java,Return,因此,当我编译这个Eclipse时,Insertionct和Shakerct的数字是0,并打印出一个平局。我知道这两种方法都正确排序,但出于某种原因,它不会返回它们对main进行的比较量,以决定哪一种方法对数组排序更快。提前谢谢你的帮助 import java.io.*; import java.util.*; public class Sorts { private static Scanner in; public static void main(String[] a
import java.io.*;
import java.util.*;
public class Sorts {
private static Scanner in;
public static void main(String[] args) throws Exception {
in = new Scanner(System.in);
System.out.print("How many strings will you be entering? ");
int sz = Integer.parseInt (in.nextLine());
String[] A = new String[sz];
String[] B = new String[sz];
for (int i = 0; i < sz; i++){
System.out.print ("Enter String #"+(i+1)+": ");
A[i] = in.nextLine();// sets the array at i equal to a string
B[i] = A[i]; // sets array B to the same as array A so I can use it in the shaker sort method
}
int Insertionct = 0;
int Shakerct = 0;
System.out.println(Insertionct);
System.out.println(Shakerct);
if (Shakerct > Insertionct) {
System.out.println("Insertion Sort was faster!");
} else if (Shakerct < Insertionct) {
System.out.println("Shaker Sort was faster!");
} else {
System.out.println("It was a tie");
}
}
public static int InsertionSort (int Insertionct, String[] A) throws Exception { //sorts the array of strings with the insertion sort.
// initializes the count variable
int sz = A.length; // sets size equal toe array A
for (int i = 0; i < sz-1; i++)
for (int j = i; j >= 0 && A[j].compareTo (A[j+1]) > 0; j--) {
Insertionct++;
String t = A[j]; //switch A[j], A[j+1]
A[j] = A[j+1];
A[j+1] = t;
}
return Insertionct;
}
public static int ShakerSort (int Shakerct, String[] B) throws Exception {//Uses the ShakerSort in order to order the array.
int sz = B.length;
for (int i = 0; i < sz; i++){
int nsct = 0;
for(int j = nsct+sz-1; j > i; j--){//runs through the array backwards and then swaps if it needs to
Shakerct++;
if (B[j].compareTo(B[j-1]) < 0) {
nsct = 0;
String t = B[j];
B[j] = B[j-1];
B[j-1] = t;
} else {
nsct++; // if no swap happens it increases no swap to increment the starting points.
}
}
for (int j = nsct; j > sz-i-1; j++){
if (B[j].compareTo(B[j+1]) > 0){//runs through the array going forward swaps if needed
Shakerct++;
nsct = 0;
String t = B[j];
B[j] = B[j+1];
B[j+1] = t;
} else {
nsct++;// increases no-swap count if no swap happens and changes the starting point.
}
}
}
return Shakerct;
}
}
import java.io.*;
导入java.util.*;
公共类分类{
专用静态扫描仪;
公共静态void main(字符串[]args)引发异常{
in=新扫描仪(System.in);
System.out.print(“您将输入多少字符串?”);
int sz=Integer.parseInt(in.nextLine());
字符串[]A=新字符串[sz];
字符串[]B=新字符串[sz];
对于(int i=0;iInsertionct){
System.out.println(“插入排序更快!”);
}否则如果(Shakerct=0&&A[j]。比较(A[j+1])>0;j--){
Insertionct++;
字符串t=A[j];//开关A[j],A[j+1]
A[j]=A[j+1];
A[j+1]=t;
}
返回Insertionct;
}
公共静态int-ShakerSort(int-Shakerct,String[]B)引发异常{//使用ShakerSort对数组进行排序。
int sz=B.长度;
对于(int i=0;ii;j--){//向后遍历数组,然后在需要时交换
Shakerct++;
如果(B[j]),比较(B[j-1])<0{
nsct=0;
字符串t=B[j];
B[j]=B[j-1];
B[j-1]=t;
}否则{
nsct++;//如果未发生交换,则增加no swap以增加起点。
}
}
对于(int j=nsct;j>sz-i-1;j++){
如果(B[j].compareTo(B[j+1])>0{//在需要时通过数组进行正向交换
Shakerct++;
nsct=0;
字符串t=B[j];
B[j]=B[j+1];
B[j+1]=t;
}否则{
nsct++;//如果不发生交换,则增加不交换计数并更改起点。
}
}
}
返回振动筛RCT;
}
}
您不在main
中调用InsertionSort
和ShakerSort
您不在main
中调用InsertionSort
和ShakerSort