java random number
Java
import java.util.Random;
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;import java.util.Random;
class scratch{
public static void main(String[] args) {
Random rand = new Random();
System.out.println( rand.nextInt(5) );
//prints a random Int between 0 - 4 (including 0 & 4);
}
}package com.example.guessthenumber;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int randomNumber;
public void generateRandomNumber(){
Random rand = new Random();
randomNumber = rand.nextInt(20) + 1;
}
public void GuessTheNumber(View view){
EditText numberET = (EditText) findViewById(R.id.numerEditText);
int guessValue = Integer.parseInt(numberET.getText().toString());
String message;
if(guessValue > randomNumber)
{
message = "Lower!";
}
else if (guessValue < randomNumber)
{
message = "Higher!";
}
else
{
message = "Good Guess!!! Try Again";
generateRandomNumber();
}
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
Log.i("Entered Text", numberET.getText().toString());
Log.i("Random Number", Integer.toString(randomNumber));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateRandomNumber();
}
}
public class HelloWorld {
public static void main(String[] args) {
// triangular number - first 10
int x = 1;
int triangularNumber = 1;
while(x < 10)
{
System.out.println(triangularNumber);
x++;
triangularNumber = triangularNumber + x;
}
}
}
int rand = ThreadLocalRandom.current().nextInt(x,y);public class GuessTheNumber {
public static void main(String[] args) {
int randomInt = (int)(20.0 * Math.random());
int num1 = randomInt;
int num2 = 10;
if(num1 == num2){
System.out.println("Correct!");
} else if(num1 > num2){
System.out.println("Too low!");
} else{
System.out.println("Too high!");
}
}
}
Also in Java:
- Title
- java android development get element by id
- Category
- Java
- Title
- iterate map in java 8 using stream
- Category
- Java
- Title
- value receive null with post method in the java spring controller
- Category
- Java
- Title
- get long from date java
- Category
- Java
- Title
- how to sort collection in java
- Category
- Java
- Title
- java packages example
- Category
- Java
- Title
- javafx textarea size
- Category
- Java
- Title
- how to split a string in java
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- java stream sort Collator
- Category
- Java
- Title
- number to string java
- Category
- Java
- Title
- write an object java in a file
- Category
- Java
- Title
- java hashmap put
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- Java script code for add
- Category
- Java
- Title
- kotlin enable and disable parents view children
- Category
- Java
- Title
- int to binary java
- Category
- Java
- Title
- java resultset to table
- Category
- Java
- Title
- how to stop a void java
- Category
- Java
- Title
- java uuid
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- import java.util.*;
- Category
- Java
- Title
- find the day of the week Calendar class in java
- Category
- Java
- Title
- java fileinputstream
- Category
- Java
- Title
- how to copy all files and subdirectories in directory in java
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- spigot sounds
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- separateur JMenu swing java
- Category
- Java
- Title
- arraylist add method
- Category
- Java
- Title
- how to create an array list in java
- Category
- Java
- Title
- java string array to arraylist
- Category
- Java
- Title
- how to exit a java program
- Category
- Java
- Title
- string to double java
- Category
- Java
- Title
- print a letter in java
- Category
- Java
- Title
- 3-way radix quicksort java
- Category
- Java
- Title
- joptionpane fonctionnement java
- Category
- Java
- Title
- java call another constructor
- Category
- Java
- Title
- getordefault java
- Category
- Java
- Title
- how to make stage dragable in javafx
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- show menuitem when menu hovered javafx
- Category
- Java
- Title
- java convert string with commas to long
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- creating thread in java example
- Category
- Java
- Title
- java getter
- Category
- Java
- Title
- check java version
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- spring code in java
- Category
- Java
- Title
- java int stream min
- Category
- Java
- Title
- change fragment in android studio
- Category
- Java
- Title
- check last character of string java
- Category
- Java
- Title
- saving String character in arraylist
- Category
- Java
- Title
- how to compare two maps in java
- Category
- Java
- Title
- convert array of int to arraylist java
- Category
- Java
- Title
- java 8 iterating and manipulating list
- Category
- Java
- Title
- java cannot find file path
- Category
- Java
- Title
- arraylist to array java
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- spring data elasticsearch aggregation max
- Category
- Java
- Title
- list java oracle
- Category
- Java
- Title
- how to write a java for loop?
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- how to set a variable java
- Category
- Java
- Title
- close searchview android java
- Category
- Java
- Title
- labeled for loop in java
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- how to pass enum in postman body
- Category
- Java
- Title
- properties java 8 maven in pom xml
- Category
- Java
- Title
- java list as parameter
- Category
- Java
- Title
- filter and map multiple fields from java stream
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- Caused by: java.lang.NumberFormatException: Invalid int: ""
- Category
- Java
- Title
- java variable declaration
- Category
- Java
- Title
- how to remove a certain string in a arraylist java
- Category
- Java
- Title
- android studio java random number generator
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- java math.random
- Category
- Java
- Title
- java mockito print called methods
- Category
- Java
- Title
- how to make a list java
- Category
- Java
- Title
- Java how to handle HTTP GET request after establishing TCP connection
- Category
- Java
- Title
- processing pi
- Category
- Java
- Title
- java array swap
- Category
- Java
- Title
- how to declare array java
- Category
- Java
- Title
- Could not resolve project :app
- Category
- Java
- Title
- generate random number java
- Category
- Java
- Title
- java optional to collection
- Category
- Java
- Title
- JavaFX onMouseEntered
- Category
- Java
- Title
- compare strings in java
- Category
- Java
- Title
- how to make a copy of an array java
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- what is primitive data type in java
- Category
- Java
- Title
- change java version command line debian
- Category
- Java
- Title
- how to close a jframe in java with an if statement
- Category
- Java
- Title
- java string to integer
- Category
- Java
- Title
- how to create an action listener in java
- Category
- Java
- Title
- java 8 map foreach
- Category
- Java
- Title
- clear stringbuilder java
- Category
- Java