如何询问用户掷 6 面骰子的次数?
How to ask user for number of times to roll a 6 sided dice?
我如何询问用户要掷多少个 6 面骰子才能将它们添加到给定的偏移量?
我有一个 6 面模具滚动并添加到给定的偏移量,但需要添加用户输入 D6。
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);
}
}
您可以编写一个简单的 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);
}
你已经询问了用户他们想要滚动多少次,此时你可以使用 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
}
您可以通过 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);
}
}
这是它应用于您的代码的样子
我如何询问用户要掷多少个 6 面骰子才能将它们添加到给定的偏移量?
我有一个 6 面模具滚动并添加到给定的偏移量,但需要添加用户输入 D6。
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);
}
}
您可以编写一个简单的 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);
}
你已经询问了用户他们想要滚动多少次,此时你可以使用 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
}
您可以通过 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);
}
}
这是它应用于您的代码的样子