d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Java Scripting (net Beans) > Java
Add Reply New Topic New Poll
Member
Posts: 2,204
Joined: Feb 4 2012
Gold: 1,645.05
Sep 4 2013 04:08pm
I have to write a program to generate Random Price that prints a random price between $10.00 and $19.95 every time the program is run.

atm i have this:


package pricegen;
//Price gen $10-19.95

import java.util.Random;


public class PriceGen {
public static void main(String[] args) {
Random rand = new Random();
double randomNum = rand.nextDouble(19.95 - 10);
System.out.println(randomNum);
}
}


This post was edited by urtuu on Sep 4 2013 04:10pm
Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Sep 4 2013 06:12pm
Quote (urtuu @ Sep 4 2013 05:08pm)
I have to write a program to generate Random Price that prints a random price between $10.00 and $19.95 every time the program is run.

atm i have this:


package pricegen;
//Price gen $10-19.95

import java.util.Random;


public class PriceGen {
    public static void main(String[] args) {   
    Random rand = new Random();
    double randomNum = rand.nextDouble(19.95 -  10);
  System.out.println(randomNum);
}
}



Try this, although I can't guarantee it prints exactly what you want since I don't have anything to compile/run java code at hand...

double randomNum = rand.nextDouble(19.95 - 10) +10



Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Sep 4 2013 07:34pm
Whelp, apparently I'm not familiar with the rand library...

try this

rand.nextDouble()*(19.95-10.0) + 10.0

The problem here is that nextDouble does not take a parameter; it spits out a double between 0 and 1
Member
Posts: 2,204
Joined: Feb 4 2012
Gold: 1,645.05
Sep 4 2013 07:39pm
Quote (zackill4 @ Sep 4 2013 08:34pm)
Whelp, apparently I'm not familiar with the rand library...

try this

rand.nextDouble()*(19.95-10.0) + 10.0

The problem here is that nextDouble does not take a parameter; it spits out a double between 0 and 1



package pricegen;
//Price gen $10-19.95

import java.util.Random;



public class PriceGen {
public static void main(String[] args) {
Random randNumb = new Random();
rand.nextDouble()*(19.95-10.0) + 10.0;
System.out.println(randomNumb);


this is what i have now it is saying that it is not a statement.....and when i run it it says run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
at pricegen.PriceGen.main(PriceGen.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Sep 4 2013 07:42pm
maybe because you never declared randomNumb and you didn't store the result of the calculation?

Try this

rand.nextDouble()*(19.95-10.0) + 10.0;
System.out.println(randomNumb);

change to
System.out.println(rand.nextDouble()*(19.95-10.0) + 10.0);

Edit:
sigh, don't make me break out my java compiler lol

This post was edited by zackill4 on Sep 4 2013 07:43pm
Member
Posts: 2,204
Joined: Feb 4 2012
Gold: 1,645.05
Sep 4 2013 07:44pm
okay, that worked now is there a way to make it only 2 dec places?
Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Sep 4 2013 07:48pm
Quote (urtuu @ Sep 4 2013 08:44pm)
okay, that worked now is there  a way to make it only 2 dec places?


DecimalFormat df = new DecimalFormat("##.##");
System.out.println(df.format(rand.nextDouble()*(19.95-10.0) + 10.0));

there are a few other ways to do this apparently
Member
Posts: 2,204
Joined: Feb 4 2012
Gold: 1,645.05
Sep 4 2013 07:49pm
Quote (zackill4 @ Sep 4 2013 08:48pm)
DecimalFormat df = new DecimalFormat("##.##");
System.out.println(df.format(rand.nextDouble()*(19.95-10.0) + 10.0));

there are a few other ways to do this apparently


it says canot find symble
Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Sep 4 2013 08:00pm
Quote (urtuu @ Sep 4 2013 08:49pm)
it says canot find symble


got it fixed lol, forgot the import

thanks for the tip :)
Go Back To Homework Help Topic List
Add Reply New Topic New Poll