logo

Java Random nextInt() שיטה

שיטת nextInt() של המחלקה Random מחזירה את ערך int הפסאודורנדום הבא, המופץ באופן אחיד מרצף מחולל המספרים האקראיים.

תחביר

 public int nextInt() 

פרמטרים

זֶה

Switch case Java

ערך החזרה

השיטה nextInt() מחזירה את ערך int הפסאודו-אקראי הבא שנלקח מהרצף של מחולל המספרים האקראיים.

דוגמה 1

 import java.util.Random; public class JavaRandomNextIntExample1 { public static void main(String[] args) { Random random = new Random(); //return the next pseudorandom integer value Integer val = random.nextInt(); System.out.println('Random Integer value : '+val); Integer val1 = random.nextInt(); System.out.println('Random Integer value : '+val1); } } 
בדוק את זה עכשיו

תְפוּקָה:

 Random Integer value : -298063488 Random Integer value : 1400961289 

Java Random nextInt() שיטה

שיטת nextInt(int n) של מחלקה אקראית מחזירה ערך int פסאודו-אקראי בין אפס (כולל) לערך שצוין (בלעדי), שנמשך מרצף מחולל המספרים האקראיים.

תחביר

 public int nextInt(int n) 

פרמטרים

נ : זהו הגבול על המספר האקראי שיש להחזיר. זה חייב להיות חיובי.

שיטות מחרוזת

ערך החזרה

השיטה nextInt() מחזירה את ערך int הפסאודו-אקראי הבא בין אפס ל-n שנמשך מהרצף של מחולל המספרים האקראיים.

לשבור את ג'אווה

זורק

המתודה nextInt() זורקת את IllegalArgumentException, אם n אינו חיובי.

דוגמה 2

 import java.util.Random; public class JavaRandomNextIntExample2 { static int j=1; public static void main(String[] args) { Random random = new Random(); for (int i=1;i<8;i++) { return the next pseudorandom integer value val="random.nextInt(i);" system.out.println(j++ +'. random intvalue : '+val+' '); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1. Random intvalue : 0 2. Random intvalue : 1 3. Random intvalue : 0 4. Random intvalue : 3 5. Random intvalue : 1 6. Random intvalue : 0 7. Random intvalue : 0 </pre> <h2>Example 3</h2> <pre> import java.util.Random; import java.util.Scanner; import java.util.concurrent.ArrayBlockingQueue; public class JavaRandomNextIntExample3 { static int j=1; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&apos;Enter number of players.&apos;); int capacity = scanner.nextInt(); ArrayBlockingQueue queue = new ArrayBlockingQueue(capacity); System.out.println(&apos;Enter name of &apos; + capacity + &apos; Players.&apos;); for (int i = 0; i <capacity; i++) { system.out.print(j++ + ' '); string str="scanner.next();" queue.add(str); } j="1;" random random(); for (string xyz : queue) integer val="random.nextInt(capacity);" system.out.println(j++ +'. +xyz+' will have '+val+' bonus points.'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Enter number of players.3 Enter name of 3 Players. 1 Sukla 2 Mukta 3 Amar 1. Sukla will have 0 bonus points. 2. Mukta will have 1 bonus points. 3. Amar will have 2 bonus points. </pre> <h2>Example 4</h2> <pre> import java.util.Random; public class JavaRandomNextIntExample4 { public static void main(String[] args) { Random random = new Random(); //return the next pseudorandom integer value Integer val = random.nextInt(0); System.out.println(&apos;Random Integer value : &apos;+val); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Exception in thread &apos;main&apos; java.lang.IllegalArgumentException: bound must be positive atjava.util.Random.nextInt(Random.java:388) at com.javaTpoint.JavaRandomNextIntExample4.main(JavaRandomNextIntExample4.java:9) </pre> <h4>Note: The bound value must be positive else it will give IllegalArgumentException.</h4></capacity;></pre></8;i++)>

דוגמה 3

 import java.util.Random; import java.util.Scanner; import java.util.concurrent.ArrayBlockingQueue; public class JavaRandomNextIntExample3 { static int j=1; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&apos;Enter number of players.&apos;); int capacity = scanner.nextInt(); ArrayBlockingQueue queue = new ArrayBlockingQueue(capacity); System.out.println(&apos;Enter name of &apos; + capacity + &apos; Players.&apos;); for (int i = 0; i <capacity; i++) { system.out.print(j++ + \' \'); string str="scanner.next();" queue.add(str); } j="1;" random random(); for (string xyz : queue) integer val="random.nextInt(capacity);" system.out.println(j++ +\'. +xyz+\' will have \'+val+\' bonus points.\'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Enter number of players.3 Enter name of 3 Players. 1 Sukla 2 Mukta 3 Amar 1. Sukla will have 0 bonus points. 2. Mukta will have 1 bonus points. 3. Amar will have 2 bonus points. </pre> <h2>Example 4</h2> <pre> import java.util.Random; public class JavaRandomNextIntExample4 { public static void main(String[] args) { Random random = new Random(); //return the next pseudorandom integer value Integer val = random.nextInt(0); System.out.println(&apos;Random Integer value : &apos;+val); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Exception in thread &apos;main&apos; java.lang.IllegalArgumentException: bound must be positive atjava.util.Random.nextInt(Random.java:388) at com.javaTpoint.JavaRandomNextIntExample4.main(JavaRandomNextIntExample4.java:9) </pre> <h4>Note: The bound value must be positive else it will give IllegalArgumentException.</h4></capacity;>

דוגמה 4

 import java.util.Random; public class JavaRandomNextIntExample4 { public static void main(String[] args) { Random random = new Random(); //return the next pseudorandom integer value Integer val = random.nextInt(0); System.out.println(&apos;Random Integer value : &apos;+val); } } 
בדוק את זה עכשיו

תְפוּקָה:

 Exception in thread &apos;main&apos; java.lang.IllegalArgumentException: bound must be positive atjava.util.Random.nextInt(Random.java:388) at com.javaTpoint.JavaRandomNextIntExample4.main(JavaRandomNextIntExample4.java:9) 

הערה: הערך המחובר חייב להיות חיובי, אחרת הוא ייתן את IllegalArgumentException.