简体   繁体   中英

Random and Boolean Errors

I'm having an issue with my program.

import java.util.*;
public class P1D{
public static void main (String args[]) {

boolean error = false;
String randString;

int i = 0;
System.out.println("Number|Power|Between");

for(i = 1; i <= 10; i++)
    {

    int number = i; //numbers 1-10
    int numbersq = (int)(Math.pow(i, 2)); //square of numbers

    Random rand = new Random();

    int random = (rand.nextInt(numbersq) + (number - 1)); //random number between number & square (inclusive)

    if (number == 1) //prevents random from being over 1
        {
            random = 1;
        }

    if (random > numbersq || random < number) //if random is out of bounds, displays an error
    {
        //error = true;
        randString = (String.valueOf(random) + "   <-- ERROR");

    } //notifies me of errors if random is out of bounds

    else //if no error, random isn't changed and error=false
    {
        error = false;
        randString = String.valueOf(random);
    }
    System.out.println(number + "\t" + numbersq + "\t" + randString);
    }

    if ( error = false )
        {
            System.out.println("all good in the hood");
        }

    else
        {
            System.out.println("Something went wrong");
        }

}
}

The program is intended to:

  • List numbers 1-10.
  • Square the number.
  • Print a random number between the original number and the square (inclusive)

The issue is that the random number is often above or below the limit (not by much) and the Boolean to check for errors always says that there is an error, even if there isn't one.

Can somebody offer advice on how to remedy the random limit and the Boolean issue?

Try this solution - it meets all your requirements

import java.util.*;

public class P1D{

    public static void main (String args[]) {
        boolean error = false;
        String randString;
        System.out.println("Number|Power|Between");

       for(int i = 1; i <= 10; i++)
       {
            int numbersq = i*i; //square of numbers
            Random rand = new Random();

            //random number between number & square (inclusive)
            int random = (rand.nextInt((numbersq - i) + 1) + i);

            //prevents random from being over 1
            if (i == 1)
                random = 1;

            //if random is out of bounds, displays an error
            if (random > numbersq || random < i) {
                error = true;
                randString = (String.valueOf(random) + "   <-- ERROR");
            }
            //notifies me of errors if random is out of bounds

            //if no error, random isn't changed and error=false
            else {
                error = false;
                randString = String.valueOf(random);
            }
            System.out.println(i + "\t" + numbersq + "\t" + randString);
        }

        if (!error) {
            System.out.println("all good in the hood");
        } else {
            System.out.println("Something went wrong");
        }

    }
}

Number|Power|Between

1 1 1

2 4 3

3 9 9

4 16 10

5 25 11

6 36 29

7 49 24

8 64 35

9 81 75

10 100 52

all good in the hood

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM