JAVA基础实验

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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位置开始排放
The First Experiment — Java programming basic
Q1. Install, configure and test JDK, then output “hello world” in the command window. Install,configure and test JDK follow the step on the PPT. Write a simple Java source code, compile, interpret and run it. Type javac test.java in command line. Type java test to see the result. This process is called as Interpreting and running.
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;
public class year { public static void main(String[] args) { Scanner input=new Scanner(System.in); for(int i=0;;i++){ System.out.println("请输入年份"); int nian=input.nextInt(); if (nian%4!=0||nian%100==0&&nian%400!=0){ System.out.println(nian+"是平年"); }else{ System.out.println(nian+"是闰年"); } } } }
A[i] = sc.nextDouble(); } catch(InputMismatchException e) { sc.next(); // 这里sc.next用于接收导致异常的 非double的输入 System.out.println("Nຫໍສະໝຸດ Baidu." + 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
Q8. Input five arbitrary integers from your keyboard, and output them from smallest to largest, note that only if structure can be employed in your program. I have no idea on sorting,so I take the documents given by Mr Pan as an reference.And so as the Q9. package java_Sort; import java.util.Scanner; public class exchange_Sort { public static void main(String[] args) { int i, j; int[] A = new int[5]; System.out.println("please input the numbers:"); Scanner sc = new Scanner(System.in); // 从键盘输入数据,为A数组赋值 for (i = 0; i < A.length; i++) { System.out.println("A[" + i + "]="); A[i] = sc.nextInt(); } sc.close(); // 采用冒泡排序法对A数组排序 for (i = 0; i < A.length - 1; i++) { for (j = i + 1; j < A.length; j++)
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"); }
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数组不相等"); } }
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;
} 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 + "] = " );
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 + "天"); } } }
相关文档
最新文档