generate random number java
Java
// JAVA - WITH A RANGE
(Math.random() * ((max - min) + 1)) + minimport 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);
}
}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!");
}
}
}int rand = ThreadLocalRandom.current().nextInt(x,y);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();
}
}
Also in Java:
- Title
- java function without return
- Category
- Java
- Title
- firebase connecten
- Category
- Java
- Title
- org.mapstruct maven dependency
- Category
- Java
- Title
- java xml element get attribute value
- Category
- Java
- Title
- rgb to hex java
- Category
- Java
- Title
- padding a string with 0 in java
- Category
- Java
- Title
- set html text android java
- Category
- Java
- Title
- how to declare an array in java
- Category
- Java
- Title
- java stream collect to string
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- binary search java
- Category
- Java
- Title
- how to create a subclass in java
- Category
- Java
- Title
- editer un label java
- Category
- Java
- Title
- java decler variabel
- Category
- Java
- Title
- iterate over map keys java
- Category
- Java
- Title
- java singleton
- Category
- Java
- Title
- spigot cancel repeating task
- Category
- Java
- Title
- convert string into unicode java
- Category
- Java
- Title
- java for each loop
- Category
- Java
- Title
- how to make a pre set list java
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- count word in string no matter the delimiter java
- Category
- Java
- Title
- how to install java 8 in ubuntu 16.04
- Category
- Java
- Title
- java script snippet for responsive
- Category
- Java
- Title
- java classes and methods
- Category
- Java
- Title
- edit a txt file using java
- Category
- Java
- Title
- sending a excel in an attachment in email java
- Category
- Java
- Title
- java get current date without time
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- android cardview dependency
- Category
- Java
- Title
- take a peice from array java
- Category
- Java
- Title
- java while loop break
- Category
- Java
- Title
- how to add element to arraylist
- Category
- Java
- Title
- basic java coding
- Category
- Java
- Title
- how to get token oauth2.0 java example response
- Category
- Java
- Title
- reentrantreadwritelock java
- Category
- Java
- Title
- java prev permutation
- Category
- Java
- Title
- java how to stop code
- Category
- Java
- Title
- java array to arraylist
- Category
- Java
- Title
- Spigot API inventory close
- Category
- Java
- Title
- Primitive Type vs. Reference Type
- Category
- Java
- Title
- lopp array java
- Category
- Java
- Title
- converter int array para string java
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- how to initialize an array in java
- Category
- Java
- Title
- switch java 11
- Category
- Java
- Title
- javadoc reference general toString
- Category
- Java
- Title
- java swing change label icon
- Category
- Java
- Title
- spigot custom join message
- Category
- Java
- Title
- string contains java
- Category
- Java
- Title
- make recycler view non scrollable
- Category
- Java
- Title
- bootstrap button bigger
- Category
- Java
- Title
- java class array of objects
- Category
- Java
- Title
- convert list of integer to array in java
- Category
- Java
- Title
- how to create an array list in java
- Category
- Java
- Title
- Java sort Map by values
- Category
- Java
- Title
- how to check if a string is in alphabetical order in java
- Category
- Java
- Title
- test function that call a function javascrip
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- for loop java
- Category
- Java
- Title
- java spring boot json deserialize date inaccurate
- Category
- Java
- Title
- how to get all of the chldren of a panel in java
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- spigot chat color with symbol
- Category
- Java
- Title
- java check if file exists
- Category
- Java
- Title
- D/NetworkSecurityConfig: No Network Security Config specified, using platform default
- Category
- Java
- Title
- string to double java
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- insertion sort doubly linked list java
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- how to check internet is working or not in java
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- spigot sounds
- Category
- Java
- Title
- java infinitew recursion
- Category
- Java
- Title
- for next loop javasxcrop
- Category
- Java
- Title
- how to fill a list with a single value java
- Category
- Java
- Title
- room insert and return id
- Category
- Java
- Title
- declare array java inline
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- how to do for each in java
- Category
- Java
- Title
- inputstream to string java
- Category
- Java
- Title
- spigot heal player
- Category
- Java
- Title
- how to create java jframe in eclipse
- Category
- Java
- Title
- how to open a webpage with java
- Category
- Java
- Title
- jbutton set background transparent
- Category
- Java
- Title
- java print type of object
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- déclarer un tableau en java
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- get today date in java 8
- Category
- Java
- Title
- imagesbutton how to set background
- Category
- Java
- Title
- how to replace all of one character with nothing in java
- Category
- Java
- Title
- how to collect objective in java
- Category
- Java
- Title
- how to draw a line in java
- Category
- Java
- Title
- java clone method
- Category
- Java
- Title
- array in line java
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- java log base 2
- Category
- Java
- Title
- split with scquere braket in java
- Category
- Java