【问题标题】:How to split an input of numbers (student ID) from user to implement into formula如何将用户输入的数字(学生 ID)拆分为公式
【发布时间】:2018-12-16 16:10:52
【问题描述】:

我在使用分配给我的这个初学者 java 程序时遇到了麻烦,我对 java 完全陌生,而且我在使用这个特定程序时遇到了很多麻烦。这些是说明:

您的程序应提示用户输入一个 n 位数字作为学生 ID,然后显示 输入的 ID 的有效性。您可以假设 n 将在 6 和 10 的范围内。校验和 数字是学生证的一部分。您的算法是将输入的 ID 中最右边的数字与 计算校验和数字。如果没有匹配,那么您的程序将报告输入的 ID 为 无效的。 例如,输入的 ID 1234567 无效,因为计算的第 7 位是 1 (= (1 * (digit1) + 2 * (digit 2) + 3 * (digit 3) + 4 * (digit 4) + 5 * (digit 5) + 6 * (digit 6)) % 10) 与实际的第 7 位不同,即 7. 但是,如果输入的 ID 是 1234561,您的程序将显示接受消息。

我尝试做的第一步是从用户输入中读取每个没有空格的数字,就好像它有空格一样。然后我试图将这些数字中的每一个分配给数字 1、数字 2 等变量来计算。

import java.util.Scanner;
public class H4_Ruby{
public static void main(String[] args){
// this code scans the the user input 
  Scanner scan = new Scanner(System.in);
  System.out.println("Please enter a student ID between 6-10 digits");
  String nums = scan.nextLine(); 
  String[] studentIdArray = nums.split(" ");
  int[] studentID = new int [studentIdArray.length];



  for (int i = 0; i < studentIdArray.length; i++) 
  { 
     studentID[i]= Integer.parseInt(studentIdArray[i]);
     System.out.print(studentID[i] + ",");
  }
}

到目前为止,这是我的代码......

【问题讨论】:

  • 我对整个空间的事情感到非常困惑。他们输入的号码有空格吗?您的教授给出的描述似乎表明它只是一系列没有空格的数字。准确吗?
  • 这将是用户的输入,没有空格
  • 好的,这更有意义。我想我有一个答案。我只是想在发布之前在脑海中考虑一下。
  • 我可能完全错了,但我觉得完成这个程序最有效的方法是将“学生证”拆分为单独的数字,将这些数字中的每一个设置为一个变量,然后将它们插入进入方程

标签: java for-loop while-loop


【解决方案1】:

您不能通过nums.split(" ") 拆分来获得String 数组。你已经从用户那里得到了一个String id,没有空格。

所以迭代通过String并保持计算乘数的产品总和,如下所示。我在代码本身中添加了 cmets

public static void main(String[] args) {
    // this code scans the the user input
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter a student ID between 6-10 digits");
    String nums = scan.nextLine();

    int multiplier = 1, totalProductSum = 0;
    for (int i = 0; i < nums.length() - 1; i++) {

        // if the character is not digit, its not a valid id
        if (!Character.isDigit(nums.charAt(i))) {
            System.out.println("Invaild Id");
            return;
        }

        // maintain the sum of products and increment the multiplier everytime
        totalProductSum += (multiplier * (nums.charAt(i) - '0'));
        multiplier++;
    }

    // the condition for validity i.e totalProduct sum mod 10 and last digit in input Id
    if ((totalProductSum % 10) == (nums.charAt(nums.length() - 1) - '0'))
        System.out.println("Valid Id");
    else
        System.out.println("Invaild Id");
}

【讨论】:

  • 这几乎正是我要建议的,Quin。不过,老实说,这段代码比我的要优雅得多。不错,Shanu。
  • 你能解释一下第一个if语句之后的这部分代码吗
  • (nums.charAt(i) - '0'));
  • 特别是 - '0' 部分
  • @Quin 这用于将字符转换为整数。另见stackoverflow.com/a/46343671/3694269
【解决方案2】:

在这里,你也可以使用这个:

final class H4_Ruby {
  public static void main(final String... args) {
    System.out.printf("%s student ID%n", (H4_Ruby.isValid(H4_Ruby.read())) ? "Valid" : "Invalid");
  }

  private static String read() {
    final Scanner scan = new Scanner(System.in);
    // [start] do-while here for input validation: length and all characters must be digits
    System.out.print("Please, enter a student ID between 6-10 digits: ");
    final String input = scan.nextLine();
    // [end] do-while here for input validation: length and all characters must be digits
    return input;
  }

  private static boolean isValid(final CharSequence studentId) {
    int partial = 0;
    for (int i = 0; i < (studentId.length() - 1); i++) {
      partial += ((i + 1) * Character.getNumericValue(studentId.charAt(i)));
    }
    return (partial % 10) == Character.getNumericValue(studentId.charAt(studentId.length() - 1));
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多