Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can someone help me with this exercise? Write a program that generates 100 random numbers and stores each number into an integer array. For this program, you must meet the following technical requirements: 1) In main, generate the 100 random numbers and store each in an array 2) In main, ask the user to enter a number to see if it exists in the array 3) Write a function that takes the array and user number as input parameters and returns either true or false if the number does or does not exist in the array respectively 4) In main, call this function appropriately and use the result to print out to the user if the number they provided is in the array
This is an extremely easy problem, and I will walk you through the steps, but not give you all the code because I know what it is like to be a beginner, and I learn visually by reading code.
You need an import for this, and that import is java.util.Random
You need an array to store it in, create an array with int[] and set it to a size of 100
You need a random object Random rand = new Random()
To generate 100 random integers and add them to an array you need to loop 100 times. Within that loop generate a number and add it to the array at index i
To check if a user inputted key exists once again loop through the array until a arr[i] == key, if it never finds a match it does not contain the number
If you have questions please leave a comment and I will try to be helpful
Use the java.util.Random class to generate random numbers like so:
Random randomGenerator = new Random();
int randomLessThan100 = randomGenerator.nextInt(100);
int randomLessThan1000 = randomGenerator.nextInt(1000);
int randomBetween50And100 = randomGenerator.nextInt(49) + 51;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have scenario where in an text box i need to enter value like : 234-89. Numbers need to randomly generated each time i run the test, "-" should be added after 3 numbers and rest two numbers should also be random. How can i do it ?
Generate two random numbers, the first one 3 digits, the second one 2 digits. Then concatenate both variables with a hyphen in between them.
If you need help generating random numbers, see this link https://www.educative.io/edpresso/how-to-generate-random-numbers-in-java
there are many options to generate random numbers in Java
Option -1
JavaFaker's fake data classes in java
or Option 2-
import java.util.Random;
Random rand = new Random();
// Generate 3digit random integers in range 0 to 999
int rand1 = rand.nextInt(1000);
// Generate 2digit random integers in range 0 to 99
int rand2 = rand.nextInt(100);
String textfortexbox=Integer.toString(rand1)+"-"+Integer.toString(rand2);
Output 999-99
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm a beginner at java and I've been trying to make a calculator using Swing. So far everything is good, but I'm having trouble with how the number pad will work. I want the calculator's output to be a float value, but I want it to be so that if you click "1", the output will display "1", not "1.00". How could I go about this?
Also, I cannot think of a way to append a number to another number. For example, if I input 1 then input 2, the output would be 1.02, not 12. How do I get the program to make the output be a whole number when possible?
For the first question: You first want to check that there are no digits after the decimal point when just displaying 1 instead of 1.00. To do so you can check that this actually is the case with this if statement:
if(number % 1.0f == 0)
next, you want to convert this number into a string that just displays the number without the decimal points, to do so you can cast number to an int and then convert it into a string with:
String.valueOf((int) number)
For the second question: To append a new digit to the number I would suggest generating a string with the number the user enters using StringBuilder. In practice it would look something like:
StringBuilder sb = new StringBuilder();
// when user presses "2" button for example:
sb.append("2");
In the end you want to convert this generated String into a float in order to perform calculations on it, which you can do with:
float value = Float.valueOf(sb.toString());
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So lets say I want to randomly choose from the set of two numbers 1 and 3.
How do I go about doing that? Do I just assign int a to 1, int b to 3, and then do randomly select from a and b?
If there are only two numbers to choose from then you can use the value of a boolean, because it returns either true or false.
One-liner solution assuming that int a = 1 and int b = 3:
int randomOfTwoInts = new Random().nextBoolean() ? a : b;
If you have a specific list of numbers then put them in list structure (an array works well). Then you have the easier task of looking for a random index in the range from 0 to last array index. This post lists strategies for doing that:How do I generate random integers within a specific range in Java?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to generate random numbers . Now, I want to generate a new set of random numbers in a way that that increases the probability of returning the numbers that were generated earlier.What is the best way of achieving this ?
I dont know if predefined functions exist or not, but I have something in mind that you can try.
Make an ArrayList of the numbers. (eg. if you want to generate from
0to30, then make an ArrayList of Integers from 0 to 30).
Shuffle the ArrayList(Collections.shuffle())
Pick the first 2 numbers.
Then, just add those 2 numbers into the ArrayList as many times as you want the probablity to shift towards them.
Shuffle again and pick
You can store that values on array.
And you can have a random condition also for having a new pair of number or pick randomly on your array.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to solve the following problem:
-find flowchart or algorithm and show it in java languange
"Write a Java program that prompts the user to input a four-digit positive integers between 1001 and 9999. The program then finds the reverse of that integers. For example, if the input integer i2 3245, its reverse is 5423."
My problem is that I don't know what formula to use. I have also asked my demonstrator, but he just said that the formula uses a percentage and divide. How should I approach a solution to this problem?
Since this is a learning assignment, I will give you only hints:
To get the last digit in base N, use x % N; for base ten, that would be x % 10
To drop the last digit in base N, integer-divide by N; for base ten, that would be x /= 10
Repeating this process four times and printing the digits in reverse order will give you the desired result. Since you know that the value has exactly four digits, you do not need a loop.
This might not be what the teacher accepts/wants/expects, but for educational purposes, this would be the easiest way to do it:
String input = "1234";
int result = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println(result):
prints
4321