收藏 分销(赏)

java练习题.doc

上传人:xrp****65 文档编号:9432428 上传时间:2025-03-26 格式:DOC 页数:43 大小:326.50KB 下载积分:10 金币
下载 相关 举报
java练习题.doc_第1页
第1页 / 共43页
java练习题.doc_第2页
第2页 / 共43页


点击查看更多>>
资源描述
第一章 1. 熟悉常用的dos命令 d: 盘符切换 cd 进入指定文件夹 dir 列出当前目录下的文件或文件夹 cd.. 返回上一级目录 cd\ 返回根目录 md 创建目录 rd 删除目录 del 删除文件,删除一堆后缀名为*.txt的文件 cls 清屏 exit 退出dos命令行 2. 编码实现换行输出你心目中男神的名字和女神的名字 class Demo{ public static void main(String[] args){ System.out.print("吴亦凡\n白百合"); } 3. 编码实现输出你的身高和体重,中间用制表符间隔 class Demo{ public static void main(String[] args){ System.out.print("身高:178cm\t体重:63kg"); } } 5. 能熟练描述Java跨平台的大致原理 跨平台:通过Java语言编写的应用程序在不同的系统平台上都可以运行。 原理:在需要运行java应用程序的操作系统上, 先安装一个Java虚拟机JVM。 由JVM来负责Java程序在该系统中的运行。 第二章 1.自己给定一个四位数,然后求出每位数字之和 class Exercise1{ public static void main(String[] args){ int a = 1111,b,c,d,e,f,g; b = a/1000; c = a%1000; d = c/100; e = c%100; f = e/10; g = e%10; System.out.print(b+d+f+g); } } 2.商场为员工提供了基本工资(2000元)、物价津贴及房租津贴,其中物价津贴为基本工资的40%,房租津贴为基本工资的25%。编程计算实领工资。 class Exercise2{ public static void main(String[] args){ int a =2000; double b=a*0.4; double c=a*0.25; double d=a+b+c; System.out.print(d); } } 3.对两个整数变量的值进行互换(使用第三方变量 瓶子) class Exercise3{ public static void main(String[] args){ int a =12,b = 13,c; c =b; b =a; a =c; System.out.println(a); System.out.print(b); } } 4.自己给定5个整数,通过编程求出最大值。 class Exercise5{ public static void main(String[] args){ int a=1,b=2,c=3,d=4,e=5; int max =(a>b)?a:b; max =(max>c)?max:c; max =(max>d)?max:d; max =(max>e)?max:e; System.out.print(max); } } 第三章 1.成绩大于90给,否则不给 class IfStarementDemo { public static void main(String[] args) { int score = 91; //考试成绩 if (score>90) //判断成绩是否大于90 { System.out.println("给"); } else{ System.out.println("不给"); } } } 2. 对令狐冲的考试成绩评测:成绩>=90 :奖励葵花宝典,90>成绩>=80 :奖励辟邪剑谱,80>成绩>=60 :废除武功,成绩<60 :逐出师们 class IfStarementDemo { public static void main(String[] args) { int score =50; if (score>=90) { System.out.println("a"); } else if (score>=80) { System.out.println("b"); } else if (score>=60) { System.out.println("c"); } else{ System.out.println("d"); } } } 3. int a=1,b=2,c=3; if (a<0) if(b<0) c=10; else c=20; System.out.println(c);//c=3 4. 令狐冲参加比武大会:如果获得第一名,将出任武林盟主,如果获得第二名,将出任武当掌门,如果获得第三名,将出任峨嵋掌门,否则,将被逐出师门。 int mc=1; switch (mc) { case 1 : System.out.println("a"); break; case 2: System.out.println("b"); break; case 3: System.out.println("c"); break; default: System.out.println("d"); 5. 自己给定一个月份,判断属于哪个季节。(3-5月为春季、6-8月为夏季、9-11月为秋季、12-2月为冬季) int x=4 ; if (x==3||x==4||x==5) { System.out.println("春季"); } else if (x==6||x==7||x==8) { System.out.println("夏季"); } else if (x==9||x==10||x==11) { System.out.println("秋季"); } else if (x==12||x==1||x==2) { System.out.println("冬季"); } else System.out.println("没有对应的季节"); if(x>12 || x<1) System.out.println(x+"没有对应的季节"); else if(x>=3 && x<=5) System.out.println(x+"是春天"); else if(x>=6 && x<=8) System.out.println(x+"是夏天"); else if(x>=9 && x<=11) System.out.println(x+"是秋天"); else System.out.println(x+"是冬天"); int month1=2; switch (month1) { case 1: case 2: System.out.println("冬天"); break; case 3: case 4: case 5: System.out.println("春天"); break; case 6: case 7: case 8: System.out.println("夏天"); break; case 9: case 10: case 11: System.out.println("夏天"); break; case 12: System.out.println("冬天"); break; } 6. 求出1-100之间偶数和 public static void main(String[] args) { int sum =0; for (int i =0; i<=100;i++ ) { if (i%2==0) { sum+=i; } } System.out.println(sum); } } 7. 请输出一个4行5列的星星(*)图案 for (int x=0;x<4;x++ ) { for (int y=0;y<5 ; y++) { System.out.print("*"); } System.out.println(); } 8.请输出如下图形 * ** *** **** ***** for (int x =0;x<5 ;x++ ) { for (int y = 0;y<=x; y++) { System.out.print("*"); } System.out.println(); } 9.请输出如下图形 ***** **** *** ** * for (int x =0;x<5 ;x++ ) { for (int y = 5;y>=x+1; y--) { System.out.print("*"); } System.out.println(); } 10.在控制台输出九九乘法表。(\t:一个制表符的位置) for (int x =1; x<=9; x++) { for (int y=1;y<=x ; y++) { System.out.print(y+"*"+x+"="+y*x+"\t"); } System.out.println(); } 11.求5的阶乘(5*4*3*2*1) int accumulate=1; for (int a=5;a>0 && a <=5;a-- ) { accumulate =accumulate*a; } System.out.print(accumulate); 12.请输出1-100之间能同时被2、3、5整除的数有多少个? int count =0; for (int x = 1;x<=100 ;x++ ) { if (x%30==0) { count++; } } System.out.println("count="+count); 13. 在控制台输出一个金字塔。 for (int x =1;x<=4 ;x++ ) { for (int y = 3;y>=x; y--) { System.out.print(" "); } for (int b =1;b<2*x+1;b++ ) { System.out.print("*"); } System.out.println(); } for (int a = 1;a<=4 ;a++ ) { for (int b = 1;b<=4-a ; b++) { System.out.print(" "); } for (int c = 1;c<=2*a-1 ;c++ ) { System.out.print("*"); } System.out.println(); } 第四章 1.定义一个n,在控制台输出nn乘法表。(\t:一个制表符的位置) class ExerciseDemo3 { public static void main(String[] args) { printCFB(10); } public static void printCFB(int num){ for (int x =1; x<=num; x++) { for (int y=1;y<=x ; y++) { System.out.print(y+"*"+x+"="+y*x+"\t"); } System.out.println(); } } } 2.在控制台输出一个金字塔。 ####### ##### ### # for (int a = 1;a<=4 ;a++ ) { for (int b = 1;b<=a-1 ; b++) { System.out.print(" "); } for (int c =1 ;c>=7-a*2 ;c-- ) { System.out.print("*"); } System.out.println(); } 3.我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。请问,我折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度? class WhileDemo { public static void main(String[] args) { double zhufeng = 8848; double zhi = 0.01; int count = 0; while (zhi<zhufeng) { zhi = zhi * 2; count ++; } System.out.print(count); } } class WhileDemo { public static void main(String[] args) { double zhufeng = 8848; double zhi = 0.01; int count = 0; do { zhi = zhi*2; count++; } while (zhi<zhufeng); System.out.print(count); } } 4. 在控制台输出1-100之间奇数和(使用while、continue) class TestDemo2 { public static void main(String[] args) { System.out.println(sum()); } public static int sum(){ int a = 1; int sum = 0; while (a<=100) { if (a%2==0) { a++; continue; } sum +=a; a++; } return sum; } } 5. 求出1-100之间偶数和(使用while、continue) class TestDemo1 { public static void main(String[] args) { sum() //System.out.println(sum()); } public static int sum(){ int a = 1; int sum = 0; while (a<=100) { if (a%2==1) { a++; continue; } sum+=a; a++; } return sum; } } 6. 结合break,实现一张纸折叠多少次高度超过珠峰。 class TestDemo3 { public static void main(String[] args) { System.out.println(count()); } public static int count(){ double a = 8848; double b = 0.02; int count=1; while (true) { b*=2; count ++; if (b>=a) { break; } } return count; } } 第五章 1. 设计一个方法实现求任意两个数的最大值 public static void main(String[] args) { max(9,8); System.out.println(max(9,8)); } public static int max(int a ,int b){ //return a>b?a:b; if (a>b) { return a; }else { return b; } } } import java.util.Scanner; class MethodDemo { public static void main(String[] args) { Scanner sc = new Scanner (System.in); System.out.println("请输入第一个数字"); int a = sc.nextInt(); System.out.println("请输入第二个数字"); int b = sc.nextInt(); int max = max(a,b); System.out.println(max); } public static int max(int a ,int b){ //return a>b?a:b; if (a>b) { return a; }else { return b; } } } 2. 设计一个方法实现输出一个任意行任意列的矩形 class MethodDemo2 { public static void main(String[] args) { draw(7,8); } public static void draw(int x ,int y){ for (int a=0;a<x ;a++ ) { for (int b = 0;b<y ;b++ ) { System.out.print("*"); } System.out.println(); } } } import java.util.Scanner; class MethodDemo2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入行数"); int a = sc.nextInt(); System.out.println("请输入列数"); int b = sc.nextInt(); draw(a,b); } public static void draw(int x ,int y){ for (int a=0;a<x ;a++ ) { for (int b = 0;b<y ;b++ ) { System.out.print("*"); } System.out.println(); } } } 3.定义一个数据n(1<=n<=9),输出对应的nn乘法表 class MethodDemo3 { public static void main(String[] args) { draw(9); } public static void draw(int x ){ for (int a =1;a<=x ;a++ ) { for (int b =1;b<=a ;b++ ) { System.out.print(b+"*"+a+"="+a*b+"\t"); } System.out.println(); } } } import java.util.Scanner; class MethodDemo3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输n的值"); int a = sc.nextInt(); draw(a); } public static void draw(int x ){ for (int a =1;a<=x ;a++ ) { for (int b =1;b<=a ;b++ ) { System.out.print(b+"*"+a+"="+a*b+"\t"); } System.out.println(); } } } 4. class ArrayDemo1 { public static void main(String[] args) { int[] arr = {1,2,3,4,5,6,7,8,9}; boolean flag = true; for (int a=0;a<arr.length ; a++) { int temp=5; if (temp==arr[a]) { System.out.println("存在"); flag=false; break; } } if(flag){ System.out.println("不存在"); } } } 第七章 1.自定义一个类模拟“员工”,属性有:姓名、性别、年龄、职务,方法有:上班、开会 class Staff { String name ; String sex; int age; String job; public void work(){ System.out.println("员工的名字是"+name+"性别"+sex+"工作是"+job); } public void attendMeeting(){ } } class TestStaff { public static void main(String[] args) { Staff s =new Staff(); s.name="Harry"; s.sex="男"; s.age=25; s.job="客服"; s.work(); } } 2.自定义一个类模拟“图书”,属性有:书名、作者、价格,方法有一个:以字符串的形式输出书名、作者和价格 class Book { String name; String author; double price; public void output() { System.out.println("书名是:"+name+",作者是:"+author+",价格是:"+price); } } class TestBook { public static void main(String[] args) { Book b=new Book(); b.name="檀香刑"; b.author="莫言"; b.price=48.3; b.output(); } } 3.定义一个标准的员工类 class TestStaff { public static void main(String[] args) { Staff s =new Staff(); s.setName("Hurry"); s.setSex("男"); s.setAge(25); s.setJob("客服"); System.out.println(s.getName()); System.out.println(s.getSex()); System.out.println(s.getAge()); System.out.println(s.getJob()); s.work(); s.attendMeeting(); } } class Staff { private String name ; private String sex; private int age; private String job; public Staff(){ } public Staff(String a,String b,int c,String d){ name=a; sex=b; age=c; job=d; } public void setName(String name1){ name = name1; } public String getName(){ return name; } public void setSex(String sex1){ sex = sex1; } public String getSex(){ return sex; } public void setAge(int age1){ age = age1; } public int getAge(){ return age; } public void setJob(String job1){ job= job1; } public String getJob(){ return job; } public void work(){ System.out.println("一个叫"+name+",性别为"+sex+",工作是"+job+"的员工"); } public void attendMeeting(){ System.out.println("正在开会"); } } 4. 定义一个标准的图书类。 class TestBook { public static void main(String[] args) { Book b=new Book(); b.setName("檀香刑"); b.setAuthor("莫言"); b.setPrice(48.3); System.out.println(b.getName()); System.out.println(b.getAuthor()); System.out.println(b.getPrice()); b.output(); } } class Book { private String name; private String author; private double price; public Book(){ } public Book(String a,String b,double c){ name=a; author=b; price=c; } public void setName(String name){ this.name = name; //name = name1; } public String getName(){ return name; } public void setAuthor(String author){ this.author = author; //author = author1; } public String getAuthor(){ return author; } public void setPrice(double price){ this.price = price; //price = price1; } public double getPrice(){ return price; } public void output() { System.out.println("书名是:"+name+",作者是:"+author+",价格是:"+price); } } 5. 手机有品牌、价格等属性,可以打电话和发短信。创建两个手机对象,分别通过setter和构造方法实现属性初始化,最后输出这些信息 class Phone { private String brand; private double price; public Phone(){ } public Phone(String a,double b){ brand=a; price =b; } public void setBrand(String brand ){ this.brand=brand; } public String getBrand(){ return brand; } public void setPrice(double price){ this.price=price; } public double getPrice(){ return price; } public void call(){ } public void sendMessage(){} } class TestPhone { public static void main(String[] args) { Phone p= new Phone(); p.setBrand("三星"); p.setPrice(3200); System.out.println(p.getBrand()); System.out.println(p.getPrice()); p.call(); p.sendMessage(); Phone p1= new Phone(); p1.setBrand("苹果"); p1.setPrice(5200); System.out.println(p1.getBrand()); System.out.println(p1.getPrice()); p1.call(); p1.sendMessage(); } } 6. 老师有名字、年龄等属性,可以讲课、提问。创建两个老师对象,分别通过setter和构造方法实现属性初始化,最后输出这些信息 class Teacher { private String name; private int age; public Teacher(){ } public Teacher(String a,int b){ name=a; age =b; } public void setName(String name ){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } public void teach(){ System.out.println("教学"); } public void askQuestion(){ System.out.println("提问"); } } class TestTeacher { public static void main(String[] args) { Teacher t = new Teacher(); t.setName("狄"); t.setAge(18); System.out.println(t.getName()); System.out.println(t.getAge()); t.teach(); t.askQuestion(); Teacher t2 = new Teacher("张",28); t2.teach(); t2.askQuestion(); } } 7. 会员有会员号、积分、密码等属性,有个功能方法就是注册 class VIP { private String number; private int jifen; private String password; public VIP(){ } public VIP(String a,int b,String c){ number = a; ji
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服