JAVA基础实验.doc_第1页
JAVA基础实验.doc_第2页
JAVA基础实验.doc_第3页
JAVA基础实验.doc_第4页
JAVA基础实验.doc_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

The First Experiment Java programming basicQ1. 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.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);Q4. Input four int items named Age, Math, English and History from your keyboard, give a prompt if any items 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 + = );Ai = 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.1Fig.4.2Q5. 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;i0)System.out.println(5 出现在X的第 + pos + 个位置);/ equals方法判断两个数组是否相等,如果数组长度和对应位置的元素都相同,则/ 认为两个数组是相等的if(Arrays.equals(Y, W)System.out.println(Y数组和W数组相等);elseSystem.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;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+是平年);elseSystem.out.println(nian+是闰年);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;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;elsedays = 28;break;default:System.out.println(年月有误,请检查);System.out.println(year + 年 + month + 月共有 + days + 天);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 int5;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 + =);Ai = sc.nextInt();sc.close();/ 采用冒泡排序法对A数组排序for (i = 0; i A.length - 1; i+) for (j = i + 1; j Aj) int t = Ai;Ai = Aj;Aj = t;System.out.println(The sorted numbers:);/ 通过foreach循环输出排序后的数组for (int k : A)System.out.print(k + );Q9. Sort an int array by the bubble and selection algorithms, the capacity of the array should be no less than 10.Select Sort:package java_select;public class Select_Sort public static void main(String args) int a=49,38,65,97,76,13,27,49,78,34,12,64,1,8; System.out.println(排序之前:); for (int i = 0; i a.length; i+) System.out.print(ai+ ); /简单的选择排序 for (int i = 0; i a.length; i+) int min = ai; int n=i; /最小数的索引 for(int j=i+1;ja.length;j+) if(ajmin) /找出最小的数 min = aj; n = j; an = ai; ai = min; System.out.println(); System.out.println(排序之后:); for (int i = 0; i a.length; i+) System.out.print(ai+ ); Fig.9.1Bubble Sort:package java_bubble;public class bubbleSort public static void main(String args) int a=49,38,65,97,76,13,27,49,78,34,12,64,1,8; System.out.println(排序之前:); for (int i = 0; i a.length; i+) System.out.print(ai+ ); /冒泡排序 for (int i = 0; i a.length; i+) for(int j = 0; jaj+1) int temp = aj; aj = aj+1; aj+1 = temp; System.out.println(); System.out.println(排序之后:); for (int i = 0; i a.length; i+) System.out.print(ai+ ); Fig.9.2Q10. Output all primes and non-primes in an int array respectively, the capacity of the array should be no less than 10. The prime number is a number that can only be divided without remainder by 1 and itself.So we can use for statement.package java_primeNumber;import java.util.Scanner;public class primes_nonprimes public static void main(String args) int num, i, j;int A = new int 10;System.out.println(please input the numbers);Scanner sc = new Scanner(System.in);/ 从键盘录入A数组的数据for(i=0;iA.length;i+)Ai = sc.nextInt();sc.close();/ 寻找并输出A数组中的素数for (i = 0; i A.length ; i+) num = Ai;for (j = 2; j = num; j+)if (num % j = 0)break;if (num = j) System.out.println(The Prime Number: + num);elseSystem.out.println(The Nonprime Number:+num);Q11. Input an integer from your keyboard and check whether it is a palindrome number.package java_palindrome;import java.util.Scanner;import javax.swing.JOptionPane;public class palindrome public static void main(String args) String inputValue = JOptionPane.showInputDialog(请输入一个整数); long inputValueLong = Long.parseLong(inputValue); long temp = inputValueLong; long reverseLong = 0L; while(inputValueLong != 0) reverseLong = reverseLong*10+inputValueLong%10; inputValueLong = inputValueLong/10; if(reverseLong = temp) System.out.println(你输入的是回文数); else System.out.println(你输入的不是回文数);Fig.11.1 Fig.11.2 Q12. Output each unit of an integer and its length, e.g., 123 should be output as 1, 2, and 3, and the length is 3. The units and length of the integer depend on your input from the keyboard.I first used an array to put every unit of the number so the volume of the array would exactly be the length of the number.However,I run into a problem on how to output the units by order.Finally I solved the by output the array in reverse.package java_eachunit;import java.util.Scanner;public class each_unit public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(请输入您所要查的数:); int num = input.nextInt(); String str = String.valueOf(num); int n = str.length(); int a=new intn; System.out.println(这个数的位数是+n); int i=0; for(i=0;i=0;j-) System.out.println(aj+ ); /逆序输出数组aQ13. Input an integer from your keyboard and check whether it is a narcissistic number, the integer is limited to an n-units number, here n is no less than 3.Though there is a simple solution to this experiment,I am a little greedy that I wanted to find a way to check self numbers which are not limited by units.At last,I achieved it by a variable “n”.package java_selfnum;import java.util.Scanner;public class self_num public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(请输入您所要查的数:); int num = input.nextInt(); int h = num; String str = String.valueOf(num); int n = str.length(); int a=new intn; System.out.println(num); int c=0; int s=new intn; for(int m = 0 ; mn ; m+) sm=1; for(int i=0;in;i+) ai=num%10; /将数字的最后一位放入数组 num=num/10; /去掉最后一个已放进数组的数字for(int j=0;jn;j+)si*=ai;c+=si; if(c=h) System.out.println(h+是自幂数); else System.out.println(h+不是自幂数); Fig.13.1 Fig.13.2 Fig.13.3Fig.13.4 Fig.13.5 Fig.13.6Q14. Output the n-th element of the Fibonacci sequence, n should be input from the keyboard. I have made a little change to the reference material,for the Fibonacci sequence has an special element.package java_Fibonacci;import java.util.Scanner;public class Fibonacci public static void main(String args) Scanner input = new Scanner(System.in);System.out.println(请输入您所要查的元素序列:);int n = input.nextInt(); int f_before_0 = 0;int f_before_1 = 1;int f_before_2 = 1;int f_i= 0;if(n = 1 | n = 2)System.out.println(No. + n + = + 1);else for(int i = 3; i = n; i+) f_i = f_before_1 + f_before_2;f_before_2 = f_before_1;f_before_1 = f_i;System.out.println(No. + n + = + f_i);Fig.14.1 Fig.14.2Q15. Output the first n terms of the Fibonacci sequence, n should be input from the keyboard.package java_Fibonacci2;import java.util.Scanner;public class get public static void main(String args) Scanner input = new Scanner(System.in);System.out.println(请输入您所要查的斐波那契数列项数:);int n = input.nextInt();int a = new intn; a0 =0; a1 = 1; for (int i = 2; i a.length; i+) ai = ai - 1 + ai - 2; System.out.println(斐波那契数列的前+n+项如下所示:); for (int i = 0; i a.length; i+) if (i % 5 = 0) System.out.println(); System.out.print(ai+t); Q16. Output the sum of the first n terms of the Fibonacci sequence, n should be input from the keyboard.package java_Fibonacci3;import java.util.Scanner;public class Sum public static void main(String args) Scanner input = new Scanner(System.in);System.out.println(请输入您所要求和的斐波那契数列项数:);int n = input.nextInt();int a = new intn; a0 =0; a1 = 1; for (int i = 2; i a.length; i+) ai = ai - 1 + ai - 2; System.out.println(斐波那契数列的前+n+项的和为:); int s=0; for (int i = 0; i a.length; i+) s+=ai; System.out.println(s);Q17. Calculate the factorial of n, n should be input from the keyboard and is supposed to be less than 10.package java_Factorial;import java.util.Scanner;public class Factorial public static void main(String args) Scanner input = new Scanner(System.in);System.out.println(请输入您所要求阶乘的数n:);int n = input.nextInt();int result = 1;for(int i = 1; i = n; i+) result = result * i;System.out.println(n + 的阶乘是: + result);Q18. Reverse an int array, that is, A0 exchanges its value with AN-1, A1 exchanges its value with AN-2, and so on.There are two difficulties in that:one is how to output an array,the other is how to exchange its elements and output it in reverse.package java_ReverseArr;import java.util.Arrays;import java.util.Scanner;public class ReverseArr public static void main(String args) System.out.println(请输入数组长度);Scanner input=new Scanner(System.in);int n=input.nextInt();int a=new intn;System.out.println(请输入依次数组元素);Scanner sc=new Scanner(System.in);for(int i=0;ia.length;i+)ai=sc.nextInt();sc.close();System.out.println(交换前数组为:+Arrays.toString(a);int j=0;for(j=0;j0;i-)int t=an-1;for(int j=n-1;j0;j-)aj=aj-1;a0=t;System.out.println(右移后数组为:+Arrays.toString(a);for(int i=k;i0;i-)int t=a0;for(int j=0;jn-1;j+)aj=aj+1;an-1=t;System.out.println(左移后数组为:+Arrays.toString(a); Q20. Calculate C = AB, here A, B and C are all matrices, if the multiplication cannot be conducted, a prompt should be given, A and B should be input from the keyboard.package java_matrixMultiply;import java.util.Scanner;public class matrixMultiply public static void main(String args) int i, j, k;int rowA, colA, rowB, colB;Scanner sc = new Scanner(System.in);System.out.print(请输入A矩阵的行数:);rowA = sc.nextInt();System.out.print(请输入A矩阵的列数:);colA = sc.nextInt();System.out.println(Matrix A is + rowA + * + colA);System.out.print(请输入B矩阵的行数:);rowB = sc.nextInt();System.out.print(请输入B矩阵的列数:);colB = sc.nextInt();System.out.println(Matrix B is + rowB + * + colB);/ A的列与B的行不相等时,无法相乘if (colA != rowB)System.out.println(A和B矩阵不可以相乘!);/ A和B可以相乘else int A = new introwAcolA;int

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论