他们的区别在于对于空格的处理方式不同,以及返回值不同
nextLine():
不将空格看作是两个字符串的间隔,而是看作字符串的一部分,返回时,作为String类型一起返回
next():
将空格看作是两个字符串的间隔
nextInt():
与next()方法类似,只是他的返回值是int类型,将空格看作是两个输入的数据的间隔
nextInt()
Scanner之nextInt():
Random之nextInt():
Random类,用于生成随机数字,该类的nextInt(int n)函数表示随机生成0~n之间的整数
代码:
package org.xiaowu.random.demo;
import java.util.Random;
import org.junit.Test;
public class RandomDemo {
@Test
public void Demo(){
Random rnd = new Random();
int code = rnd.nextInt(8999) + 1000;
System.out.println("code:"+code);
}
@Test
public void Demo1(){
Random r = new Random();
int nextInt = r.nextInt();
Random r1 = new Random(10);
int nextInt2 = r1.nextInt();
System.out.println("nextInt:"+nextInt);
System.out.println("nextInt2:"+nextInt2);
}
/**
* 生成[0,1.0)区间的小数
*
*/
@Test
public void Demo2(){
Random r = new Random();
double d1 = r.nextDouble();
System.out.println("d1:"+d1);
}
/**
* 生成[0,5.0)区间的小数
*
*/
@Test
public void Demo3(){
Random r = new Random();
double d2 = r.nextDouble()* 5;
System.out.println("d1:"+d2);
}
/**
* 生成[1,2.5)区间的小数
*
*/
@Test
public void Demo4(){
Random r = new Random();
double d3 = r.nextDouble() * 1.5 + 1;
System.out.println("d1:"+d3);
}
/**
* 生成任意整数
*
*/
@Test
public void Demo5(){
Random r = new Random();
int n1 = r.nextInt();
System.out.println("d1:"+n1);
}
/**
* 生成[0,10)区间的整数
*
*/
@Test
public void Demo6(){
Random r = new Random();
int n2 = r.nextInt(10);
int n3 = Math.abs(r.nextInt() % 10);
System.out.println("n2:"+n2);
System.out.println("n3:"+n3);
}
/**
* 生成[0,10]区间的整数
*
*/
@Test
public void Demo7(){
Random r = new Random();
int n3 = r.nextInt(11);
int n4 = Math.abs(r.nextInt() % 11);
System.out.println("n3:"+n3);
System.out.println("n4:"+n4);
}
/**
* 生成[-3,15)区间的整数
*
*/
@Test
public void Demo8(){
Random r = new Random();
int n4 = r.nextInt(18) - 3;
int n5 = Math.abs(r.nextInt() % 18) - 3;
System.out.println("n4:"+n4);
System.out.println("n5:"+n5);
}
}