[英]Trying to make a dice roller thats able to roll a die with a specific number of sides a specific number of times via a single input
到目前为止,我正在使用.charAt()来识别“ 1d6”中的第一个和第三个字符,这意味着一次滚动六面骰子。 为此,我制作了一个for循环,以使用范围为1且模具类型(在这种情况下为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);
}
}
}
当我运行此程序时,程序告诉我noOfDice和dieType的值等于49或50或其他一些较大的数字,我不明白为什么会这样。 dieInput的值正确,但是当从dieInput读取两个字符时,它们将变得不正确。 有什么想法为什么呢? 以及我代码中的其他任何问题。
免责声明:我是编码的新手,我想用我所知道的(例如,扫描仪和随机)来做到这一点,我想我想有更有效的方法来做我想做的事,但是我想用我所用的工具来做有。
您的代码有两个问题:
更改
// this gets the ASCII value which is why you are getting weird numbers // ASCII value of 1 is 49 int noOfDice = dieInput.charAt(0);
至
// this gets numeric value of the character int noOfDice = Character.getNumericValue(dieInput.charAt(0)); int dieType = Character.getNumericValue(dieInput.charAt(2));
使用它来生成介于1和dietype
之间的随机数:
int minVal = 1; int roll = (minVal + random.nextInt(dieType - minVal + 1);
经过一些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;
}
}
然后,您可以对其进行测试(和/或重写代码以满足您的目标):
public class Answer {
public static void main(String[] args) {
//test
Dice dice = new Dice(9);
System.out.println("I'm rolling the dice with "
+ dice.getSides()
+ " sides "
+ " ELEVEN TIMES "
+ dice.rollMyDiceManyTimes(11));
}
}
当然,这只是我尝试提供给您的模板...
一些输出将是:
I'm rolling the dice with 9 sides ELEVEN TIMES [9, 4, 5, 2, 8, 7, 3, 8, 2, 1, 4]
您想从字符串中获取数字,而不是字符。 这是许多方法之一
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
的预期输入,结果将是其中包含2个项目的数组,即d
之前的数字和d
之后的数字。 例如,如果您调用了split方法,并且输入字符串包含多个d
字符,则会得到一个更大的数组。 例如, 1d6d7
将导致一个数组,其中包含3个项: "1"
, "6"
和"7"
。 即使内容是字符串类型,我在上下文中也希望它们的值是数字。 这就是为什么在以下几行中使用Integer.parseInt()
将值从String转换为int的原因。 仅当字符串的值是有效的int时才有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.