java学习(1)
随机数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.util.Random; public class Root1{ public static void main(String[] args) { int[] arr={1,2,3,4,5}; Random r=new Random(); for(int i=0; i< arr.length ;i++) { int index=r.nextInt(arr.length); int temp= arr[i]; arr[i]=arr[index]; arr[index]=temp; }
for(int i=0; i<arr.length;i++) { System.out.print(arr[i]+" "); } } }
|
输出
scanf读入+方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.util.Scanner;
public class Root2 { public static void main(String[] args) { System.out.println(getSum1(1, 2)); Scanner sc=new Scanner(System.in); int put=sc.nextInt(); System.out.println(put); sc.close(); } public static void getSum(int num1,int num2) { int result=num1+num2; System.out.println(result); return; } public static int getSum1(int num1,int num2) { int result=num1+num2; return result; } }
|
输入
输出
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class Root3 { public static void main(String[] args) { Girlfriend gf1=new Girlfriend(); gf1.name="神里绫华"; gf1.age=21; gf1.gender="神里家的大小姐"; System.out.println(gf1.name+" "+gf1.age+" "+gf1.gender); gf1.eat(); gf1.sleep(); } } class Girlfriend { String name; int age; String gender; public void sleep() { System.out.println("女朋友在睡觉"); } public void eat() { System.out.println("女朋友在吃饭"); } }
|
输出
1 2 3
| 神里绫华 21 神里家的大小姐 女朋友在吃饭 女朋友在睡觉
|