




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、. JAVA编程实例1、public class MyClass private int val1,val2 ;public void myFun(int x,int y) val1=x ; val2=y ; System.out.println("The sum is: "+(val1+val2) ; public static void main(String arg) MyClass MyObj=new MyClass(); MyObj.myFun(1,2);运行结果如下:The sum is: 32、public class MyArray public stati
2、c void main(String args) int myArray; /声明数组 myArray=new int10; /创建数组 System.out.println("IndexttValue"); for(int i=0; i<myArray.length;i+) System.out.println(i+"tt"+myArrayi); /证明数组元素默认初始化为0 /myArray10=100; /将产生数组越界异常 输出: 5050 3、public class Arrays public static void main(Stri
3、ng args) int a1 = 1, 2, 3, 4, 5 ; int a2; a2 = a1; for(int i = 0; i < a2.length; i+) a2i+; for(int i = 0; i < a1.length; i+) System.out.println( "a1" + i + " = " + a1i); 运行结果:a10 = 2a11 = 3a12 = 4a13 = 5a14 = 64、public class ArrayOfStringsDemo public static void main(String
4、 args) String anArray = "String One", "String Two", "String Three" for (int i = 0; i < anArray.length; i+) System.out.println(anArrayi.toLowerCase(); 运行结果:string onestring twostring three5、public class ArrayCopyDemo public static void main(String args) char copyFrom
5、= 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' char copyTo = new char7; System.arraycopy(copyFrom, 2, copyTo, 0, 7); System.out.println(new String(copyTo); 运行结果:caffeine6、 ex3_1
6、 输入一个年份,判断它是不是闰年。 闰年: 能被4整除但不能被100整除,或者能被400整除。import java.io.*; public class ex3_1 public static void main(String args) throws IOException int year; boolean IsLeapYear; System.out.println("Enter the year:"); BufferedReader in =new BufferedReader( new InputStreamReader(System.in); year=(ne
7、w Integer(in.readLine().intValue();IsLeapYear=(year%4=0 && year%100 != 0)|(year%400 = 0); if (IsLeapYear) System.out.print(year); System.out.println( "is a leap year"); else System.out.print(year); System.out.println( "is not a leap year"); 7、输入两个整数比较大小import java.io.*;pu
8、blic class ex3_2 public static void main(String args)throws IOException int x,y; BufferedReader in = new BufferedReader( new InputStreamReader(System.in); System.out.println("Enter x and y:"); x=(new Integer(in.readLine().intValue(); y=(new Integer(in.readLine().intValue(); if (x!=y) if (x
9、>y) System.out.println("x>y"); else System.out.println("x<y"); else System.out.println("x=y"); 8、已知一个学生的分数,给出其分数等级。90-100分为级;80-89分为B级;70-79分为级;60-69分为D级;0-59分为E级public class IfElseDemo public static void main(String args) int testscore = 76; char grade; if (te
10、stscore >= 90) grade = 'A' else if (testscore >= 80) grade = 'B' else if (testscore >= 70) grade = 'C' else if (testscore >= 60) grade = 'D' else grade = 'F' System.out.println("Grade = " + grade); 或public class Grade public static char g
11、radeLevel(double g)int n = (int)Math.floor(g/10);switch (n) case 10: case 9 : return('A'); case 8 : return('B'); case 7 : return('C'); case 6 : return('D'); default: return('E'); public static void main(String args) System.out.println("gradeLevel(100)=&qu
12、ot;+gradeLevel(100); System.out.println("gradeLevel(95.5)="+gradeLevel(95.5); System.out.println("gradeLevel(88)="+gradeLevel(88); System.out.println("gradeLevel(72)="+gradeLevel(72); System.out.println("gradeLevel(68.5)="+gradeLevel(68.5); System.out.println(
13、"gradeLevel(60)="+gradeLevel(60); System.out.println("gradeLevel(59.5)="+gradeLevel(59.5); System.out.println("gradeLevel(35)="+gradeLevel(35);9、输入06之间的某一个整数,然后把它转换成星期输出。(0对应星期日)import java.io.*;public class ex3_3 public static void main(String args)throws IOException i
14、nt day; BufferedReader in =new BufferedReader( new InputStreamReader(System.in); day=(new Integer(in.readLine().intValue();switch (day) case 0: System.out.println("Sunday"); break; case 1: System.out.println("Monday"); break;case 2: System.out.println("Tuesday"); break;
15、 case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; default: System.out.println("Day out of range Sunday .Saturday"
16、; ); break; 10、打印九九乘数表public class MultiTable public static void main(String args) for (int i=1; i<=9;i+) for (int j=1; j<=i;j+) System.out.print(" "+i+"*"+j+"="+i*j); System.out.println(); 11、输入一个整数,输出它所有的因数import java.io.*;public class ex3_7 public static void m
17、ain(String args) throws IOException int n,k; BufferedReader in =new BufferedReader( new InputStreamReader(System.in); System.out.println("Enter a positive integer: "); n=(new Integer(in.readLine().intValue(); System.out.print("Number "+n+" Factors "); for (k=1; k <=
18、n; k+) if (n % k = 0) System.out.print(k + " "); System.out.println(); 12、计算数列1,2,10 的和。public class ex3_4 public static void main(String args) int i=1, sum=0; while(i<=10) sum+=i; i+; System.out.println("sum="+sum); 13、输入一个整数,然后输出它的翻转形式import java.io.*;public class ex3_5 publ
19、ic static void main(String args)throws IOException int n, right_digit, newnum = 0; BufferedReader in = new BufferedReader( new InputStreamReader(System.in); System.out.println("Enter the number: "); n=(new Integer(in.readLine().intValue(); System.out.print("The number in reverse order
20、 is "); do right_digit = n % 10; System.out.print(right_digit); n /= 10; while (n != 0); System.out.println(); 14、public class BreakTest public static void main( String args ) String output = "" int i; for ( i = 1; i <= 10; i+ ) if ( i = 5 ) break; / break loop only if count = 5 output += i + " " output +
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 基于深度学习的过滤水质预测研究
- 沉浸式教学法在初级汉语综合课的应用行动研究-以泰国岱密中学为例
- 胎盘影像诊断
- 感统组合培训
- 集中注意力与心理健康维护
- 阑尾手术护理查房
- 《智能网联汽车技术》课件-超声波雷达
- 预防溺水班会课件
- 顶岗实习安全课件
- 音标课件图片高清
- 17025检测和校准实验室认可准则解析
- 工业废水处理工(中级工)理论试题库汇总-上(单选、多选题)
- 潜水泵操作JSA分析表
- DL∕T 5622-2021 太阳能热发电厂储热系统设计规范
- 物理化学实验:实验12 胶体的制备和电泳
- 高中物理选修 分子动理论
- 领军人才选拔试题答案
- CNC数控车床操作指导书
- 管道施工主要质量保证措施及通病防治措施
- 失火罪消防责任事故罪消防刑事案件移送移交报告
- 斯巴达勇士赛
评论
0/150
提交评论