Closed. This question needs to be more focused。它当前不接受答案。
想改善这个问题吗?更新问题,使其仅关注editing this post一个问题。
3年前关闭。
Improve this question
当我学习Java时,在执行我希望程序执行的操作时也遇到了麻烦。我正在看新闻波士顿学习,但是,他教给我了一切,即如何格式化用户输入。
到目前为止,这是我的代码。
因此,我需要格式化用户输入的“cpr”,我需要采用######-####格式。我得到了长度部分,但是,如果用户不满足有关格式的要求,我想说的是请重试,就像使用cpr一样。我在Google上找到了多种有关格式的指南,但找不到能按照我想要的格式进行格式化的指南。
有谁比你们问得更好!另外,如果您有关于如何改善程序的提示,请分享。 您检查长度为11 ---好的,您已经知道了 您迭代您的输入字符串;然后检查每个字符是否都是数字。 [除了第7个字符-必须是“-”! ]
要检查字符是否为数字,可以使用Character.isDigit()。
在这种情况下,您可以将该检查放入单个正则表达式中;并尝试一下。
但是如前所述;初学者应该从手动做这些事情开始。了解发生了什么事;只有这样继续研究更强大但也更复杂的解决方案。
想改善这个问题吗?更新问题,使其仅关注editing this post一个问题。
3年前关闭。
Improve this question
当我学习Java时,在执行我希望程序执行的操作时也遇到了麻烦。我正在看新闻波士顿学习,但是,他教给我了一切,即如何格式化用户输入。
到目前为止,这是我的代码。
import java.util.Scanner;
public class FitnessExtreme {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to FitnessExtreme Centre");
System.out.println(" XX XX");
System.out.println(" XX XX");
System.out.println(" XXXX");
System.out.println(" XX XX");
System.out.println(" XX XX");
int membership = 299;
double tax = 1.25;
double total = membership*tax;
// Scans for user input
Scanner scan = new Scanner(System.in);
// Retrieves customer name
System.out.println("First name: ");
String fname = scan.next();
System.out.println("Last name: ");
String lname = scan.next();
// Retrieves CPR number
System.out.println("CPR number: ");
String cpr = scan.next();
// Validate CPR format
if(cpr.length() != 11) {
System.out.println("Format was not met, please try again");
System.out.println("CPR number: ");
cpr = scan.next();
}
// Retrieves Telephone number
System.out.println("Telephone number: ");
String phone = scan.next();
// Retrieves Address
System.out.println("House number: ");
String hnumber = scan.next();
System.out.println("Street name: ");
String sname = scan.next();
System.out.println("Post code: ");
String pcode = scan.next();
// Customer print out
System.out.println("Thank you " + fname + " " + lname);
System.out.println("I have registered following information on you: ");
System.out.println("CPR number: " + cpr);
System.out.println("Telephone number: " + phone);
System.out.println("House number: " + hnumber);
System.out.println("Street name: " + sname);
System.out.println("Post code " + pcode);
// Continue?
System.out.println("");
System.out.println("If this information is correct and you want to continue, please enter 'yes'");
String verify = scan.next();
if(verify.equals("yes")) {
System.out.println("Alright " + fname + " your price is gonna be:");
System.out.println("Membership subscription - subtotal: " + membership);
System.out.println("VAT " + tax);
System.out.println("Total: " + total);
}
}
}
因此,我需要格式化用户输入的“cpr”,我需要采用######-####格式。我得到了长度部分,但是,如果用户不满足有关格式的要求,我想说的是请重试,就像使用cpr一样。我在Google上找到了多种有关格式的指南,但找不到能按照我想要的格式进行格式化的指南。
有谁比你们问得更好!另外,如果您有关于如何改善程序的提示,请分享。
最佳答案
对于初学者,我建议不要使用“最短”的解决方案,而应该使用易于理解的解决方案。
您说您想要这种格式######-####;换句话说:6位数,破折号,4位数。
您可以像这样建立支票:
要检查字符是否为数字,可以使用Character.isDigit()。
在这种情况下,您可以将该检查放入单个正则表达式中;并尝试一下。
但是如前所述;初学者应该从手动做这些事情开始。了解发生了什么事;只有这样继续研究更强大但也更复杂的解决方案。