Java 试着制作一个骰子辊,它能够通过一个单一的输入将一个有特定边数的骰子滚动特定的次数

Java 试着制作一个骰子辊,它能够通过一个单一的输入将一个有特定边数的骰子滚动特定的次数,java,java.util.scanner,dice,random,Java,Java.util.scanner,Dice,Random,到目前为止,我使用.charAt()来识别say“1d6”中的第一个和第三个字符,意思是滚动一次六面骰子。为此,我创建了一个for循环,使用范围为1的random类和die type(在本例中为6),循环执行次数由输入决定(在本例中为1)。问题是我得到的值是相当随机的 import java.util.Scanner; import java.util.Random; public class DiceRoller3 { public static void main(String[]

到目前为止,我使用.charAt()来识别say“1d6”中的第一个和第三个字符,意思是滚动一次六面骰子。为此,我创建了一个for循环,使用范围为1的random类和die type(在本例中为6),循环执行次数由输入决定(在本例中为1)。问题是我得到的值是相当随机的

import java.util.Scanner;
import java.util.Random;
public class DiceRoller3 {
    public static void main(String[] args) {
    //Creates a new scanner object
    Scanner input = new Scanner(System.in);
    //Creates a new Random object
    Random random = new Random();

    //fluff text
    //System.out.println("What would you like to roll?");

    //Sets dieInput equal to the input the scanner reads.
    String dieInput = input.next();

    //sets noOfDice equal the the first character of the dieInput String
    int noOfDice = dieInput.charAt(0);

    //sets dieType equal to the 3rd character of the dieInput String
    int dieType = dieInput.charAt(2);

    //test to show the value of noOfDice and dieType
    System.out.println(dieInput);
    System.out.println(noOfDice);
    System.out.println(dieType);


    //Loop to roll the die type determined by the input, a number of times 
    that is also determined by the input
    for(int x = 1; x <= noOfDice; x++)
    {
        int roll = random.nextInt(dieType) + 1;

        System.out.println(roll);
    }

}
import java.util.Scanner;
导入java.util.Random;
公共类掷骰子机3{
公共静态void main(字符串[]args){
//创建新的扫描仪对象
扫描仪输入=新扫描仪(System.in);
//创建新的随机对象
随机=新随机();
//绒毛状文字
//System.out.println(“您想要滚动什么?”);
//将输入设置为扫描仪读取的输入。
字符串dieInput=input.next();
//将noOfDice设置为等于输入字符串的第一个字符
int noOfDice=dieInput.charAt(0);
//将dieType设置为输入字符串的第三个字符
int dieType=dieInput.charAt(2);
//显示noOfDice和dieType值的测试
系统输出打印项次(输入);
系统输出打印LN(noOfDice);
System.out.println(dieType);
//循环滚动模具类型由输入确定,次数
这也是由输入决定的

对于(int x=1;x代码中的两个问题:

  • 改变

    // this gets the ASCII value which is why you are getting weird numbers 
    // ASCII value of 1 is 49
    int noOfDice = dieInput.charAt(0);  
    

  • 使用此选项可生成介于1和
    dietype
    之间的随机数:

    int minVal = 1;
    int roll = (minVal + random.nextInt(dieType - minVal + 1);
    

  • 您的代码有两个问题:

  • 改变

    // this gets the ASCII value which is why you are getting weird numbers 
    // ASCII value of 1 is 49
    int noOfDice = dieInput.charAt(0);  
    

  • 使用此选项可生成介于1和
    dietype
    之间的随机数:

    int minVal = 1;
    int roll = (minVal + random.nextInt(dieType - minVal + 1);
    

  • 您希望从字符串中获取整数,而不是字符

    String[] numbers = dieInput.split("d"); // "1d6" split into "1" and "6"
    int noOfDie = Integer.parseInt(numbers[0]);
    int dieType = Integer.parseInt(numbers[1]);
    
    这将避免从
    string.charAt()
    方法返回的
    char
    值中获取ascii值(也是一个int)时遇到的问题

    编辑:


    我的代码片段中的
    numbers
    数组表示拆分输入的结果。根据预期输入的两个数字,它们由
    d
    分隔,例如
    1d6
    3d10
    ,结果将是一个数组,其中包含两个项目,
    d
    前面的数字和后面的数字。例如,如果您调用d拆分方法和输入字符串有多个
    d
    字符,您将得到一个更大的数组。例如,
    1d6d7
    将生成一个包含3个项目的数组,
    “1”
    “6”
    “7”
    。即使内容是string类型,我在上下文中也希望它们的值是数字。这就是为什么在下面几行中
    Integer.parseInt()
    用于将值从字符串转换为整数。这仅在字符串值为有效整数时有效。

    您希望从字符串中获取整数,而不是字符。以下是多种方法之一

    String[] numbers = dieInput.split("d"); // "1d6" split into "1" and "6"
    int noOfDie = Integer.parseInt(numbers[0]);
    int dieType = Integer.parseInt(numbers[1]);
    
    这将避免从
    string.charAt()
    方法返回的
    char
    值中获取ascii值(也是一个int)时遇到的问题

    编辑:


    我的代码片段中的
    numbers
    数组表示拆分输入的结果。根据预期输入的两个数字,它们由
    d
    分隔,例如
    1d6
    3d10
    ,结果将是一个数组,其中包含两个项目,
    d
    前面的数字和后面的数字。例如,如果您调用d拆分方法和输入字符串有多个
    d
    字符,您将得到一个更大的数组。例如,
    1d6d7
    将生成一个包含3个项目的数组,
    “1”
    “6”
    “7”
    。即使内容是string类型,我在上下文中也希望它们的值是数字。这就是为什么在下面几行中
    Integer.parseInt()
    用于将值从字符串转换为int。这仅在字符串的值为有效int时有效。

    基于OOP思想,我将编写
    Dice
    类。例如:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class Dice {
    
        private static final Random RANDOM = new Random();
    
        private int sides;
    
        private Dice(){
            //no Dice constructor without the sides parameter
        }
    
        public Dice(int sides){
            if (sides <= 0){
                throw new RuntimeException();
            }
            else {
                this.sides = sides;
            }
        }
    
        public int getSides(){
            return this.sides;
        }
    
        private int rollMyDice(){
            return  1 + RANDOM.nextInt(this.sides);
        }
    
        public List<Integer> rollMyDiceManyTimes(int howManyRolling){
            List<Integer> result = new ArrayList<>();
            for (int i = 0; i < howManyRolling; i++){
                result.add(rollMyDice());
            }
            return result;
        }
    
    }
    
    当然,这只是我试图提供给你的一个模板

    一些输出将是:

    I'm rolling the dice with 9 sides  ELEVEN TIMES [9, 4, 5, 2, 8, 7, 3, 8, 2, 1, 4]
    

    通过一些面向对象的思想,我将编写
    Dice
    类。例如:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class Dice {
    
        private static final Random RANDOM = new Random();
    
        private int sides;
    
        private Dice(){
            //no Dice constructor without the sides parameter
        }
    
        public Dice(int sides){
            if (sides <= 0){
                throw new RuntimeException();
            }
            else {
                this.sides = sides;
            }
        }
    
        public int getSides(){
            return this.sides;
        }
    
        private int rollMyDice(){
            return  1 + RANDOM.nextInt(this.sides);
        }
    
        public List<Integer> rollMyDiceManyTimes(int howManyRolling){
            List<Integer> result = new ArrayList<>();
            for (int i = 0; i < howManyRolling; i++){
                result.add(rollMyDice());
            }
            return result;
        }
    
    }
    
    当然,这只是我试图提供给你的一个模板

    一些输出将是:

    I'm rolling the dice with 9 sides  ELEVEN TIMES [9, 4, 5, 2, 8, 7, 3, 8, 2, 1, 4]
    

    调用dieInput.charAt(n)时,在将其存储在noOfDive或dieType中之前,需要将其转换为int。请尝试调用Character.getNumericValue(c)并存储它。旁白:因为您使用的是
    charAt(2)
    您只能得到1位数字。
    2d12
    会发生什么事?
    10d4
    会发生什么事?您可以拆分输入字符串
    string[]values=dieInput.split(“d”)
    这将为您提供
    值[0]中的计数和
    值[1]中的边数
    ,即使对于
    25d100
    -您仍然必须转换为整数,正如Nicholas K所说(但可能是使用
    Integer.parseInt(值[0])
    ,因为它是字符串,而不是字符。@Mobeus:有什么答案可以满足您的问题吗?当您调用dieInput.charAt(n)时,在将其存储到noOfDive或dieType中之前,您需要将其转换为int。尝试调用Character.getNumericValue(c)并存储该值。旁白:由于您使用的是
    charAt(2)
    ,因此只会得到1个数字。
    2d12
    会发生什么?对于
    10d4
    会发生什么?您可以将输入字符串拆分为
    string[]values=dieInput.split(“d”)
    ,它将为您提供
    values[0]
    中的计数和
    values[1]
    中的边,即使对于
    25d100
    之类的东西,您仍然必须转换为整数,就像Nicholas K所说的那样(但可能使用
    Integer.parseInt(值[0])