




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、明天几号public class Date2 int year, month, day;public void setdate(int y, int m, int d) year = y;month = m;day = d;void today() System.out.println("The date of today is " + year + "/" + month + "/"+ day);boolean isleap(int y) return (y % 4 = 0 && y % 100 != 0) | ye
2、ar % 400 = 0;void tomorrow() int d, m, y;d = day + 1;m = month;y = year;if (d > 28) && month = 2) if (!isleap(year) | d > 29) d = 1;m = m + 1; else if (d > 30&& (month < 7 && month % 2 = 0 | month > 7 && month % 2 = 1) d = 1;m = m + 1; else if (d > 3
3、1) d = 1;m = m + 1; if (m = 13) y = y + 1;m = 1; System.out.println("The date of tomorrow is " + y + "/" + m + "/" + d);public static void main(String args) Date2 de = new Date2();de.setdate(2004, 2, 28);if (de.isleap(de.year)System.out.println(de.year + " is a lea
4、p year.");de.today();de.tomorrow();兔子对数package Sy;public class Rabbit public static void main(String args) int month = 4;Rabbit r = new Rabbit();System.out.print(month + "月共有" + r.getNumber(month)+"对兔子。");public int getNumber(int month) int num1 = 1, num2 = 1;int num3 = 0;if
5、 (month = 1)return num1;else if (month = 2)return num2;else for (int i = 3; i <= month; i+) num3 = num1 + num2;num1 = num2;num2 = num3;return num3;输出1131135311357531135797531package Ch3;/类似杨辉三角public class YH public static void main(String args) int i;int yh = new int5;for (i = 0; i < 5; i+) y
6、hi = new int2 * i + 1;/ 第i行有(2*i+1)个元素yhi0 = 1;yhi2 * i = 1;for (i = 1; i < 5; i+)/ i:1-4;j:1-7(0,8上的数为0)for (int j = 1; j < 2 * i; j+)yhij = yhi - 1j - 1 + 2;/ 前一行前一列加2for (i = 0; i < 5; i+) for (int j = 0; j <= 2 * i; j+)System.out.print(yhij + "t");System.out.println();或publ
7、ic class YH public static void main(String args) int i,j,k; for(i=0;i<=4;i+) for ( j=0;j<20-i;j+) System.out.print(" "); for (k=0;k<=2*i;k+) if (k<=i) System.out.print(" "+(2*k+1); else System.out.print(" "+(2*(2*i-k)+1); System.out.println(); 键盘输入m,输出小于等于m的
8、所有素数for (i = 2; i <= m; i+) p = true;for (int j = 2; j < i; j+)if (i % j = 0) p = false;break;if (p = true) System.out.print(i + "t"); n+; if (n % 10 = 0) System.out.println();输出一整数的所有因子 for(int i=1;i<=m;i+) if (m%i=0) System.out.print(i+"t");多少天后钢材小于5米 int d=0;float m=2
9、000; while (m>=5) m=m/2; d+; System.out.print(d+": "); System.out.println(m); System.out.print("You need "+d+" days"); 月份英文名输出 int m; String str; String month="January","February","March","April","May","June"
10、;,"July","August","september","October","November","December" BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in); System.out.print("Input the m:"); str=buf.readLine(); m=Integer.parseInt(str); if (m>=1
11、&& m<=12) System.out.println(monthm-1); else System.out.print("Your Input is wrong"); 或者可以用P55 switch 语句单词计数int n = 0;for (int i = 0; i < s1.length(); i+) char m = s1.charAt(i);if(Character.toLowerCase(m)<97| Character.toLowerCase(m) > 122)&& Character.toLowerCa
12、se(s1.charAt(i - 1) >= 97&& Character.toLowerCase(s1.charAt(i - 1) <= 122)n = n + 1;System.out.println(n);0-9出现次数int m;int a=new int10;long aa=1586488654895649l;for(int i=0;i<=15;i+)m=(int) (aa%10);am=am+1;aa=aa/10; for(m=0;m<10;m+)System.out.println(m+" :"+am);包的引用pack
13、age Ch4;import java.util.*;public class Date private int year,month,day;public Date(int y,int m,int d)year=y;month=m;day=d;public Date();public int thisyear() /方法this year 得到当前年份return Calendar.getInstance().get(Calendar.YEAR);package Ch4;import Ch4.Date; /导入Ch4包中的Date类class Person String name;int a
14、ge;public Person(String na, int ag) name = na;age = ag;public Person() public int birth(int y) /得到出生年份return y - age + 1;public class Person_ex public static void main(String args) Person ps = new Person("Tom", 21);Date now = new Date();int y = now.thisyear();System.out.println( + &
15、quot; was born in " + ps.birth(y);第四章1import java.util.*;public class Person private String name;private char sex;private int year, month;public Person() public Person(String nm, char sx, int y, int m) name = nm;sex = sx;year = y;month = m;public void printPerson() Calendar now = Calendar.getIn
16、stance();int age = now.get(Calendar.YEAR) - year;System.out.println("Name: " + name + ",Sex: " + sex + ", Age: " + age);public static void main(String args) Person pe1 = new Person("Tom", 'm', 1980, 10);pe1.printPerson();饮料味道public abstract class Drink
17、 final static int coffee=1;final static int beer=2;final static int milk=3;public abstract void taste();public static Drink getDrink(int drinkType)switch(drinkType)case 1:return new Coffee();case 2:return new Beer();case 3:return new Milk();default:return null;public class Coffee extends Drinkpublic
18、 void taste()System.out.println("咖啡: 苦");package Sy;import java.io.*;public class Test public static void main(String args) throws IOExceptionBufferedReader buf;buf=new BufferedReader(new InputStreamReader(System.in);System.out.println("请输入饮料类型(1-3):");String str=""trys
19、tr=buf.readLine();catch(Exception e)int drinkType=Integer.parseInt(str);Drink drink=Drink.getDrink(drinkType);if(drink=null)System.out.println("对不起!没有您输入的饮料类型。");elsedrink.taste();多种构造方法,不同的构造方法创建对象(求面积)public class Rectangle double width, length, girth, area;public Rectangle() ;public Rec
20、tangle(double wd, double le) width = wd;length = le;public void setWidth(double wd) width = wd;public void setLength(double le) length = le;public double getWidth() return width;public double getLength() return length;public double girth() return 2 * (width + length);public double area() return widt
21、h * length;public void printRectangle() System.out.println("Width=" + width + ", Length=" + length);public static void main(String args) Rectangle re1 = new Rectangle(10, 20);Rectangle re2 = new Rectangle();re2.setWidth(3);re2.setLength(4);re1.printRectangle();System.out.println(
22、"Girth=" + re1.girth() + ", Area=" + re1.area();re2.printRectangle();System.out.println("Girth=" + re2.girth() + ", Area=" + re2.area();子串的查找public class SubString public static void main(String args) String str1 = new String("addActionListener");Str
23、ing str2 = new String("Action");int n;n = str1.indexOf(str2);if (n >= 0) System.out.println("String_2 is in String_1");System.out.println("The substring before String_2 is "+ str1.substring(0, n);System.out.println("The substring behind String_2 is "+ str1.
24、substring(n + str2.length();elseSystem.out.println("String_2 is not in String_1");学生管理系统public class Student String stuno, name;float math, english, computer;public Student() public Student(String stuno, String name, float math, float english,float computer) this.stuno = stuno; =
25、name;this.math = math;this.english = english;puter = computer;public String getStuno() return stuno;public void setStuno(String stuno) this.stuno = stuno; 。public void setComputer(float computer) puter = computer;public float sum() float s;s = this.math + this.english + puter;return s;public float a
26、vg() float avg;avg = this.sum() / 3;return avg;public float max() /最大值float max = this.math;if (this.english > max)max = this.english;if (puter > max)max = puter;return max;public float min() /最小值float min = this.math;if (this.english < min)min = this.english;if (puter < min)min = puter;
27、return min;package Sy;public class StudentOperation Student stuArray;int num = 0;public StudentOperation() stuArray = new Student30;public Student getStuArray() return stuArray;public int getNum() return num;public boolean addStudent(Student student) if (num < 30) stuArraynum = student;num+;retur
28、n true; else System.out.println("stuArray.lenth" + stuArray.length);System.out.println("数组越界,不能增加。");return false;public boolean delStudent(String stuno) for (int i = 0; i < stuArray.length; i+)if (stuArrayi.getStuno().equals(stuno) for (int j = i; j < num; j+)stuArrayj = s
29、tuArrayj + 1;stuArray29 = null;return true;return false;public Student queryStuno(String stuno) for (int i = 0; i < num; i+)if (stuArrayi.getStuno().equals(stuno) System.out.println("学号" + "t" + "姓名" + "t" + "数学" + "t"+ "英语" +
30、"t" + "计算机");System.out.println(stuArrayi.getStuno() + "t"+ stuArrayi.getName() + "t" + stuArrayi.getMath()+ "t" + stuArrayi.getEnglish() + "t"+ stuArrayi.getComputer();return stuArrayi;return null;public Student queryName(String name) for
31、(int i = 0; i < num; i+)if (stuArrayi.getName().equals(name) return stuArrayi;return null;public boolean updStu(Student stuno) for (int i = 0; i < num; i+)if (stuArrayi.getStuno().equals(stuno.getStuno() stuArrayi = stuno;return true;return false;public void print() System.out.println("学号
32、" + "t" + "姓名" + "t" + "数学" + "t" + "英语"+ "t" + "计算机");tryfor (int i = 0; i < num; i+)System.out.println(stuArrayi.getStuno() + "t"+ stuArrayi.getName() + "t" + stuArrayi.getMath()+ "t&qu
33、ot; + stuArrayi.getEnglish() + "t"+ stuArrayi.getComputer() + "t");catch(NullPointerException e)public void statistics() System.out.println("学科" + "t" + "平均成绩" + "t" + "最高分" + "t" + "最低分"+ "t");System
34、.out.print("数学" + "t");float avg = 0, sum = 0;for (int i = 0; i < num; i+)sum += stuArrayi.getMath();avg = sum / num;System.out.print(avg + "t");float max = 0;for (int i = 0; i < num; i+)if (stuArrayi.getMath() > max)max = stuArrayi.getMath();System.out.print(m
35、ax + "t");float min = 100;for (int i = 0; i < num; i+)if (stuArrayi.getMath() < min)min = stuArrayi.getMath();System.out.println(min + "t");System.out.print("英语" + "t");float avg1 = 0, sum1 = 0;for (int i = 0; i < num; i+)sum1 += stuArrayi.getEnglish(
36、);avg1 = sum1 / num;System.out.print(avg1 + "t");float max1 = 0;for (int i = 0; i < num; i+)if (stuArrayi.getEnglish() > max1)max1 = stuArrayi.getEnglish();System.out.print(max1 + "t");float min1 = 100;for (int i = 0; i < num; i+)if (stuArrayi.getEnglish() < min1)min1
37、 = stuArrayi.getEnglish();System.out.println(min1 + "t");System.out.print("计算机" + "t");float avg2 = 0, sum2 = 0;for (int i = 0; i < num; i+)sum2 += stuArrayi.getComputer();avg2 = sum2 / num;System.out.print(avg2 + "t");float max2 = 0;for (int i = 0; i <
38、num; i+)if (stuArrayi.getComputer() > max2)max2 = stuArrayi.getComputer();System.out.print(max2 + "t");float min2 = 100;for (int i = 0; i < num; i+)if (stuArrayi.getComputer() < min2)min2 = stuArrayi.getComputer();System.out.println(min2 + "t");package Sy;import java.io.
39、*;public class StudentManagement public static void main(String args) StudentManagement sm = new StudentManagement();StudentOperation so = new StudentOperation();int menu;while (true) menu = sm.printMenu();if (menu = 1) Student s = sm.addStudent();so.addStudent(s);if (menu = 2) String stuno = sm.que
40、ry();so.delStudent(stuno);if (menu = 3) String stuno = sm.query();so.queryStuno(stuno);if (menu = 4) Student student = sm.updStudent();so.updStu(student);if (menu = 5) so.print();if (menu = 6) so.statistics();if (menu = 7) break;public Student addStudent() Student student = new Student();System.out.
41、println("请输入学号: ");BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);String str = ""try str = buf.readLine(); catch (Exception e) student.setStuno(str);System.out.println("请输入姓名: ");buf = new BufferedReader(new InputStreamReader(System.in);str
42、 = ""try str = buf.readLine(); catch (Exception e) student.setName(str);System.out.println("请输入数学: ");buf = new BufferedReader(new InputStreamReader(System.in);str = ""try str = buf.readLine(); catch (Exception e) float math = Float.parseFloat(str);student.setMath(math)
43、;System.out.println("请输入英语: ");buf = new BufferedReader(new InputStreamReader(System.in);str = ""try str = buf.readLine(); catch (Exception e) float english = Float.parseFloat(str);student.setEnglish(english);System.out.println("请输入计算机: ");buf = new BufferedReader(new I
44、nputStreamReader(System.in);str = ""try str = buf.readLine(); catch (Exception e) float computer = Float.parseFloat(str);student.setComputer(computer);return student;public String query() System.out.println("请输入学号: ");BufferedReader buf;buf = new BufferedReader(new InputStreamRea
45、der(System.in);String str = ""try str = buf.readLine(); catch (Exception e) return str;public Student updStudent() Student student = new Student();System.out.println("请输入学号: ");BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);String str = ""try str = buf.readLine(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中医三基填空试题及答案
- 中医十二经络试题及答案
- 农业产业强镇建设资金申请项目资金使用管理与风险防范报告
- 2025年事业单位工勤技能-安徽-安徽客房服务员四级(中级工)历年参考题库含答案解析
- 2025年事业单位工勤技能-安徽-安徽动物检疫员一级(高级技师)历年参考题库含答案解析
- 2025年事业单位工勤技能-安徽-安徽中式烹调师四级(中级工)历年参考题库含答案解析
- Dimethyl-biphenyl-4-4-dicarboxylate-Standard-生命科学试剂-MCE
- 6-Thioguanosine-Standard-生命科学试剂-MCE
- 1-3-5-Tribromo-2-iodobenzene-d2-2-4-6-Tribromoiodobenzene-d-sub-2-sub-生命科学试剂-MCE
- 招聘过程中企业文化匹配技巧面试题
- 同步控制器说明书
- 辅助角公式练习题
- GB/T 7631.8-1990润滑剂和有关产品(L类)的分类第8部分:X组(润滑脂)
- GB/T 40333-2021真空计四极质谱仪的定义与规范
- GB/T 35778-2017企业标准化工作指南
- 羽毛球校本教材
- GB/T 15601-2013管法兰用金属包覆垫片
- GB/T 12325-2008电能质量供电电压偏差
- 汽轮机原理-凝汽器课件
- 二年级下册认识方向练习题
- 检验报告(风机)
评论
0/150
提交评论