【问题标题】:Compare scanner input to array将扫描仪输入与阵列进行比较
【发布时间】:2016-11-11 02:29:22
【问题描述】:

想法: 用户通过键入“W”、“L”或“T”(赢、输或平)输入他们的赌注。程序在这些参数内生成随机结果。用户输入和结果被打印出来,并根据正确的赌注给出分数,这是由程序提供的。

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

如果不是多个“问题”和“答案”,我可以使用(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);

}
}

【问题讨论】:

  • 你可以添加你的 test3 类吗?

标签: java arrays compare


【解决方案1】:

一种方法是在下面的代码中。

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

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

  }
}

【讨论】:

    【解决方案2】:

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

    主要:

    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 示例:

    投注游戏开始...

    输入“W”表示获胜,输入“L”表示失败,输入“T”表示平局。

    请输入您的赌注:

    w

    请输入您的赌注:

    w

    请输入您的赌注:

    w

    请输入您的赌注:

    w

    请输入您的赌注:

    w

    本周的胜负和平局加载...

    结果:

    l l l t w

    您的赌注是:

    wwwww

    您的投注正确: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");
    
    
        }
    

    【讨论】:

    • 效果很好。必须试着记住这一点。谢谢!
    • @Silsand 我添加了一个没有数组的额外解决方案,如果您想查看它,只需进行一次迭代。我很乐意提供帮助 :) 如果您对我的回答有任何疑问,请不要犹豫。我会上网。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多