如何询问用户滚动6面骰子的次数?

时间:2016-03-24 22:47:44

标签: java dice

如何向用户询问要将多少个6面骰子滚动到其给定的偏移量?

我有一个6面模具滚动并被添加到给定的偏移但需要添加用户输入D6s。

import java.util.*;
import java.lang.Math;

public class Fun

{
    public static void main(String[] args) {

      Scanner scan = new Scanner(System.in); 
      Random rand = new Random();



      System.out.print("How many dice do you want to roll?");
      int D6 = scan.nextInt();


      System.out.print("What would you like your offset to be?");
      int offset = scan.nextInt();

      int roll= rand.nextInt(6)+1;
      int total= roll+offset;

      System.out.print("The result of rolling "+D6+"D6+"+offset+" is "       +total);

}
}

3 个答案:

答案 0 :(得分:0)

你可以写一个简单的for循环,迭代D6次数并加上数字,例如:

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    Random rand = new Random();

    System.out.print("How many dice do you want to roll?");
    int D6 = scan.nextInt();

    System.out.print("What would you like your offset to be?");
    int offset = scan.nextInt();

    int total = 0;

    for(int i=0 ; i<D6 ; i++){
        int number = rand.nextInt(6) + 1;
        System.out.println("Rolled " + number);
        total += number;
    }

    total = total + offset;

    System.out.print("The result of rolling " + D6 + "D6+" + offset + " is " + total);

}

答案 1 :(得分:0)

您已经告诉用户他们想要滚动多少次,此时您可以使用for循环:

System.out.print("How many dice do you want to roll?");
int D6 = scan.nextInt();
for (int i = 0; i < D6; i++) { //This will roll the specified amount of times
    //Rest of code here
}

答案 2 :(得分:0)

你可以通过for循环多次掷骰子:

import java.util.*;
import java.lang.Math;

public class Felcan_A02Q3{
public static void main(String[] args) {

  Scanner scan = new Scanner(System.in); 
  Random rand = new Random();



  System.out.print("How many dice do you want to roll?");
  int D6 = scan.nextInt();


  System.out.print("What would you like your offset to be?");
  int offset = scan.nextInt();

  int total = offset; //the value of total is set to the same as offset
  for(int x = 0; x < D6; x++){ //this loop repeats as often as the value of D6 is
    total =+ rand.nextInt(6)+1; //here every roll of the dice is added to the total value
  }

  System.out.print("The result of rolling "+D6+"D6+"+offset+" is "       +total);

}
}

这就是它应用于您的代码的方式