-1

私は一日中このコードに取り組んでいますが、正しく理解できないようです:(

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に慣れていません。ローカル変数が重複しているというエラーが表示されます。それは私の宿題です。私を助けてください :(

4

2 に答える 2

1

あなたはi2回宣言しました:

Scanner i = new Scanner(System.in);

for(int i = 0 ; i < x.length-1 ; i++)

Scanner変数にもっと意味のある名前を付けることをお勧めします。

于 2015-03-07T13:27:43.257 に答える
0

i変数を複数回使用しているため、i変数の名前を変更します

       Scanner i=new Scanner(System.in);

i変数の名前を変更する

       Scanner scan=new Scanner(System.in);  
于 2015-03-07T13:31:06.753 に答える