排序数组,重复的局部变量,错误
Sorting Arrays, duplicate local variable, error
我整天都在编写这段代码,但我似乎无法正确处理:(
import java.util.Random;
import java.util.Scanner;
import java.io.*;
public class TrialSixthree{
public static void main(String[]args){
Scanner i = new Scanner(System.in);
int c;
int choice;
Random t = new Random();
for (c = 1; c <= 5; c++) {
System.out.println(t.nextInt(1000));
}
System.out.println(" \n1: BUBBLE SORT ");
System.out.println(" 2: SELECTION SORT ");
System.out.println(" 3: QUICK SORT ");
System.out.println(" Choose a number from 1-3 ");
choice= i.nextInt();
if(choice == 1){
System.out.print("You chose BUBBLE sort!");
System.out.println(x[i]+"");//I don't know what's wrong in this line
for(int i = 0 ; i < x.length-1 ; i++){
for (int j = 0 ; j < x.length-1 ; j++){
if ( x[j] > x[j]){
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
} else if(choice == 2){
System.out.print("You chose SELECTION sort!");
System.out.println(x[i]+"");
int temp, min;
for(int i=0;i<x.length-1;i++) {
min = i;
for(int j=i+1;j<x.length;j++) {
if(x[min]>x[j]) {
min = j;
}
if(min!=i) {
temp = x[min];
x[min] = x[i];
x[i] = temp;
}
}
}
} else if(choice == 3){
System.out.println("You chose QUICK sort!");
System.out.println(x[i]+"");
int temp;
for(int i=0;i<x.length-1;i++) {
for(int j=i+1;j<x.length;j++) {
if(x[i]>x[j]) {
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
} else {
System.out.println("Not in the choices!");
}
}
}
我好像不太明白。我还是 java 的新手。有一个错误提示重复的局部变量。这是我的作业。请帮助我:(
您声明了两次 i
:
Scanner i = new Scanner(System.in);
和
for(int i = 0 ; i < x.length-1 ; i++)
我建议你给 Scanner
变量起一个更有意义的名字。
你多次使用了一个i
变量
所以重命名一个 i
变量
Scanner i=new Scanner(System.in);
重命名一个 i
变量
Scanner scan=new Scanner(System.in);
我整天都在编写这段代码,但我似乎无法正确处理:(
import java.util.Random;
import java.util.Scanner;
import java.io.*;
public class TrialSixthree{
public static void main(String[]args){
Scanner i = new Scanner(System.in);
int c;
int choice;
Random t = new Random();
for (c = 1; c <= 5; c++) {
System.out.println(t.nextInt(1000));
}
System.out.println(" \n1: BUBBLE SORT ");
System.out.println(" 2: SELECTION SORT ");
System.out.println(" 3: QUICK SORT ");
System.out.println(" Choose a number from 1-3 ");
choice= i.nextInt();
if(choice == 1){
System.out.print("You chose BUBBLE sort!");
System.out.println(x[i]+"");//I don't know what's wrong in this line
for(int i = 0 ; i < x.length-1 ; i++){
for (int j = 0 ; j < x.length-1 ; j++){
if ( x[j] > x[j]){
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
} else if(choice == 2){
System.out.print("You chose SELECTION sort!");
System.out.println(x[i]+"");
int temp, min;
for(int i=0;i<x.length-1;i++) {
min = i;
for(int j=i+1;j<x.length;j++) {
if(x[min]>x[j]) {
min = j;
}
if(min!=i) {
temp = x[min];
x[min] = x[i];
x[i] = temp;
}
}
}
} else if(choice == 3){
System.out.println("You chose QUICK sort!");
System.out.println(x[i]+"");
int temp;
for(int i=0;i<x.length-1;i++) {
for(int j=i+1;j<x.length;j++) {
if(x[i]>x[j]) {
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
} else {
System.out.println("Not in the choices!");
}
}
}
我好像不太明白。我还是 java 的新手。有一个错误提示重复的局部变量。这是我的作业。请帮助我:(
您声明了两次 i
:
Scanner i = new Scanner(System.in);
和
for(int i = 0 ; i < x.length-1 ; i++)
我建议你给 Scanner
变量起一个更有意义的名字。
你多次使用了一个i
变量
所以重命名一个 i
变量
Scanner i=new Scanner(System.in);
重命名一个 i
变量
Scanner scan=new Scanner(System.in);