首页 > 解决方案 > 为什么我的数组得到排序?

问题描述

你好,谢谢大家

信息:

该程序用于课堂作业

作业是用一个数组来模拟彩票

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

我使用的方法是指定的,所以我不能将任何 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

  }
}

标签: javaarrayssortingmethodsscope

解决方案


在你的方法中:

公共静态布尔赢(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;
        }
      } 

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

希望能帮助到你 :)


推荐阅读