gpt4 book ai didi

java - 我如何在java中分割我的输入值

转载 作者:行者123 更新时间:2023-12-02 03:16:54 25 4
gpt4 key购买 nike

import java.util.Scanner;

public class Main_Code {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("please input dice value");
String pattern;
pattern = input.nextLine();
String[] parts = pattern.split("[d][-]");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];

System.out.println(
"The Pattern is : " + pattern + "\n" +
"Number of dice : " + part1 + "\n" +
"Number of dice surface : " + part1 + "\n" +
"Card numbers is : " + part3);

我会用扫描仪输入(**2d6-4, 3d6-3**)这种类型的值。现在我想分割2d6-4(骰子数量:2,骰子面数:6,牌数是4)我该如何分割?

最佳答案

您可以使用正则表达式并拆分为多个标记:

 final String input = "2d6-4";
final String[] tokens = input.split("-|d");
System.out.println(Arrays.toString(tokens));

输出将是一个数组,如下所示:

[2, 6, 4]

骰子、表面、卡片......

关于java - 我如何在java中分割我的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40170622/

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