比较扫描仪输入到阵列

时间:2016-11-11 02:29:22

标签: java arrays compare

思想: 用户输入'W','L'或'T'(胜利,亏损或平局)来下注。程序在这些参数中生成随机结果。打印用户输入和结果,并根据程序提供的正确投注显示分数。

我在如何继续将用户生成的扫描仪输入与产生随机结果的arraylist进行比较时遇到问题。

如果不是多个“问题”和“答案”,我可以使用(val.equals(input))排序。但是,每个单独的投注都是随机的,必须与用户投注相匹配才能总结用户得分,这会使其复杂化。 任何帮助表示赞赏。

public class test3 {
public static void main(String[] args) {

    int score = 0;

    System.out.println("Betting game initiating... \nType 'W' for win, 'L' for loss and 'T' for tie.");

    Scanner input = new Scanner(System.in);
    String array[] = new String[5];

    for (int i = 0; i < 5; i++)
    {
        System.out.println("Please enter your bet:");
        array[i]=input.nextLine();
    }

    List<String> list = new ArrayList<String>();
    list.add("w");
    list.add("l");
    list.add("t");

    System.out.println("This week wins, losses and ties loading...\n");
    System.out.println("Result:");

    test3 obj = new test3();
    for(int i = 0; i < 5; i++){
        System.out.print(obj.getRandomList(list) + " ");
    }

    System.out.println("\n\nYour bets were:");
    for (int i = 0; i < 5; i++){
        System.out.print(array[i] + " ");
    }

    System.out.println("\n\nYou were correct on: " + score + " bettings");


}

private Random random = new Random();

public String getRandomList(List<String> list) {
    int index = random.nextInt(list.size());
    return list.get(index);

}
}

2 个答案:

答案 0 :(得分:0)

为简单起见,我删除了test3,但基本上你需要在数组上保存结果并生成随机结果并保存它们(即列表)。然后你必须迭代并比较每个游戏结果,如果你的投注是正确的,只需加一个得分。检查以下代码:

主:

public static void main(String[] args) {

        int score = 0;
        String array[] = new String[5];
        List < String > randomResultList = new ArrayList<String> ( );


        System.out.println("Betting game initiating... \nType 'W' for win, 'L' for loss and 'T' for tie.");

        Scanner input = new Scanner(System.in);


        for (int i = 0; i < 5; i++)
        {
            System.out.println("Please enter your bet:");
            array[i]=input.nextLine();
        }

        System.out.println("This week wins, losses and ties loading...\n");
        System.out.println("Result:");

        for(int i = 0; i < 5; i++){
            String randomResult = getRandomList();
            System.out.print( randomResult + " ");
            randomResultList.add ( randomResult );
        }

        System.out.println("\n\nYour bets were:");

        for (int i = 0; i < 5; i++){
            System.out.print(array[i] + " ");
        }
        //here is where you compare each result
        for(int i = 0; i < 5; i++)
        {
            if( array[i].equals ( randomResultList.get ( i ) ))
            {
                score++;
            }
        }

        System.out.println("\n\nYou were correct on: " + score + " bettings");


    }

    private static Random random = new Random();

    public static String getRandomList() {
        List<String> list = Arrays.asList("w", "l", "t");
        int index = random.nextInt(list.size());
        return list.get(index);
    }

I / O示例:

  

投注游戏开始...

     

键入&#39; W&#39;为了胜利,&#39; L&#39;对于损失和&#39; T&#39;为了领带。

     

请输入您的投注:

     

瓦特

     

请输入您的投注:

     

瓦特

     

请输入您的投注:

     

瓦特

     

请输入您的投注:

     

瓦特

     

请输入您的投注:

     

瓦特

     

本周获胜,失利和关系加载......

     

结果:

     

l l l t w

     

你的赌注是:

      w w w w w

     

你是对的:1次投注

额外:

您可以在同一次迭代中完成所有操作!看看这个。

public static void main(String[] args) {

        int score = 0;
        // Win Loose or Tie
        List<String> list = Arrays.asList("w", "l", "t");
        Random rand = new Random ( );

        //String with result and bets i.e (wwwww) and (wlltw). This avoid another iteration.
        String result="";
        String bets = "";

        System.out.println("Betting game initiating... \nType 'W' for win, 'L' for loss and 'T' for tie.");

        Scanner input = new Scanner(System.in);


        for (int i = 0; i < 5; i++)
        {
            System.out.println("Please enter your bet:");
            String bet =input.nextLine();
            String randomResult = ( list.get ( rand.nextInt ( list.size ( ) ) ));

            //concatenate String with results and bets with +=
            result += randomResult;
            bets += bet;

            //check if u won
            if( bet.equals ( randomResult ))
            {
                score++;
            }
        }

        //This are just the results! no more iterations.

        System.out.println("This week wins, losses and ties loading...\n");

        System.out.println("Result:" + result);

        System.out.println("\n\nYour bets were:" + bets );

        System.out.println("\n\nYou were correct on: " + score + " bettings");


    }

答案 1 :(得分:0)

执行此操作的一种方法是在下面的代码中。

您基本上需要将输入的每个元素与随机列表的每个元素进行比较,以便运行循环。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Test {

  private Random random = new Random();

  public static void main(String[] args) {

    int score = 0;

    System.out
        .println("Betting game initiating... \nType 'W' for win, 'L' for loss and 'T' for tie.");

    Scanner input = new Scanner(System.in);
    String array[] = new String[5];

    for (int i = 0; i < 5; i++) {
      System.out.println("Please enter your bet:");
      array[i] = input.nextLine();
    }


    System.out.println("\n\nYour bets were:");
    for (int i = 0; i < 5; i++) {
      System.out.print(array[i] + " ");
    }

    List<String> list = new ArrayList<String>();
    list.add("w");
    list.add("l");
    list.add("t");

    System.out.println("This week wins, losses and ties loading...\n");
    System.out.println("Result:");

    Test obj = new Test();
    List<String> randList = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
      randList.add(obj.getRandomList(list));
    }
    for(String randBet : randList){
      System.out.print( randBet + " ");
    }



    System.out.println("");
    int counter = 0;


    for (String yourbet: Arrays.asList(array)){

      if(randList.get(counter).equals(yourbet)){
        score++;
      }
      counter++;
    }

    System.out.println("\n\nYou were correct on: " + score + " bettings");


  }

  public String getRandomList(List<String> list) {
    int index = random.nextInt(list.size());
    return list.get(index);

  }
}