• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Using random number to multiply by user input

Hey all,
I want to create a random number and multiply it by a users input; the objective is to:
  • Total number of zombies = population - (A random number between: minimum expected survivors and (5 * minimum expected survivors).... So the input is going to be the minimum expected survivors and population. Please tell me what the heck I'm doing wrong. I keep getting a negative number for the number of zombies. Here is the code, which is not finished yet. Thank you!
Java:
import java.util.Random;
import java.util.Scanner;

public class ZombieCalc {

    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        
        
        System.out.print("Enter the population of the outbreak area before infection: ");
        int population = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the minimum expected survivors: ");
        int survivors = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the average estimated shotgun blasts needed to kill 1 zombie: ");
        int shotgun = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the number of zombies each survivor can kill each day: ");
        int zombiekills = Integer.valueOf(scan.nextLine());
        //  Where I'm trying to create the random that will still have the range of the input (
        // example of user putting in 500 for population and 50 for survivors and I get a negative)
        Random rn = new Random();
        int randomNum = rn.nextInt(population) + 0;
        int totzombies = population - (randomNum * 5);
        
                
        System.out.println("****** ANALYSIS ******");
        System.out.println("Population:");
        System.out.println("Total number of Zombies: " + totzombies);
        System.out.println("Total number of survivors: " + survivors);
        System.out.println(" ");
        System.out.println("Supplies:");
        System.out.println("Number of days 'till it's over: ");
        System.out.println("Lbs of food needed: ");
        System.out.println("Gallons of water needed: ");
        System.out.println("Ammount of shotguns needed: ");
        System.out.println("Amount of shotgun shells needed: ");
        
        
    
    }}
 

cbreemer

King Coder
Hey all,
I want to create a random number and multiply it by a users input; the objective is to:
  • Total number of zombies = population - (A random number between: minimum expected survivors and (5 * minimum expected survivors).... So the input is going to be the minimum expected survivors and population. Please tell me what the heck I'm doing wrong. I keep getting a negative number for the number of zombies. Here is the code, which is not finished yet. Thank you!
Java:
import java.util.Random;
import java.util.Scanner;

public class ZombieCalc {

    public static void main(String[] args) {
       
        Scanner scan = new Scanner(System.in);
       
       
       
        System.out.print("Enter the population of the outbreak area before infection: ");
        int population = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the minimum expected survivors: ");
        int survivors = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the average estimated shotgun blasts needed to kill 1 zombie: ");
        int shotgun = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the number of zombies each survivor can kill each day: ");
        int zombiekills = Integer.valueOf(scan.nextLine());
        //  Where I'm trying to create the random that will still have the range of the input (
        // example of user putting in 500 for population and 50 for survivors and I get a negative)
        Random rn = new Random();
        int randomNum = rn.nextInt(population) + 0;
        int totzombies = population - (randomNum * 5);
       
               
        System.out.println("****** ANALYSIS ******");
        System.out.println("Population:");
        System.out.println("Total number of Zombies: " + totzombies);
        System.out.println("Total number of survivors: " + survivors);
        System.out.println(" ");
        System.out.println("Supplies:");
        System.out.println("Number of days 'till it's over: ");
        System.out.println("Lbs of food needed: ");
        System.out.println("Gallons of water needed: ");
        System.out.println("Ammount of shotguns needed: ");
        System.out.println("Amount of shotgun shells needed: ");
       
       
   
    }}
It should be clear why you get a negative # of zombies 80% of the time. Look closely at your statements :

Java:
int randomNum = rn.nextInt(population) + 0;
int totzombies = population - (randomNum * 5);

Only if randomNum < population / 5 (about 20% of cases) this will yield a positive totzombies.
Why the + 0 BTW ?
 
It should be clear why you get a negative # of zombies 80% of the time. Look closely at your statements :

Java:
int randomNum = rn.nextInt(population) + 0;
int totzombies = population - (randomNum * 5);

Only if randomNum < population / 5 (about 20% of cases) this will yield a positive totzombies.
Why the + 0 BTW ?
I used a code similar to what I had before when generating a random number at the end of a username.
System.out.println("Um, it's 2022, that username isn't available. How about " + one + (random.nextInt(999))+ "?");
The 999 in the code being the range that the integer could go up to (so I thought). The +0 was just in a line of code I saw where:
Random random = new Random(int)(Math.random()*(upperRange - (lowerRange-1)))+lowerRange;

Scanner keyboard = new Scanner(System.in);
The upper range would be the user input, the lower range would be 0 (obviously). So I figured I'd just use the input (population) to determine the range. That +0 was just so I would follow the formula (although I don't think it affects anything). Thank you for your help.
 
