public class arryTest4 { //对整型数组按照由小到大的顺序进行排序 public static void main(String[] args) { int score[]= {67,89,87,69,90,100,75,90}; for(int i=1;i<score.length;i++) { for(int j=0;j<score.length;j++) { if(score[i]<score[j]) { int temp = score[i]; score[i] = score[j]; score[j] = temp; } } } for (int i=0;i<score.length;i++) { System.out.println(score[i]+"\t"); } } }
Scanner sc=new Scanner(System.in);
意思是:通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner,作为扫描对象。
要获取输入的内容,则只需要调用Scanner的nextLine()方法
举例:
public class TestScanner {
public static void main(String[] args) { //定义main方法
Scanner s = new Scanner(System.in); //定义scanner,等待输入
System.out.println("请输入字符串:");
while (true) {
String line = s.nextLine(); //读取输入内容
if (line.equals("exit")) break; //如果读取到exit,则退出输入
System.out.println(">>>" + line); //打印输入内容
}
}
}
public class arryTest3 { public static void main(String[] args) { int score[] = {67,89,87, 69,90,100,75,90}; int max=0; int min=0; max=min=score[0]; for(int x=0;x<score.length;x++) { if(score[x]>max) { max=score[x]; } if(score[x]<min) { min=score[x]; } } System.out.println("最高成绩:"+max); System.out.println("最低成绩:"+min); } }
public class arryTest { public static void main(String[] args) { //学生成绩管理系统 int student;//控制学生的变量 double sum=0,avg=0;//总成绩和平均成绩 double[] temp=new double[10]; //创建一个Scanner类的对象,用它来获得用户的输入 Scanner sc=new Scanner(System.in); System.out.println("请输入前10名的成绩"); for(student=0;student<temp.length;student++) { //读取用户的输入 temp[student]=sc.nextDouble(); sum+=temp[student]; } avg=sum/10; System.out.println("平均值是:"+avg); for(student=0;student<temp.length;student++) { if(temp[student]<avg) { System.out.println(temp[student]+"该学生的成绩低于平均成绩"); }else if(temp[student]>avg) { System.out.println(temp[student]+"该学生的成绩高于平均成绩"); }else { System.out.println(temp[student]+"该学生的成绩等于平均成绩"); } } } }
public class TestArr {
public static void main(String[] args) {
//声明数组
double array1[];
double[] array2;
double[] array3,array4,array5;
//分配内存空间
array1=new double[5];
array1[0]=0;
array1[1]=1;
array1[2]=3;
array1[3]=23;
System.out.println(array1[3]);
}
}
public class testWhile { public static void main(String[] args) { int i=1; while(i<=10) {//括号里写true变成死循环 System.out.println("while第"+i+"次循环"); i++; } } }