JAVA基础实验
- 格式:pdf
- 大小:828.50 KB
- 文档页数:27
Q7. Input a particular year and month from your keyboard, and then output how many days this month has. The algorithm used in this experiment is also making choices.Take there are about 30 days in a month into consideration, we can use switch here. package java_Daysjudge; import java.util.Scanner; public class Days_judge { public static void main(String[] args) { int days=0;
System.arraycopy(X, 1, W, 2, 4); System.out.println("复制的数组W:" + Arrays.toString(W)); // binarySearch方法返回某个元素在数组中的位置,要求数 组首先排序完毕 int pos = Arrays.binarySearch(X, 5); if(pos>0) System.out.println("5 出现在X的第" + pos + "个 位置"); // equals方法判断两个数组是否相等,如果数组长度和对应 位置的元素都相同,则 // 认为两个数组是相等的 if(Arrays.equals(Y, W)) System.out.println("Y数组和W数组相等"); else System.out.println("Y数组和W数组不相等"); } }
Q2. Install and use Eclipse to output “hello world”.
Q3. Output your Name and Student ID Number. This experiment just need me to make a little change on the second one. public class testb { public static void main(String[] args) { System.out.println("王梦迪 1341906103"); }
A[i] = sc.nextDouble(); } catch(InputMismatchException e) { sc.next(); // 这里sc.next用于接收导致异常的 非double的输入 System.out.println("No." + i + "'s input is error, please try again"); i--; //这里i--用于抵消for循环中的i++产生的 下标移位 } } sc.close(); for(double i:A) System.out.println(i + " "); } } Fig.4.1
பைடு நூலகம்
Fig.4.2
Q5. Read
ArraysUtil.java
carefully,
add
the
proper
comments in the program, and finally analyze the result with your own words. I have learned some operations on array. package java_ArraysUtil; import java.util.Arrays; import java.util.Scanner; public class ArraysUtil { public static void main(String[] args) { double [] X = new double [5]; System.out.println("please input the numbers:"); Scanner sc = new Scanner(System.in); for(int i=0;i<X.length;i++) X[i]=sc.nextDouble(); sc.close(); // sort方法可将数组按顺序排序 Arrays.sort(X); // toString方法将数组转为字符串格式,其中[]标记分别加 在数组的首端和末尾 System.out.println("排序后数组X:" + Arrays.toString(X)); // copyOfRange方法将数组中下标从from到to-1的元素复制 到另一个数组 double [] Y = Arrays.copyOfRange(X, 2, 4); System.out.println("复制的数组Y:" + Arrays.toString(Y)); // copyOf方法将数组复制成一个长度为newLength的数组, 如长度变小则采取 // 截断的方式,如长度变长则采取补0的方式 double [] Z = Arrays.copyOf(X, 10); System.out.println("复制的数组Z:" + Arrays.toString(Z)); double [] W = new double [10]; // arraycopy方法从数组下标src位置开始的length个元素复 制到另一个 // 数组中,复制的元素从dest位置开始排放
} Q4. Input four int items named Age, Math, English and History from your keyboard, give a prompt if any item’s input is not correct. That made me know something about the operation of output in Java. package java_input; import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入年龄、数学、英语和历史成 绩:"); int age = sc.nextInt(); int math = sc.nextInt(); int english = sc.nextInt(); int history = sc.nextInt(); sc.close(); System.out.println("年龄:" + age); System.out.println("数学:" + math); System.out.println("英语:" + english); System.out.println("历史:" + history); } } package java_input; import java.util.InputMismatchException; import java.util.Scanner; public class ScannerException { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double [] A = new double [5]; for(int i = 0; i < A.length; i++) { try { System.out.println("A[" + i + "] = " );
Q6. Input a particular year from your keyboard and check whether it is a leap year. The importance of this experiment is using a if-else to check the leap year. package java_leapyear; import java.util.Scanner;
Scanner input=new Scanner(System.in); for(int i=0;;i++){ System.out.println("请输入年份和月份"); int year=input.nextInt(); int month=input.nextInt(); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if ((year % 4 == 0) && (year % 100 != 0) || (year % 400) == 0) days = 29; else days = 28; break; default: System.out.println("年月有误,请检查"); } System.out.println(year + "年" + month + "月共有" + days + "天"); } } }