简体   繁体   English

我正在尝试使用带有 .charAt() 的 Scanner 从单个字符串获取字符输入

[英]I am trying to take to char inputs from a single string using Scanner with .charAt()

im trying to make a dice roller that can take a single input and roll a die with a certain number of sides a certain number of times.我正在尝试制作一个骰子滚轮,它可以接受单个输入并将具有一定数量边的骰子滚动一定次数。 so if I input "2d4" itll roll a 4 sided die 2 times, and give me both results separately.因此,如果我输入“2d4”,它将掷 2 次 4 面骰子,并分别给我两个结果。 To do this ive been trying to use the scanner class since im rather new to programming.为此,我一直在尝试使用扫描仪类,因为我对编程还很陌生。 Im trying to take it step by step and use what I know.我试图一步一步地使用我所知道的。 Im sure there are better classes to use and such but Im trying to do it with scanner specifically.我确定有更好的类可以使用,但我试图专门用扫描仪来做。

import java.util.Scanner;
import java.util.Random;
public class DiceRoller3 {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Random Random = new Random();

        char NumberOfDice = input.next().charAt(0);

        char DieType = input.next().charAt(2);

This, i believe is causing me to have to create two inputs, due to it searching for the "next" input to look for a character in. I would like to be able to do it with one input, is there a way to do that?我相信这导致我必须创建两个输入,因为它会搜索“下一个”输入以查找字符。我希望能够用一个输入来完成,有没有办法做到那?

You can use the nextLine method of Scanner class您可以使用 Scanner 类的 nextLine 方法

String userInput = input.nextLine();
char noOfDie = userInput.charAt(0);
char dieType = userInput.charAt(2);

The method returns a string of a complete line entered by the user.该方法返回用户输入的完整行的字符串。 Works by giving your input and pressing enter.通过提供您的输入并按 Enter 来工作。

Try this Scanner input = new Scanner(System.in);试试这个 Scanner input = new Scanner(System.in); Random Random = new Random();随机随机=新随机();

    String inputString = input.next();
    char numberOfDice = inputString.charAt(0);
    char dieType = inputString.charAt(2);

Please mind each time you call next() for scanner you read next String value from input stream.请注意每次为扫描仪调用 next() 时,您都会从输入流中读取下一个字符串值。 It is of course up to you how to input your data but you could also enter it separately, ie如何输入数据当然取决于您,但您也可以单独输入,即

2
d
4

instead of代替

2d4

in that case you would read it like this:在这种情况下,你会这样读:

    char numberOfDice = input.next().charAt(0);
    char die = input.next().charAt(0);
    char dieType = input.next().charAt(0);

I'm not a java guy but if the format is always number d number you can do this我不是一个 java 人,但如果格式总是 number d number 你可以这样做

 Scanner input = new Scanner(System.in);
 String myinput= input.nextLine();
 int NumberOfDice=Character.getNumericValue(myinput.charAt(0));
 int DieType= Character.getNumericValue(myinput.charAt(2));

EDIT: this is better if the your dice number can go above 9编辑:如果您的骰子数可以超过 9,那就更好了

 int NumberOfDice=Integer.parseInt(myinput.substring(0,String.IndexOf("d")));
 int DieType =Integer.parseInt(myinput.substring(String.IndexOf("d")+1)));

You may want to get the whole string at once, locate the position of d , then get the left and the right part of it :您可能想一次获取整个字符串,找到d的位置,然后获取它的左右部分:

    Scanner input = new Scanner(System.in);

    String string = input.nextLine();

    int indexOfD = string.indexOf('d');

    String numberOfDice = string.substring(0, indexOfD);
    String dieType = string.substring(indexOfD + 1, string.length());

    System.out.println(numberOfDice);
    System.out.println(dieType);

Alternatively, you may also split the string with d as the separator :或者,您也可以使用d作为分隔符分割字符串:

    String[] parts = string.split("d");

    String numberOfDice = parts[0];
    String dieType = parts[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM