首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在java的一行中输出5个随机数?

如何在java的一行中输出5个随机数?
EN

Stack Overflow用户
提问于 2018-10-07 23:56:41
回答 2查看 0关注 0票数 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在另一条线上的秒针

EN

回答 2

Stack Overflow用户

发布于 2018-10-08 08:05:30

代码System.out.println();将打印换行符。你必须在你想要的每只手循环的地方注入那些代码。由于这是家庭作业,我会将这部分留给读者

票数 0
EN

Stack Overflow用户

发布于 2018-10-08 08:57:53

这样试试:

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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档