1 import java.io.*; 2 import java.util.Arrays; 3 import java.util.InputMismatchException; 4 import java.util.Scanner; 5 6 public class AverageScore { 7 public static void main(String[] args) throws IOException { 8 // System.out.println("请输入5位同学的成绩:"); 9 Scanner input=new Scanner(System.in); 10 // String []arr=new String[5]; 11 // for(int i=0;i<arr.length;i++){ 12 // arr[i]=input.next(); 13 // } 14 // System.out.println("5位同学的成绩为:"); 15 // for (int i = 0; i < arr.length; i++) { 16 // System.out.print(arr[i]+","); 17 // } 18 FileOutputStream fos=new FileOutputStream("\\shiyan10\\data.txt"); 19 FileInputStream fis=new FileInputStream("\\shiyan10\\data.txt"); 20 int flag=0;//用于判断是否执行后续代码 21 try { 22 for(int i=1;i<=5;i++){ 23 System.out.println("请输入第"+i+"个学生的成绩:"); 24 int s=input.nextInt(); 25 if(s<0||s>100){ 26 throw new Exception("不是0-100的数"); 27 } 28 fos.write(s); 29 flag=1; 30 } 31 } catch (InputMismatchException e) { 32 System.out.println("不是整数"); 33 }catch (Exception e){ 34 System.out.println("不是0-100的数"); 35 } 36 if(flag==1){ 37 int []arr=new int[5]; 38 int all=0;//5位同学成绩总和 39 for (int j = 0; j < arr.length; j++) { 40 arr[j]=fis.read(); 41 all+=arr[j]; 42 } 43 System.out.println("5位学生的平均数为:"+all/arr.length); 44 Arrays.sort(arr);//把数组元素从小到大排序的便捷方法 45 System.out.println("5位学生从小到大的排序为:"); 46 for (int k = 0; k < arr.length; k++) { 47 System.out.print(arr[k]+",");//输出排序结果 48 } 49 } 50 51 fis.close(); 52 fos.close(); 53 } 54 }
-------------------------------------------------------------------------------------------------------------------------------------------
1 import java.io.FileNotFoundException; 2 import java.io.FileOutputStream; 3 import java.io.FileWriter; 4 import java.io.IOException; 5 6 public class Equilateral_Triangle { 7 public static void main(String[] args) throws IOException { 8 //输出了一个边长为3的非倒立等边三角形 9 10 FileWriter fos=new FileWriter("\\shiyan10\\triangle.txt"); 11 for(int i=0;i<4;i++){ 12 for(int j=0;j<4-i;j++){ 13 System.out.print(" "); 14 fos.write(" "); 15 } 16 for(int j=0;j<i;j++) { 17 System.out.print(" *"); 18 fos.write(" *"); 19 } 20 System.out.println(); 21 } 22 fos.close(); 23 } 24 }
-------------------------------------------------------------------------------------------------------------------------------------------
标签:arr,java,int,System,实验,import,Java,out From: https://www.cnblogs.com/dy53/p/17376011.html