Java 如何获取int值并返回带星号的int值

Java 如何获取int值并返回带星号的int值,java,Java,Java新手,想知道如何获取整数并将其前12个数字输出为*。 我的日常Java挑战是: “编写一个接受信用卡号的程序,只显示最后四个字符。卡号的其余部分必须替换为***s。” 我从一个简单的方法开始,输出int本身 import java.util.Scanner; public class creditHide { public static void main(String[] args) { // Defines the cc number as

Java新手,想知道如何获取整数并将其前12个数字输出为*。 我的日常Java挑战是: “编写一个接受信用卡号的程序,只显示最后四个字符。卡号的其余部分必须替换为***s。”

我从一个简单的方法开始,输出int本身

import java.util.Scanner;

public class creditHide {
    public static void main(String[] args)
    {
            // Defines the cc number as an integer...
        int cCNumber;
            // Defines Scanner for the users' input...
        Scanner scanner = new Scanner(System.in);

            // Prints a prompt for the users' cCNumber...
        System.out.print("CREDIT CARD: ");
            // Assigns the next input as the users' cCNumber...
        cCNumber = scanner.nextInt();

        // (Here I would want a way of hiding the first 12 numbers)

            // Test to make sure it reads correctly...
        System.out.println(cCNumber);
    }
}
我想我必须重新考虑我最初的做法,接受任何解决方案。 谢谢:)


Sleep

这里有一种方法,可以使用正则表达式检查输入的有效性,如果提供的卡号有效,则隐藏前12位数字:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;

class creditHide {

    private static Pattern CC_PATTERN = Pattern.compile("\\d{4} \\d{4} \\d{4} (\\d{4})");

    public static void main(String[] args)
    {
        // Defines the cc number as an integer...
        String cCNumber;

        // Defines Scanner for the users' input...
        Scanner scanner = new Scanner(System.in);

        // Prints a prompt for the users' cCNumber...
        System.out.print("CREDIT CARD: ");

        // Assigns the next input as the users' cCNumber...
        cCNumber = scanner.nextLine();

        // Check for a valid credit card number, and hide first 12 digits of credit card number
        Matcher m = CC_PATTERN.matcher(cCNumber.strip());
        if (m.matches())
            cCNumber = String.format("**** **** **** %s", m.group(1));
        else
            // TODO: Do something more rigorous when an invalid card is entered
            cCNumber = "invalid";

        // Test to make sure it reads correctly...
        System.out.println(cCNumber);
    }
}

利用所有的建议,制定了一个比较好的解决方案,非常感谢大家

以下是解决方案:

import java.util.Scanner;                                                                               // Imports Scanner utilization
public class creditHide {
    public static void main(String[] args)
    {
        String cCNumber;                                                                                
        Scanner scanner = new Scanner(System.in;                                                        
        System.out.print("CREDIT CARD: ");                                                                  
        cCNumber = scanner.next();                                                                      
        if(cCNumber.length()>16)
        {
            System.out.println("ERROR 001, INVALID: TOO MANY VALUES, Please insert 16 values.");
        }
        else if(cCNumber.length()<16)
        {
            System.out.println("ERROR 002, INVALID: NOT ENOUGH VALUES, Please insert 16 values.");
        }
        else if(cCNumber.length() == 16)
        {
            String cCNumber2 = cCNumber.replace(cCNumber, "************" + cCNumber.substring(12)); characters
            System.out.println(cCNumber2);                                                              
        }                                                               
        else
        {
            System.out.println("ERROR 003, INVALID: INVALID VALUES, Please insert 16 values.");
        }
    }
}
import java.util.Scanner;//导入扫描仪利用率
公营信用卡{
公共静态void main(字符串[]args)
{
字符串编号;
扫描仪=新扫描仪(System.in;
系统输出打印(“信用卡”);
cCNumber=scanner.next();
如果(cCNumber.length()>16)
{
System.out.println(“错误001,无效:值太多,请插入16个值”);
}
else if(cCNumber.length()
import java.util.Scanner;
公营信用卡{
公共静态void main(字符串[]args){
长卡号;
扫描仪扫描=新扫描仪(System.in);
cardNumber=scan.nextLong();
int a=(int)(卡号%10000);
系统输出打印项次(“卡号******”+a);
}
}

再次阅读要求,看看结果会是什么样子。如何达到要求并不重要:如果您需要的是一个最终结果,它由12个星号组成,后跟输入的最后四个“字母”。只需创建一个新字符串。一个小提示:因为它被调用“信用卡号码"这并不意味着您必须将其视为代码中的一个数字。除非要求说明您必须使用int/long,否则您可能希望将信用卡号输入作为一个简单的字符串。或者,如果您希望使用数字数据类型,您至少应该检查int的最大值,并考虑如何将16位信用卡编号设置为c将ard数字转换为该整数。是的,有意义。现在我已经回顾了包含您的两条注释的代码。使用字符串有意义。但是仍然不确定如何告诉输出将前12个“项”更改为星号。谢谢您的评论。