简体   繁体   English

比较扫描仪输入到阵列

[英]Compare scanner input to array

Idea: User inputs their bets by typing either 'W','L' or 'T' (wins, losses or tie). 提示:用户输入“ W”,“ L”或“ T”(赢,输或平局)输入赌注。 Program generates random results within these parameters. 程序在这些参数内生成随机结果。 User input and result gets printed, and a score is presented based on correct bets, which is supplied by the program. 打印用户输入和结果,并根据程序提供的正确下注显示分数。

Im having issues on how to proceed with comparing user generated input from scanner to an arraylist that produces a random result. 我在如何继续比较用户从扫描仪生成的输入到产生随机结果的数组列表方面遇到问题。

If it were not for the multiple "questions" and "answers" I could use a (val.equals(input)) of sort. 如果不是多个“问题”和“答案”,则可以使用(val.equals(input))排序。 However, each individual bet is random and must be matched against the users bets to sum up the users score, that complicates it. 但是,每个单独的下注都是随机的,必须与用户下注相匹配才能总结出用户得分,这使情况变得复杂。 Any help appreciated. 任何帮助表示赞赏。

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);

}
}

I removed test3 for simplicity, but basically you need save results on array and generate random results and saving them (ie a list). 为了简单起见,我删除了test3,但是基本上您需要将结果保存在数组上并生成随机结果并将其保存(即列表)。 Then you have to iterate through and compare each game result, and if your bet is correct, just add one to score. 然后,您必须迭代并比较每个游戏的结果,如果您的投注正确,则只需加一个即可得分。 Check the code below: 检查以下代码:

Main: 主要:

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 Example: I / O示例:

Betting game initiating... 博彩游戏启动...

Type 'W' for win, 'L' for loss and 'T' for tie. 输入“ W”代表胜利,输入“ L”代表损失,输入“ T”代表并列。

Please enter your bet: 请输入您的赌注:

w w ^

Please enter your bet: 请输入您的赌注:

w w ^

Please enter your bet: 请输入您的赌注:

w w ^

Please enter your bet: 请输入您的赌注:

w w ^

Please enter your bet: 请输入您的赌注:

w w ^

This week wins, losses and ties loading... 本周获胜,失利和联系...

Result: 结果:

llltw ll

Your bets were: 您的赌注是:

wwwww wwwww

You were correct on: 1 bettings 您在以下方面是正确的:1下注

Extra: 额外:

You could do all on the same iteration! 您可以在同一迭代上完成所有工作! check this out. 看一下这个。

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");


    }

One way to do this is in the code below. 一种方法是在下面的代码中。

You basically need to compare each element of your input with each element of the random list so run loop. 基本上,您需要将输入的每个元素与随机列表的每个元素进行比较,以便运行循环。

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);

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM