为什么我的数组会被排序?

时间:2018-04-29 23:12:00

标签: java arrays sorting methods scope

您好,感谢所有

信息:

此程序适用于课程作业

分配是使用数组来模拟彩票

我使用一维的整数数组来表示彩票上的数字

我指定的方法是指定的,所以我不能将任何fxn从一种方法移动到另一种方法(例如:我无法确定用户是否在主方法中而不是在指定方式中赢得了抽奖方法)

问题:

我得到的 - 显示方法显示我的用户输入的数组从最小到最大

排序

我想要的 - 用户输入的原始数组

我的代码:

import java.util.Scanner;
import java.util.Random;
public class Program2 {
  /*notes
  the numbers do not have to match in order
  you must validate all user input
  numeric output should be displayed with commas
  the code should be written using efficient processing
  use comments, naming conventions, and program structure
  */
  //main method
  public static void main(String[] args) {
    Scanner kbr = new Scanner(System.in); //scanner object for user input
    boolean auto = false;
    int choice;
    int[] yourTix;
    int[] winningTix;
    int tries = 0;
    int[] freq = new int[20];

    //display lottery info
    System.out.println("Welcome to the Wacky Lottery Program!");
    System.out.println("1 - Pick my own");
    System.out.println("2 - Computer picks for me");

    //validation loop
    do {
      choice = kbr.nextInt();

      if (choice < 1 || choice > 2)
        System.out.println("Invalid input. please enter either 1 or 2");

    } while (choice < 1 || choice > 2);

    //selecting automatic choice or not
    if (choice == 2)
      auto = false;
    else
      auto = true;

    //generating your ticket
    yourTix = yourTix(auto);

    //this do/while loop keeps generating winning tickets until you win
    do {
      //get new winning ticket
      winningTix = winningTix();

      //check frequency of each number and add to the frequency accumulator array
      for (int i = 0; i < winningTix.length; i++) {
        //accumulate frequency @ index that is equal to winningTix[i]
        int num = winningTix[i] - 1;
        freq[num]++;
      }

      //accumulating the number of tries
      tries++;
    } while (win(yourTix, winningTix) == false);

    //using the display method to print out the stats of this lottery
    display(yourTix, winningTix, tries, freq);


  }

  //method that determines the user's ticket numbers
  public static int[] yourTix(boolean auto) {
    Random r = new Random();
    Scanner kbr = new Scanner(System.in); //scanner object for user input
    int[] tix = new int[5]; //creating a new array of integers with 5 integers

    //this loop gets the user's input for each number on the ticket
    for (int i = 0; i < tix.length; i++) {
      if (auto == true) {
        System.out.print("Please enter number " + (i + 1) + ": ");
        do {
          tix[i] = kbr.nextInt();
          if (tix[i] < 1 || tix[i] > 20)
            System.out.println("Invalid input. Please only enter 1-20");
        } while (tix[i] < 1 || tix[i] > 20);
      } else {
        tix[i] = r.nextInt(19) + 1;
      }
    }

    //return the ticket array
    return tix;

  }

  //method to determine the winning numbers
  public static int[] winningTix() {
    Random r = new Random();
    int[] winningTix = new int[5];

    for (int i = 0; i < winningTix.length; i++)
      winningTix[i] = r.nextInt(20) + 1;

    return winningTix;
  }

  //method to determine if the user won
  public static boolean win(int[] yt, int[] wt) {
    boolean same = true;
    int startScan, index, minIndex, minValue;


    //sort user's array numerical order
    for (startScan = 0; startScan < (yt.length - 1); startScan++) {
      minIndex = startScan;
      minValue = yt[startScan];
      for (index = startScan + 1; index < yt.length; index++) {
        if (yt[index] < minValue) {
          minValue = yt[index];
          minIndex = index;
        }
      }
      yt[minIndex] = yt[startScan];
      yt[startScan] = minValue;
    }
    //sort computer's array numerical order
    for (startScan = 0; startScan < (wt.length - 1); startScan++) {
      minIndex = startScan;
      minValue = wt[startScan];
      for (index = startScan + 1; index < wt.length; index++) {
        if (wt[index] < minValue) {
          minValue = wt[index];
          minIndex = index;
        }
      }
      wt[minIndex] = wt[startScan];
      wt[startScan] = minValue;
    }


    index = 0; // Loop control variable

    // determine whether the elements contain the same data
    while (same && index < wt.length) {
      if (yt[index] != wt[index])
        same = false;
      index++;
    }

    //return whether or not the winning array and the user's array matches
    return same;
  }



  //method that displays and writes to file
  public static void display(int[] winning, int[] user, int tries, int[] freq) {
    //message indicating the win
    System.out.println("You won!");
    System.out.println();

    //display winning lottery ticket numbers in their original order
    System.out.println("Here is the winning ticket");
    for (int i = 0; i < winning.length; i++)
      System.out.println(winning[i]);
    System.out.println();

    //display user numbers in the original order
    System.out.println("Here is your ticket");
    for (int i = 0; i < user.length; i++)
      System.out.println(user[i]);
    System.out.println();

    //the number of times it took to get a match with a winning ticket
    System.out.printf("It took %,d tries to win", tries);
    System.out.println();

    //the number of times each value 1-20 was generated for a winning lottery ticket
    for (int i = 0; i < freq.length; i++) {
      System.out.printf("The number %d was generated %,d times", i + 1, freq[i]);
      System.out.println();
    }

    //this should also be written to a file

    //this should also be written to a file
    //note: if tie, display all the numbers

  }
}

2 个答案:

答案 0 :(得分:0)

在你的方法中:

public static boolean win(int [] yt,int [] wt){

您正在对阵列进行排序:

//sort user's array numerical order
    for (startScan = 0; startScan < (yt.length - 1); startScan++) {
      minIndex = startScan;
      minValue = yt[startScan];
      for (index = startScan + 1; index < yt.length; index++) {
        if (yt[index] < minValue) {
          minValue = yt[index];
          minIndex = index;
        }
      }
      yt[minIndex] = yt[startScan];
      yt[startScan] = minValue;
    }
    //sort computer's array numerical order
    for (startScan = 0; startScan < (wt.length - 1); startScan++) {
      minIndex = startScan;
      minValue = wt[startScan];
      for (index = startScan + 1; index < wt.length; index++) {
        if (wt[index] < minValue) {
          minValue = wt[index];
          minIndex = index;
        }
      } 

这就是为什么它会对数组进行排序。

希望有所帮助:)

答案 1 :(得分:0)

您正在使用public static boolean win(int[] yt, int[] wt) { yt = yt.clone(); // take a copy first wt = wt.clone(); // take a copy first 方法对数组进行排序。如果您不想对原始数组进行排序,并且您只是为了进行比较,请尝试对副本进行排序。

您可以添加这些行,以便只对副本进行操作。

public static int[] copy(int[] array) {
    int[] copy = new int[array.length];
    for (int i = 0; i < array.length; i++)
        copy[i] = array[i];
    return copy;
}

public static boolean win(int[] yt, int[] wt) {
    yt = copy(yt); // take a copy first
    wt = copy(wt); // take a copy first

使用您已经使用的代码,您可以编写

$con = mysqli_connect("localhost", "root", "") 

mysqli_select_db($con,"online_ticket");