简体   繁体   中英

Compare scanner input to array

Idea: User inputs their bets by typing either 'W','L' or 'T' (wins, losses or tie). 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. 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). 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:

Betting game initiating...

Type 'W' for win, 'L' for loss and 'T' for tie.

Please enter your bet:

w

Please enter your bet:

w

Please enter your bet:

w

Please enter your bet:

w

Please enter your bet:

w

This week wins, losses and ties loading...

Result:

llltw

Your bets were:

wwwww

You were correct on: 1 bettings

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

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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