内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
我需要帮助在一行中打印5个随机整数,这里是我的代码和输出
import java.util.*;
public class Hw05{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
Random rand = new Random();
System.out.println("How many times do you want to generate a hand");
while(!myScanner.hasNextInt()){
myScanner.next();
System.out.println("Please input valid statement");
}
int numHands = myScanner.nextInt();
int counter2 = 0;
while (counter2 < numHands){
counter2++;
int counter = 1;
while(counter <= 5){
counter++;
int cardGen = rand.nextInt(52) + 1;
System.out.print(cardGen + " ");
}
}
} }
输入2手数时的输出:
你想要多少次生成一只手
2
42 32 45 48 37 4 16 15 42 14
理想情况下我想要
42 32 45 48 37一只手
4 16 15 42 14在另一条线上的秒针
这样试试:
string hand=""; for(int l=0;l<2(number of hands variable),l++){ for(int h=0;h<5(number of cards variable);h++){ int cardGen = rand.nextInt(52) + 1; hand+= cardGen.ToString()+" "; } hand+="/n"; } System.out.print(hand);