Java 如何将其转换为表输出?
问题是要我掷两个骰子,分别在两列中打印输出,然后为两个骰子的总和创建第三列Java 如何将其转换为表输出?,java,dice,Java,Dice,问题是要我掷两个骰子,分别在两列中打印输出,然后为两个骰子的总和创建第三列 import java.util.Random; public class DiceRolls { public static void main(String[] args) { System.out.println("Dice 1\tDice 2"); Random ran = new Random(); int numberOne; for
import java.util.Random;
public class DiceRolls {
public static void main(String[] args) {
System.out.println("Dice 1\tDice 2");
Random ran = new Random();
int numberOne;
for (int x = 0; x < 7; x++) {
numberOne = ran.nextInt(6) + 1;
System.out.println(numberOne);
}
int numberTwo;
for (int y = 0; y < 7; y++) {
numberTwo = ran.nextInt(6) + 1;
System.out.println(" " + numberTwo);
}
}
}
import java.util.Random;
公营骰子{
公共静态void main(字符串[]args){
System.out.println(“骰子1\tDice 2”);
Random ran=新的Random();
整数;
对于(int x=0;x<7;x++){
numberOne=ran.nextInt(6)+1;
系统输出打印LN(数字);
}
整数二;
对于(int y=0;y<7;y++){
numberTwo=ran.nextInt(6)+1;
系统输出打印项次(“+numbertw);
}
}
}
我认为您的想法是错误的,试图在一个模具的所有卷上循环,然后在另一个模具上循环。如果您尝试同时滚动两个骰子,然后添加它们并打印输出,这会使事情简单得多:
//How many runs you want
int numRuns = 7;
for (int x = 0; x < numRuns; x++) {
Random ran = new Random();
int dieOne = ran.nextInt(6) + 1;
int dieTwo = ran.nextInt(6) + 1;
System.out.format("| Die 1:%3d| Die 2:%3d| Total:%3d|\n", dieOne, dieTwo, dieOne + dieTwo);
}
最后一行是怎么做的“System.out.format”(|%4d |%4d |%5d | \n),dieOne,dieTwo,dieOne+dieTwo);@Cleo在正文中添加了一个解释。4,4,5是什么之间的空格?还有为什么你要写dieOne,dieTwo,然后再写dieOne+dieTwo?
//How many runs you want
int numRuns = 7;
System.out.println("-----------------");
System.out.println("|Die1|Die2|Total|");
System.out.println("-----------------");
for (int x = 0; x < numRuns; x++) {
Random ran = new Random();
int dieOne = ran.nextInt(6) + 1;
int dieTwo = ran.nextInt(6) + 1;
System.out.format("|%4d|%4d|%5d|\n", dieOne, dieTwo, dieOne + dieTwo);
}
System.out.println("-----------------");