简体   繁体   English

编写一个程序,该程序生成10到100之间的10个随机整数的列表

[英]Write a program that generates a list of 10 random integers between 10 and 100

I am having trouble with the following assignment: 我在执行以下作业时遇到了麻烦:

Write a program that generates a list of 10 random integers between 10 and 100. Your program should print the values followed by how many values were odd and how many were even. 编写一个程序,该程序生成10到100之间的10个随机整数的列表。您的程序应打印值,后跟多少个奇数和偶数个值。

Here is the code I have so far: 这是我到目前为止的代码:

public static void main(String[]args)
{
  for(int i = 0; i<10; i++){
        System.out.println("Random number: " + (int)(Math.random() * 101 + 10));
        if((int)(Math.random() * 101 + 10) %2 == 0){
          System.out.println((int)(Math.random() * 101 + 10) + " even numbers were generated");}
        else
          System.out.println((int)(Math.random() * 101 + 10) + "odd numbers were generated");
        }
   }
}

Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!

for (int i = 0; i < 10; i++) {
    System.out.println((int) (Math.random() * (90 + 1)) + 10);
}

This will generated and print 10 random integers between 10 and 100. As for the odd and even part, you can divide the number by 2 and check for the remainder. 这将生成并打印10至100之间的10个随机整数。对于奇数和偶数部分,您可以将数字除以2,然后检查余数。 I'll leave the rest to you. 剩下的我留给你。

This will generate 10 random integers between 10 and 100, perform the count of even and odd and finally display the counts. 这将生成10到100之间的10个随机整数 ,对偶数和奇数进行计数,最后显示计数。

public static void main(String[] args) {
  Random rnd = new Random(System.currentTimeMillis());
  int even = 0;
  int odd = 0;
  for (int i = 0; i < 10; i++) {
    int randomNumber = 10 + rnd.nextInt(91);
    System.out.println("Random number " + (1 + i)
        + ": " + randomNumber);
    if (randomNumber % 2 == 0) {
      even++;
    } else {
      odd++;
    }
  }
  System.out.println(even + " even numbers "
      + "were generated");
  System.out.println(odd + " odd numbers "
      + "were generated");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在10x10矩阵中生成0-100之间的随机数,然后显示大于75的数字。 - Generates a random number between 0-100 in a 10x10 matrix and then display numbes greater than 75. 你如何编写一个 java 程序生成前 100 个回文数(从 11、22 等开始),每行 10 个数字 - How do you write a java program that generates the first 100 palindromic numbers (starting from 11, 22, etc), with 10 numbers per line JTable的RowSorter无法正确对整数进行排序(1,10,100…2,20…3) - RowSorter of JTable not sorting Integers correctly (1,10,100…2,20…3) 如何为链表编写随机整数的构造函数 - How to write a constructor of random integers for a Linked List 编写一个程序,生成随机的20次掷骰序列 - Write a program that generates a random sequence of 20 die tosses 尝试使用 0-10 之间的 10 个随机数生成 2 个向量 - Trying to generate 2 vectors with 10 random numbers between 0-10 如何返回一个包含10个随机整数的数组中的最大整数? - How to return the largest integer in an Array that has 10 random integers in it? 如何在数组大小为10的情况下生成最多100个随机数 - How to generate random numbers up to 100 in an array size of 10 输入 10 个整数或 0 后程序不会退出 - Program won't quit after 10 integers or 0 have been entered 读取 1 到 100 之间的整数并计算每个整数的出现次数的程序 - program that reads integers between 1 and 100 and counts the occurrence of each
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM