Jump to content

Java Programming Help

creativename

My assignment is to make a java app that generates 3 random numbers, between 1 and 50, and then make it output the lowest of the 3 random numbers.

 

 

I have managed to get the user to input 3 numbers, and make it find the lowest number of that, and I have also managed to generate 3 random numbers between 1 and 50. But I cannot figure out how to get it to both at the same time.

 

 

The system output should be something like this

 

EXAMPLE:
 Generated Numbers: 1, 50, 100

Lowest Number: 1

 

 

 

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

no guarantee, but try this:

 

store each value in a var: a, b, c

if(a<=b&&a<=c) { System.out.printl(a);}else if(b<=c&&b<=a){ System.out.printl(b);}else if(c<=a&&c<=b){ System.out.printl(c);}else{ System.out.printl("Solution did not work!");}

Java Programmer, AMD Fanboy and soon to be casemodder

Link to comment
Share on other sites

Link to post
Share on other sites

Its hard to tell what you're going for but it looks like what you're missing is storing the randomly generated numbers as variables.
eg:

 

x = random_number

y = random_number

z = random_number

 

print (x,y,z)

 

result = find_lowest_number_function (x,y,z)

 

print (result)

 

forgive my pseudo code, I haven't worked with java in a while but something along those lines should work.

Link to comment
Share on other sites

Link to post
Share on other sites

maybe after they have been stored as variables, try the Math.min(int a, int b, int c) method??

Link to comment
Share on other sites

Link to post
Share on other sites

If you can make the user input 3 numbers and you can find the lowest of them you shouldn't have any problem.

If you post your code that does the above I/we can tell you how you need to modify in order to use random numbers.

CPU: Ryzen 3 3600 | GPU: Gigabite GTX 1660 super | Motherboard: MSI Mortar MAX | RAM: G Skill Trident Z 3200 (2x8GB) | Case: Cooler Master Q300L | Storage: Samsung 970 EVO 250G + Samsung 860 Evo 1TB | PSU: Corsair RM650x | Displays: LG 27'' G-Sync compatible 144hz 1080p | Cooling: NH U12S black | Keyboard: Logitech G512 carbon | Mouse: Logitech g900 

Link to comment
Share on other sites

Link to post
Share on other sites

Here is what I came up with:

import java.util.Random;public class LowestNum {	public static void main(String[] args) {		Random r = new Random();				int randNums[] = {r.nextInt(50) + 1, r.nextInt(50) + 1, r.nextInt(50) + 1}; //Generates three random numbers in an array		int a = 0, minNum = 50;				while(a < randNums.length) { //Runs while a is less than the array length						if(randNums[a] < minNum) { //Checks if randomNumber in array is less than the current minimum number				minNum = randNums[a]; //And sets it equal to that number if it is lower			}						a++;		}				//Prints out the results		System.out.println("Generated numbers: " + randNums[0] + ", " + randNums[1] + ", " + randNums[2]);		System.out.println("Lowest number: " + minNum);	}}
Link to comment
Share on other sites

Link to post
Share on other sites

This is my solution :D

import java.util.Random;	public class what{	public static void main(String[] args){  				Random r = new Random();		int min = r.nextInt(51); //Generates between 0 and 50		System.out.print("Generated numbers: " + min);		for(int i = 0; i < 2; ++i){			int n = r.nextInt(51); //Generates between 0 and 50			if(min > n){				min = n;			}			System.out.print(" " + n);		}		System.out.println('\n' + "Minimum of the numbers: " + min);	}}
Link to comment
Share on other sites

Link to post
Share on other sites

 

This is my solution :D

Ha, I like the way you find the smallest number! (but i think the OP abandoned the thread when they didn't get a reply in 20 minutes)

Link to comment
Share on other sites

Link to post
Share on other sites

If you're compiling in Java 8 this can be done in one line using the new Stream API.

import java.util.Random;public class RandomNumber {	public static void main(String[] args) {		System.out.println(new Random().ints(3, 0, 50).min().getAsInt());	}}

Basically what this does is spawn a random object and creates a stream of 3 OptionalInts ranging from 0 to 50, returning only the smallest of the 3. Finally I chose to convert it from an optional int to a normal integer for easier manipulation. The nice thing about this solution is it doesn't require you to do any comparison as it is all done by the int stream. The downside is that it only gives you the minimum number, although the solution can be modified to show all of the numbers.

There are 10 types of people in this world, those who can read binary and those who can't.

There are 10 types of people in this world, those who can read hexadecimal and F the rest.

~Fletch

Link to comment
Share on other sites

Link to post
Share on other sites

If you're compiling in Java 8 this can be done in one line using the new Stream API.

import java.util.Random;public class RandomNumber {	public static void main(String[] args) {		System.out.println(new Random().ints(3, 0, 50).min().getAsInt());	}}

Basically what this does is spawn a random object and creates a stream of 3 OptionalInts ranging from 0 to 50, returning only the smallest of the 3. Finally I chose to convert it from an optional int to a normal integer for easier manipulation. The nice thing about this solution is it doesn't require you to do any comparison as it is all done by the int stream. The downside is that it only gives you the minimum number, although the solution can be modified to show all of the numbers.

This is genius Oo

 

Sadly I didn't learn Java 8 so I'll see into it on my own. How is the performance though?

It was LOVE that purchased this traitors heart!

Link to comment
Share on other sites

Link to post
Share on other sites

This is genius Oo

 

Sadly I didn't learn Java 8 so I'll see into it on my own. How is the performance though?

 

To be honest I'm not quite sure what the performance improvements of the new stream API are as I have only been looking at it for about a week now. For simple operations as this I don't think its about the performance, but as for manipulation large amounts of BigData streams are probably faster, especially because you can now run Parallel Stream side by side. Streams can be finite, or infinite for example the following program will continously output a random integer between 0 and 50 until you stop the programs execution:

import java.util.Random;import static java.lang.System.out;public class RandomNumber {	public static void main(String[] args) {		new Random().ints(0, 50).forEach(out::println);	}}

I found this interesting article regarding the performance of stream in Java 8: http://java.dzone.com/articles/java-8-streams-api-laziness

 

This nifty article talks about the improvements of Stream vs Collections API https://weblogs.java.net/blog/manningpubs/archive/2013/12/04/streams-vs-collections-what%E2%80%99s-difference-java-8-lambdas-action-45-savings

 

Essentially all of the manipulation is done within the stream before the stream is closed. This allows you to modify the data while it loads for example sorting it on demand. Take a look at the following snippit of code which generates 50 random integers into a Stream and then it orders them while they are loaded.

import java.util.Random;import static java.lang.System.out;public class RandomNumber {	public static void main(String[] args) {		new Random().ints(50, 0, 50).sorted().forEach(out::println);	}}

All of the iteration is done within the stream. Perhaps this isn't the best example but for example if you are loading many dates from something like a database, you could sort them based off a comparison method which you define. They are then loaded in the order which you define and you can then iterate over them while the stream is open.

 

In terms of performance you might also want to look into Concurrency. Here is an article written by oracle on parallelism http://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

 

Hope this helps a bit.

There are 10 types of people in this world, those who can read binary and those who can't.

There are 10 types of people in this world, those who can read hexadecimal and F the rest.

~Fletch

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×