一:
基本输入输出:
1 import java.util.Scanner; //导入扫描仪 2 public class Test1{ 3 public static void main(String[] args){ 4 Scanner in=new Scanner(System.in); //声明扫描仪并开辟空间 5 System.out.println("请输入姓名:"); 6 String name=in.next(); //接收字符串 7 System.out.println("请输入年龄:"); 8 int age=in.nextInt(); 9 System.out.println("姓名:"+name); 10 System.out.println("年龄:"+age); 11 } 12 } 13 /* 14 in.nextInt()方法用来接收int型数据 15 in.nextDouble()方法用来接收double型数据 16 in.next()方法用来接收字符串 17 注意没有in.nextChar()方法 18 */
注意:import java.util.*;可以把java.util包中的所有内容都导入
二 生成随机数 /*生成随机数 //代码一: import java.util.Random; public class random{ public static void main(String[] args){ Random xx=new Random(); int number=xx.nextInt(10); //生成0到9的随机整数 System.out.println("随机数为:"+number); } } */ //代码二:---支持多线程 public class random{ public static void main(String[] args){ //生成0到9的随机数,在强制类型转换之前生成的是0到10的double型数据 int number=(int)(Math.random()*10); System.out.println(number); } }