Here is my final code and still having problems: 1. Calculations are still off and making negative numbers where there shouldn't be. 2. My if/else statement is printing below as opposed to on the same line as the "Number of days left".
Java:
import java.util.Random;
import java.util.Scanner;

public class ZombieCalc {

    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        double shotguns,days,lbs,water;
        
        
        System.out.print("Enter the population of the outbreak area before infection: ");
        int population = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the minimum expected survivors: ");
        int survivors = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the average estimated shotgun blasts needed to kill 1 zombie: ");
        int shotgun = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the number of zombies each survivor can kill each day: ");
        int zombiekills = Integer.valueOf(scan.nextLine());
        Random rn = new Random();
        int totzombies = population - (rn.nextInt(5) * survivors);
        days = totzombies / (survivors * zombiekills);
        lbs = days * survivors * 4;
        water = days * survivors;
        shotguns = survivors * 1.1;
        int shells = totzombies * shotgun;
        
        System.out.println("****** ANALYSIS ******");
        System.out.println("Population:");
        System.out.println("Total number of Zombies: " + totzombies);
        System.out.println("Total number of Survivors: " + (survivors - totzombies));
        System.out.println(" ");
        System.out.println("Supplies:");
        System.out.println("Number of days 'till it's over: " + days );
        if (days > 5) {
            System.out.print("Quite a few days left");
        }  else System.out.print("Not too many days left.");
        System.out.println("Lbs of food needed: " + lbs);
        System.out.println("Gallons of water needed: " + water);
        System.out.println("Ammount of shotguns needed: " + shotguns);
        System.out.println("Amount of shotgun shells needed: " + shells);
        
        
    
    }}
Again, thanks for any help and hope all is well out there!
 

cbreemer

King Coder
Here is my final code and still having problems: 1. Calculations are still off and making negative numbers where there shouldn't be. 2. My if/else statement is printing below as opposed to on the same line as the "Number of days left".
Java:
import java.util.Random;
import java.util.Scanner;

public class ZombieCalc {

    public static void main(String[] args) {
       
        Scanner scan = new Scanner(System.in);
        double shotguns,days,lbs,water;
       
       
        System.out.print("Enter the population of the outbreak area before infection: ");
        int population = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the minimum expected survivors: ");
        int survivors = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the average estimated shotgun blasts needed to kill 1 zombie: ");
        int shotgun = Integer.valueOf(scan.nextLine());
        System.out.print("Enter the number of zombies each survivor can kill each day: ");
        int zombiekills = Integer.valueOf(scan.nextLine());
        Random rn = new Random();
        int totzombies = population - (rn.nextInt(5) * survivors);
        days = totzombies / (survivors * zombiekills);
        lbs = days * survivors * 4;
        water = days * survivors;
        shotguns = survivors * 1.1;
        int shells = totzombies * shotgun;
       
        System.out.println("****** ANALYSIS ******");
        System.out.println("Population:");
        System.out.println("Total number of Zombies: " + totzombies);
        System.out.println("Total number of Survivors: " + (survivors - totzombies));
        System.out.println(" ");
        System.out.println("Supplies:");
        System.out.println("Number of days 'till it's over: " + days );
        if (days > 5) {
            System.out.print("Quite a few days left");
        }  else System.out.print("Not too many days left.");
        System.out.println("Lbs of food needed: " + lbs);
        System.out.println("Gallons of water needed: " + water);
        System.out.println("Ammount of shotguns needed: " + shotguns);
        System.out.println("Amount of shotgun shells needed: " + shells);
       
       
   
    }}
Again, thanks for any help and hope all is well out there!
No, the +0 was not the problem of course. I was just wondering about out it.

So now you have

int totzombies = population - (rn.nextInt(5) * survivors);

What makes you think this should always be positive ? You did not do any checking in the input variables. Recall that rn.nextInt(5) can be 0, 1, 2, 3 or 4.

About your print output from

System.out.println("Number of days 'till it's over: " + days ); if (days > 5) { System.out.print("Quite a few days left"); } else System.out.print("Not too many days left.");

Of course these will be two separate lines, as you have used println first. It should be the other way around : print first, then println.
 
Top