【问题标题】:How to make while loop check if there are 16 digits in a string如何使while循环检查字符串中是否有16位数字
【发布时间】:2020-06-02 00:49:53
【问题描述】:

如何使循环检查字符串中是否有 16 位数字,如果不足则重置字符串。我正在尝试制作一个计算校验位的信用卡程序。我有其他一切工作我只是无法让程序检查用户输入字符串中的位数。感谢所有帮助!

import java.util.Scanner;

public class LuhnAlgorithm {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number credit card number (Enter a blank line to quit: ");
        String nums = input.nextLine();

        int i = 0;
        char chk = nums.charAt(15);

        while(!nums .equals("") ) {

            if (nums.length()<16 || nums.length() > 15){ //How do I get this line to reset the while loop?
                System.out.println("ERROR! Number MUST have exactly 16 digits.");
            }

            int sum = 0;
            for( i = 0; i < 15; i++) {  
                char numc = nums.charAt(i);
                int num = Character.getNumericValue(numc);
                if ( i % 2 == 0 ) {
                    num = num * 2;
                    if ( num >= 10) {
                        num = num - 9;
                    }
                }
                sum = num + sum;
            }

            int sum2 = sum % 10;
            if (sum2 > 0) {
                sum2 = 10 - sum2;
            }
            int chk2 = Character.getNumericValue(chk);
            System.out.println("The check digit should be: " + sum2);
            System.out.println("The check digit is: " + chk);
            if ( sum2 == chk2) {
                System.out.println("Number is valid.");
            }
            else {
                System.out.println("Number is not valid. ");
            }

            System.out.print("Enter a number credit card number (Enter a blank line to quit:) ");
            nums = input.nextLine();
        }

        System.out.println("Goodbye!"); 
        input.close();
    }
}

【问题讨论】:

  • 为什么?为什么不直接测试nums.length() == 16
  • 对不起,我是编码新手。你的意思是在while循环中? while(nums.length() == 16 + !nums .equals(""))
  • “重置 while 循环”是什么意思?
  • 如果某个东西的长度是 16,你已经知道它不是 ""
  • 我需要它打印出错误消息,然后继续运行循环,直到它获得准确的位数。在这种情况下,所需的位数是 16。

标签: java string while-loop string-length


【解决方案1】:

您可以在 if 语句中包含您只希望在 length ==16 时完成的代码。

意思,而不是:

if (nums.length != 16) {
    //code if there is an error
}
//code if there is no error

你可以这样做:

if (nums.length == 16) {
    //code if there is no error
} else {
    //code if there is an error
}

(我还想指出,您在 while 循环之前设置了 chk = nums.charAt(15),但您不会在下次用户输入新的信用卡号时在 while 循环中重置它。)

【讨论】:

  • 感谢您抽出宝贵时间帮助我。
【解决方案2】:

除了扫描仪本身之外,您可以将提示和所有初始化都带入 while 循环。然后如果他们说“”,break 退出循环。如果他们说的数字太短或太长,请说 continue 以返回提示。 因此:

    import java.util.Scanner;
public class main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.print("Enter a number credit card number (Enter a blank line to quit: ");
            String nums = input.nextLine().trim();
            if (nums.length() == 0) {
                break; //exits while loop
            }
            if (nums.length() != 16) { //How do I get this line to reset the while loop?
                System.out.println("ERROR! Number MUST have exactly 16 digits.");
                continue; //goes back to the beginning right away
            }
            //still here, process the number
            char chk = nums.charAt(15);
            int sum = 0;
            for (int i = 0; i < 15; i++) {
                char numc = nums.charAt(i);
                int num = Character.getNumericValue(numc);
                if (i % 2 == 0) {
                    num = num * 2;
                    if (num >= 10) {
                        num = num - 9;
                    }
                }

                sum = num + sum;
            }

            int sum2 = sum % 10;
            if (sum2 > 0) {
                sum2 = 10 - sum2;
            }
            int chk2 = Character.getNumericValue(chk);
            System.out.println("The check digit should be: " + sum2);
            System.out.println("The check digit is: " + chk);
            if (sum2 == chk2) {
                System.out.println("Number is valid.");
            } else {
                System.out.println("Number is not valid. ");
            }





        }


        System.out.println("Goodbye!");


        input.close();
    }

}

【讨论】:

  • 感谢您花时间帮助我编写代码。我最终通过添加一个 if 语句来修复它,并让 else 运行其余的代码。我不知道“中断”和“继续”我开始查找它们并将在未来的代码中使用它们。感谢您的信息!
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 2012-08-16
  • 2021-04-18
  • 2018-05-16
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 2017-08-27
相关资源
最近更新 更多