gpt4 book ai didi

java - 要求用户输入遵循字符串格式

转载 作者:行者123 更新时间:2023-12-01 00:36:23 26 4
gpt4 key购买 nike

<分区>

在我学习 Java 的过程中,我在做我想让我的程序做的事情时也遇到了麻烦。我正在观看 thenewboston 学习,但是,他教了我一切,即如何格式化用户输入。

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

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 上找到了多份关于格式的指南,但找不到按照我想要的方式进行格式化的指南。

谁比你们更值得问!另外,如果您有关于如何改进我的程序的提示,请分享。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com