简体   繁体   English

如何针对两个数组验证Java中的用户输入?

[英]How do I validate user input in java against two arrays?

My task is to code a small program that would calculate an administrator access code that changes daily based on a predefined algorithm (month * day * constant). 我的任务是编写一个小程序,该程序将计算管理员访问代码,该访问代码根据预定义的算法(月*天*常数)每天更改。

I have the gist of the code written for my main method with the basic functionality pulling from java.time.LocalDate & format.DateTimeFormatter and then parsing the string into an integer and then calculating and displaying the results. 我有为我的主要方法编写的代码要点,其基本功能是从java.time.LocalDate&format.DateTimeFormatter中提取,然后将字符串解析为整数,然后计算并显示结果。

String month = DateTimeFormatter.ofPattern("MM").format(localDate);
String date = DateTimeFormatter.ofPattern("dd").format(localDate);
int monthResult = Integer.parseInt(month);
int dateResult = Integer.parseInt(date);
int adminAccess = monthResult * dateResult * seed;
System.out.println("Admin Passcode is: " + adminAccess);

But now I want to take this to the next level and incorporate an option to calculate the access code manually via user input. 但是现在我想将其带入一个新的水平,并包含一个选项,可以通过用户输入手动计算访问代码。

I want to validate the user input and allow numeric or string input before taking the input and assigning the correct integer representation based on their input. 我想验证用户输入并允许数字或字符串输入,然后再接受输入并根据他们的输入分配正确的整数表示形式。 I'm looking to ultimately do something along these lines (Anticipated Program Flow) 1 . 我期待最终按照这些思路做一些事情(预期程序流) 1 I'm not sure if I'm just too far above my head, but I can't quite wrap my mind around what functionality I should use a For, Do While or a Switch to process the validation. 我不确定是否离我太远了,但是我不能完全确定应该使用For,Do While还是Switch处理验证的功能。 All I have right now is: 我现在所拥有的是:

class userManual {
            public void run() {
                String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
                String [] numMonths = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
                Scanner scan = new Scanner(System.in);
                String manual = scan.next();
                for () {}
            }
        }

Any ideas would be greatly appreciated!! 任何想法将不胜感激!

try the following. 尝试以下方法。 I haven't optimized it but put it together quick and dirty to help you with the idea. 我尚未对其进行优化,但可以快速而又轻松地将其组合在一起,以帮助您实现这一想法。 Make sure to understand the concept rather than just copy-pasting code. 确保了解概念,而不仅仅是复制粘贴代码。 Hope this helps! 希望这可以帮助!

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

class UserManual {

  public static void main(String[] args) {
    Map<String, Integer> months = new HashMap<String, Integer>();
    months.put("January", 1);
    months.put("February", 2);
    months.put("March", 3);
    months.put("April", 4);
    months.put("May", 5);
    months.put("June", 6);
    months.put("July", 7);
    months.put("August", 8);
    months.put("September", 9);
    months.put("October", 10);
    months.put("November", 11);
    months.put("December", 12);
    Scanner scan = new Scanner(System.in);
    String manual = scan.next();
    String strMonth = "";
    if (months.containsKey(manual)) {
      strMonth = manual;
    } else if (months.containsValue(Integer.valueOf(manual))) {
      for (Entry<String, Integer> entry : months.entrySet()) {
        if (entry.getValue().equals(Integer.valueOf(manual))) {
          strMonth = entry.getKey();
        }
      }
    }
    if (!strMonth.equals("")) {
      System.out.println("Valid Month --> " + strMonth);
    } else {
      System.out.println("Input is invalid!");
    }
    scan.close();
  }
}

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

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