mercredi 19 octobre 2016

how to generate 25 odd random numbers using only the for and if statements

import java.util.Random;
import java.util.Scanner;

public class JavaRandomNumbers
{
  public static void main (String args[])
  {
   Random rand = new Random();
   Scanner input = new Scanner(System.in);
   int number = 0;

1. Asking the user

   System.out.println("Please set the range");
   System.out.print("Max: ");
   int max = input.nextInt();
   max += 1;
   System.out.print("Min: ");
   int min = input.nextInt();

2. Randomizing number

   number = rand.nextInt(max)+min;
   int n1 = 1;

3. Loop

    for(int i = 0; i<=24;){
    int n = number % 2;
    if (n >= 0){
       System.out.println(n1 + ". " + number);
       number = rand.nextInt(max)+min;
       n1 ++;
       i++;
        }
    else {
       number = rand.nextInt(max)+min;
         }}
  }}

4. I'm a bit new to coding as in i just started 2 weeks ago and have done about 3 hours of work i would appreciate some help




Aucun commentaire:

Enregistrer un commentaire