JAVAutil。随机的Java中的nextInt()

生成随机数本身有很好的实用价值,通过使用函数实现随机数是非常有用的。Java在其语言中有专门的 整个图书馆都是随机数 看到它在日常编程中的重要性。本文将讨论nextInt()。

null
  1. JAVAutil。随机的nextInt(): 这个 nextInt() 用于从该随机数生成器的序列中获取下一个随机整数值。
    Declaration : 
    public int nextInt()
    Parameters : 
    NA
    Return Value : 
    The method call returns the next integer number from the sequence
    Exception : 
    NA
    

    下面的示例显示了java的用法。util。随机的nextInt()

    // Java code to demonstrate the working
    // of nextInt()
    import java.util.*;
    public class NextInt1 {
    public static void main(String[] args)
    {
    // create random object
    Random ran = new Random();
    // generating integer
    int nxt = ran.nextInt();
    // Printing the random Number
    System.out.println
    ( "The Randomly generated integer is : " + nxt);
    }
    }

    
    

    输出:

    The Randomly generated integer is : -2052834321
    
  2. JAVAutil。随机的nextInt(国际北) 当前位置 nextInt(国际北) 用于获取介于0(包含)和在此参数(n)中传递的数字(独占)之间的随机数。
    Declaration : 
    public int nextInt(int n)
    Parameters : 
    n :  This is the bound on the random number to be returned. Must be positive.
    Return Value : 
    Returns a random number.
    between 0 (inclusive) and n (exclusive).
    Exception : 
    IllegalArgumentException :  This is thrown if n is not positive.
    

    下面的示例显示了java的用法。util。随机的nextInt(国际北)

    // Java code to demonstrate the working
    // of nextInt(n)
    import java.util.*;
    public class NextInt2 {
    public static void main(String args[])
    {
    // create random object
    Random ran = new Random();
    // Print next int value
    // Returns number between 0-9
    int nxt = ran.nextInt( 10 );
    // Printing the random number
    // between 0 and 9
    System.out.println
    ( "Random number between 0 and 10 is : " + nxt);
    }
    }

    
    

    输出:

    Random number between 0 and 9 is : 4
    
例外

IllegalArgumentException: 当传递的参数不是正数时会发生这种情况。 举例说明n不是正数时产生的异常:

// Java code to demonstrate the Exception
// of nextInt(n)
import java.util.*;
public class NextInt2 {
public static void main(String[] args)
{
// create random object
Random ran = new Random();
// generating number between 0 and -12345
// Raises Runtime error, as n is negative.
int nxt = ran.nextInt(- 12345 );
System.out.println
( "Generated Random number is : " + nxt);
}
}


运行时错误:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Random.java:388)
    at NextInt2.main(NextInt2.java:14)

实际应用

生成随机数有很多应用,例如 它包括彩票、赌博或小规模赌博 下面演示了一个小骰子游戏,其中两名玩家掷6分骰子,第一名获得30分者获胜。

// Java code to demonstrate the Application
// of nextInt(n)
import java.util.*;
public class NextIntAppli {
public static void main(String[] args)
{
int sum = 0 , sum1 = 0 , count = 0 , count1 = 0 ;
int turn = 0 ;
// creating random object
Random ran = new Random();
int flag = 0 ;
while ( true ) {
if (turn % 2 == 0 ) {
int p1 = ran.nextInt( 6 );
sum += p1;
System.out.printf
( "Player 1 after turn %d is : %d" , turn, sum);
}
else {
int p2 = ran.nextInt( 6 );
sum1 += p2;
System.out.printf
( "Player 2 after turn %d is : %d" , turn, sum1);
}
if (sum >= 30 ) {
flag = 1 ;
break ;
}
if (sum1 >= 30 ) {
flag = 2 ;
break ;
}
turn++;
}
if (flag == 1 )
System.out.println( "Player 1 WON!!" );
else
System.out.println( "Player 2 WON!!" );
}
}


输出:

Player 1 after turn 0 is : 0
Player 2 after turn 1 is : 4
Player 1 after turn 2 is : 2
Player 2 after turn 3 is : 9
Player 1 after turn 4 is : 5
Player 2 after turn 5 is : 9
Player 1 after turn 6 is : 6
Player 2 after turn 7 is : 14
Player 1 after turn 8 is : 8
Player 2 after turn 9 is : 18
Player 1 after turn 10 is : 12
Player 2 after turn 11 is : 21
Player 1 after turn 12 is : 13
Player 2 after turn 13 is : 26
Player 1 after turn 14 is : 18
Player 2 after turn 15 is : 29
Player 1 after turn 16 is : 18
Player 2 after turn 17 is : 34
Player 2 WON!!

本文由 苏曼·辛格·拉吉普特 .如果你喜欢GeekSforgek并想投稿,你也可以使用“投稿”撰写文章。极客。组织或邮寄你的文章到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享