




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工程成本预算方案试题及答案
- 上海中国福利会发展研究中心(宋庆龄儿童发展中心中国福利会教师教育发展中心)招聘笔试历年参考题库附带答案详解
- 工业互联网平台微服务架构性能测试报告:2025年云原生应用设计
- 2025年行政管理意见征集试题及答案
- 行政管理危机应对策略试题及答案
- 行政管理的政策实施评估方法试题及答案
- 2025年艺术培训平台在线教学平台技术创新与知识产权保护报告
- 2025年公众参与机制在环境治理项目公众参与效果跟踪报告
- 2025年公路货运行业数字化转型与智能物流网络规划报告
- 工程经济定量与定性分析试题及答案
- 阿替普酶的药理作用及应用
- 2024年学生团干部技能大赛考试题库350题(含答案)
- 走进歌剧世界智慧树知到期末考试答案章节答案2024年北京航空航天大学
- 给甲方工程联系函范文(十八篇)
- 矿山安全知识培训
- 第2课.铅笔淡彩 课件 2023--2024学年浙美版初中美术八年级下册
- 2023年高考俄语试题
- 生产性服务业集聚对我国制造业全球价值链地位影响的门槛效应研究
- 西南师大版二年级下册递等式计算练习300题及答案
- kpu鞋面工艺流程
- 图形设计方法同构、替构、解构、重构
评论
0/150
提交评论