Java入门笔记1(类和对象前)
java中使用输入函数
import java.util.Scanner
Scanner src=new Scanner(System.in)
输入两个数,使用方法返回两个数中的最大值
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b= sc.nextInt(); int c=getmax(a,b); System.out.println(c); } public static int getmax(int a1,int a2) { if(a1>a2) return a1; else return a2; } }
随机生成两个数字进行比较大小
import java.util.Random; public class Main { public static void main(String[] args) { Random sc=new Random( ); int a=sc.nextInt(10); int b= sc.nextInt(10); int c=getmax(a,b); System.out.println(c); } public static int getmax(int a1,int a2) { System.out.println(a1); System.out.println(a2); if(a1>a2) return a1; else return a2; } }
数组反转
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner src=new Scanner(System.in); int[] arr=new int[7]; for(int i=0;i<arr.length;i++) { arr[i]=src.nextInt(); } arr=reverse(arr); for(int j=0;j< arr.length;j++) { System.out.println(arr[j]); } } public static int[] reverse(int[] b1) { int a1=0; int a2= b1.length-1; while(a1<=a2) { int t=b1[a1]; b1[a1]=b1[a2]; b1[a2]=t; a1++; a2--; } return b1; } }