简体   繁体   English

多选项字符串颜色输入验证?

[英]Multi option string color input validation?

I'm struggling with a program that lets a user chose between two color by typing either the full color out (not case sensitive ) or a char that is the first letter of the color (not case sensitive), depending on what color they type it will automatically assign the other to a different variable. 我在一个程序上苦苦挣扎,该程序允许用户通过键入完整的颜色(不区分大小写)或作为颜色的首字母的char(不区分大小写)来选择两种颜色,具体取决于他们键入的颜色它将自动将另一个分配给另一个变量。 My two options are blue and green, blue seems to be working fine but when I input green or g the method keeps asking me for a new input. 我的两个选项分别是蓝色和绿色,蓝色似乎工作正常,但是当我输入绿色或g时,该方法不断要求我输入新的内容。 Here is a snip of my program that deals with the color assignment. 这是我的程序中处理颜色分配的片段。

import java.util.*;
public class Test{
  public static Scanner in = new Scanner (System.in);
  public static void main(String []args){

    System.out.println("Chose and enter one of the following colors (green or blue): ");
    String color = in.next();
    boolean b = false;
    while(!b){
      if(matchesChoice(color, "blue")){
        String circle = "blue";
        String walk = "green";
        b = true;
      }
      else if(matchesChoice(color, "green")){
        String circle = "green";
        String walk = "blue";
        b = true;
      }
    }     

  }
  public static boolean matchesChoice(String color, String choice){
    String a= color;
    String c = choice;
    boolean b =false;
    while(!a.equalsIgnoreCase(c.substring(0,1)) && !a.equalsIgnoreCase(c)){
      System.out.println("Invalid. Please pick green or blue: ");
      a = in.next();
    }
    b = true;
    return b;

  }

}

I'm basically creating a while loop that insures the user selects one of the color choices and a method to determine whether a String input by the user matches a String option for the question. 我基本上是在创建一个while循环,以确保用户选择一种颜色选择以及一种确定用户输入的String是否与问题的String选项匹配的方法。

The else if(matchesChoice(color, "green")) is unreachable. else if(matchesChoice(color, "green"))不可访问。 The matchesChoice(color, "blue") method is being called when you enter g or green , so it's always comparing it against b or blue . 输入ggreen ,将调用matchesChoice(color, "blue")方法,因此它始终将其与bblue进行比较。 Then within that method, it continues to loop because you keep entering g or green . 然后在该方法中,由于您继续输入ggreen ,它继续循环。

Just have matchesChoice return true or false if color matches choice : 只是让matchsChoice如果color匹配choice则返回truefalse

public static boolean matchesChoice(String color, String choice){
    String a= color;
    String c = choice;
    if (a.equalsIgnoreCase(c.substring(0,1)) || a.equalsIgnoreCase(c)) {
        return true;
    }
    return false;
}

Then add the scan for user input inside of the while loop in main: 然后在main的while循环内添加对用户输入的扫描:

boolean b = false;
System.out.println("Chose and enter one of the following colors (green or blue): ");
while(!b){
    String color = in.next();
    if(matchesChoice(color, "blue")){
        String circle = "blue";
        String walk = "green";
        b = true;
    }
    else if(matchesChoice(color, "green")){
        String circle = "green";
        String walk = "blue";
        b = true;
    }
    else {
        System.out.println("Invalid. Please pick green or blue: ");
    }
}

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

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