题目1:打印沙漏 要求: 分别使用while、do…while、for三种循环方式输出以下沙漏图形。
1 2 3 4 5 ***** *** * *** *****
解析: 所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
实现代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 import java.util.Scanner;public class first1 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.print("请输入一个整数:" ); int n = scanner.nextInt(); int j=(n+1 )/2 +1 ; int i=1 ; while (i<=(n+1 )/2 ){ for (int a=1 ;a<=i-1 ;a++){ System.out.print(" " ); } for (int b=1 ;b<=((n+2 )-2 *i);b++){ System.out.print("*" ); } System.out.println(); i++; } while (j<=n){ for (int c=1 ;c<=(n-j);c++){ System.out.print(" " ); } for (int d=1 ;d<=2 *j-n;d++){ System.out.print("*" ); } System.out.println(); j++; } } } import java.util.Scanner;public class first2 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.print("请输入一个整数:" ); int n = scanner.nextInt(); int j=(n+1 )/2 +1 ; int i=1 ; do { for (int a=1 ;a<=i-1 ;a++){ System.out.print(" " ); } for (int b=1 ;b<=((n+2 )-2 *i);b++){ System.out.print("*" ); } System.out.println(); i++; }while (i<=(n+1 )/2 ); do { for (int c=1 ;c<=(n-j);c++){ System.out.print(" " ); } for (int d=1 ;d<=2 *j-n;d++){ System.out.print("*" ); } System.out.println(); j++; }while (j<=n); } } import java.util.Scanner;public class first3 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.print("请输入一个整数:" ); int n = scanner.nextInt(); for (int i=1 ;i<=(n+1 )/2 ;i++){ for (int a=1 ;a<=i-1 ;a++){ System.out.print(" " ); } for (int b=1 ;b<=((n+2 )-2 *i);b++){ System.out.print("*" ); } System.out.println(); } for (int j=(n+1 )/2 +1 ;j<=n;j++){ for (int c=1 ;c<=(n-j);c++){ System.out.print(" " ); } for (int d=1 ;d<=2 *j-n;d++){ System.out.print("*" ); } System.out.println(); } } }
题目2:打印沙漏(进阶) 给定符号以及符号个数。首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例:
输出样例: 1 2 3 4 5 6 ***** *** * *** ***** 2
实现代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import java.util.Scanner;public class two { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入一个数值:" ); int q = scanner.nextInt(); System.out.println("请输入一个字符:" ); String t = scanner.next(); int f,g,n,p,u; int m=0 ; for (f=0 ;q>=m;f++){ m=m+(2 *f)*2 ; } p = (f-2 )*2 ; System.out.println("偶数型漏斗剩余:" + (q-m+2 *(p+2 ))); int k=0 ; for (g=2 ;(q-1 )>=k;g++){ k=k+(2 *g-1 )*2 ; } u = (g-3 )*2 +1 ; System.out.println("奇数型漏斗剩余:" + (q-1 -k+(u+2 )*2 )); int max = (p > u) ? p : u; n = max; for (int i=1 ;i<=(n+1 )/2 ;i++){ for (int a=1 ;a<=i-1 ;a++){ System.out.print(" " ); } for (int b=1 ;b<=((n+2 )-2 *i);b++){ System.out.print(t); } System.out.println(); } for (int j=(n+1 )/2 +1 ;j<=n;j++){ for (int c=1 ;c<=(n-j);c++){ System.out.print(" " ); } for (int d=1 ;d<=2 *j-n;d++){ System.out.print(t); } System.out.println(); } } }
题目3:个位数统计 要求: 给定一个 k 位(1=<k<9)整数N,请编写程序统计不同位上数字出现的次数。对 N 中不同位上数字,以 D:M
的格式在一行中输出该位数字 D
及其在 N 中出现的次数 M
。要求按 D
的升序输出。
输入样例:
输出样例:
实现代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 import java.util.Scanner;public class three { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入一个k位的整数(1<=k<9):" ); long n = scanner.nextLong(); long number = n; long number1 = n; int q=0 ,w=0 ,e=0 ,r=0 ,t=0 ,y=0 ,u=0 ,i=0 ,o=0 ,p=0 ; int k = 0 ; while (n > 0 ) { n /= 10 ; k++; } System.out.println(k); if (k >= 1 && k < 9 ){ System.out.println("输入的整数正确" ); for (int j=1 ;j<=k;j++){ number=number1; for (int h=j;h<k;h++) { number /= 10 ; } for (int m=j;m>1 ;m--){ number=number%10 ; } switch ((int ) number){ case 0 : q++; break ; case 1 : w++; break ; case 2 : e++; break ; case 3 : r++; break ; case 4 : t++; break ; case 5 : y++; break ; case 6 : u++; break ; case 7 : i++; break ; case 8 : o++; break ; case 9 : p++; break ; } } }else { System.out.println("输入的整数超出了规定范围" ); } int [] array = {q,w,e,r,t,y,u,i,o,p}; int [] num = {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 }; for (int z=0 ;z<array.length;z++){ if (array[z]!=0 ){ System.out.println(num[z] +" :" +array[z]); } } } }
题目4:求素数和 要求: 代码获取0到20以内所有素数,并输出和
实现代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import java.util.Scanner;public class four { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("输入一个数字,将求该数字以下的素数和:" ); int n = scanner.nextInt(); int sum=0 ; for (int i=0 ;i<=n;i++){ if (i==2 ){ sum=sum+i; } for (int j=2 ;j<i;j++){ if (i%j==0 ){ break ; } if (i==j+1 ){ sum=sum+i; } } } System.out.print(sum); } }
题目5:打印乘法表 要求: 使用嵌套循环输出乘法表
输出样例: 1 2 3 4 5 6 7 8 9 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
实现代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.Scanner;public class five { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("输入一个数字,将打印该数字的乘法表" ); int n = scanner.nextInt(); for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=i;j++){ System.out.print(j+"*" +i+"=" +(i*j)+"\t" ); } System.out.println(); } } }
题目6:输出GPLT 给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按GPLTGPLT....
这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT
的顺序打印,直到所有字符都被输出。
输入样例: 1 pcTclnGloRgLrtLhgljkLhGFauPewSKgt
输出样例:
实现代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 import java.util.Scanner;public class six { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入一串字符串:" ); String zw = scanner.nextLine(); int a=0 ,b=0 ,c=0 ,d=0 ,max=0 ; for (int i=0 ;i<zw.length();i++){ if (zw.charAt(i)=='g' ){ a++; } if (zw.charAt(i)=='G' ){ a++; } if (zw.charAt(i)=='p' ){ b++; } if (zw.charAt(i)=='P' ){ b++; } if (zw.charAt(i)=='l' ){ c++; } if (zw.charAt(i)=='L' ){ c++; } if (zw.charAt(i)=='t' ){ d++; } if (zw.charAt(i)=='T' ){ d++; } } int max1 = (a>b)?a:b; int max2 = (c>d)?c:d; max = (max1>max2)?max1:max2; for (int i=1 ;i<=max;i++){ if (a>0 ){ System.out.print("G" ); a--; } if (b>0 ){ System.out.print("P" ); b--; } if (c>0 ){ System.out.print("L" ); c--; } if (d>0 ){ System.out.print("T" ); d--; } } } }
题目7:判断日期 给定一个年份和365以内的天数,判断这一天位于该年份的几月几日。
输入样例:
输出样例:
实现代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import java.util.Scanner;public class seven { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入你想知道的年数:" ); int n = scanner.nextInt(); System.out.println("请输入你想知道的天数所在的具体位置:" ); int m = scanner.nextInt(); int b = m; if (n <= 0 ) { System.out.println("输入的年份不合法" ); System.exit(0 ); } if (m <= 0 || m >= 367 ) { System.out.println("输入的天数不合法" ); System.exit(0 ); } if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0 ) { int [] ry = {31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; for (int i = 0 ; i < 12 ; i++) { m = m - ry[i]; if (m <= 0 ) { System.out.println(n + "年" + b + "天" + "是第" + (i + 1 ) + "月" + (ry[i] + m) + "天" ); System.exit(0 ); } } } else { int [] py = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; for (int i = 0 ; i < 12 ; i++) { m = m - py[i]; if (m <= 0 ) { System.out.println(n + "年" + b + "天" + "是第" + (i + 1 ) + "月" + (py[i] + m) + "天" ); System.exit(0 ); } } } } }
题目8:后天 如果今天是星期三,后天就是星期五;如果今天是星期六,后天就是星期一。我们用数字1到7对应星期一到星期日。给定某一天,请你输出那天的“后天”是星期几。
输入格式: 输入第一行给出一个正整数D
(1 ≤ D
≤ 7),代表星期里的某一天。
输出格式: 在一行中输出D
天的后天是星期几。
输入样例:
输出样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import java.util.Scanner;public class eight { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入今天是星期几" ); int n = scanner.nextInt(); int tdl = n+2 ; if (tdl>7 ) { tdl = tdl - 7 ; } switch (tdl){ case 1 : System.out.println("两天后是星期一" ); break ; case 2 : System.out.println("两天后是星期二" ); break ; case 3 : System.out.println("两天后是星期三" ); break ; case 4 : System.out.println("两天后是星期四" ); break ; case 5 : System.out.println("两天后是星期五" ); break ; case 6 : System.out.println("两天后是星期六" ); break ; case 7 : System.out.println("两天后是星期日" ); break ; } } }
题目9:古风排版 中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。
输入格式: 输入在第一行给出一个正整数N (<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。
输出格式: 按古风格式排版给定的字符串,每列N 个字符(除了最后一列可能不足N 个)。
输入样例:
输出样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.util.Scanner;public class nine { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入一串字符串" ); String str = scanner.nextLine(); System.out.println("你希望一列打几个字符" ); int n = scanner.nextInt(); int z = str.length()/n; System.out.println(z); if (str.length()%n!=0 ){ z=z+1 ; } for (int i = 0 ; i < n; i++) { for (int j = z-1 ;j >= 0 ; j--){ int w = j * n + i; if (w < str.length()){ System.out.print(str.charAt(w)); }else { System.out.print(" " ); } } System.out.println(); } } }
题目10: 知乎上有个人问:“两个小时内如何学完 C 语言?”当然,问的是“学完”并不是“学会”。
假设一本 C 语言教科书有 N 个字,这个人每分钟能看 K 个字,看了 M 分钟。还剩多少字没有看?
输入格式: 输入在一行中给出 3 个正整数,分别是 N(不超过 400 000),教科书的总字数;K(不超过 3 000),是人每分钟能看的字数;M(不超过 120),是人看书的分钟数。
要保证人看完的字数不超过 N。
输出格式: 在一行中输出宝宝还没有看的字数。
输入样例:
输出样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.util.Scanner; public class ten { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("请问该本书的总字数为:"); int N = scanner.nextInt(); System.out.println("请问你每分钟可以看多少个字:"); int K = scanner.nextInt(); System.out.println("你能看多久呢?"); int M = scanner.nextInt(); if(N>=400000||K>=3000||M>=120){ System.out.println("输入错误,程序退出"); System.exit(0); } System.out.println("看了辣么久,你还有" + (N-(K*M)) + "个字没看完呢"); } }
管理系统 1.图书管理系统 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 //文件名为BookManageSystem //在ManageSystem软件包下 package ManageSystem; import java.util.ArrayList; import java.util.Scanner; public class BookManageSystem extends Thread{ public void run(){ System.out.println("图书管理系统启动"); System.out.println("欢迎来到图书管理系统"); Scanner myscanner = new Scanner(System.in); ArrayList list = new ArrayList(); list.add(new Book(1,"西游记","罗贯中",20)); list.add(new Book(2,"红楼梦","曹雪芹",30)); while(true){ printlist(); String process = myscanner.next(); switch(process){ case "1":{ System.out.println("1.查询图书"); for(Object o:list){ System.out.println(o); } break; } case "2":{ System.out.println("输入你想添加的图书信息,格式为:ID,书名,作者,数量"); list.add(new Book(myscanner.nextInt(),myscanner.next(),myscanner.next(),myscanner.nextInt())); System.out.println("添加成功"); break; } case "3":{ System.out.println("输入你想修改的图书ID"); int Bookid = myscanner.nextInt(); for(Object o : list){ Book b = (Book)o; if(b.getId()==Bookid){ System.out.println("输入你想修改的图书信息,格式为:书名,作者,数量"); b.setId(Bookid); b.setName(myscanner.next()); b.setAuthor(myscanner.next()); b.setNum(myscanner.nextInt()); System.out.println("修改成功"); break; }else{ System.out.println("没有找到该图书"); break; } } break; } case "4":{ System.out.println("删除图书"); int Bookid = myscanner.nextInt(); for(Object o : list) { Book b = (Book) o; if (b.getId() == Bookid) { list.remove(b); System.out.println("删除成功"); } } break; } case "5":{ System.out.println("退出系统"); return; } default:{ System.out.println("输入错误,请重新输入"); } } } } public static void printlist(){ System.out.println("输入1:查询图书 \t\t 输入2:添加图书"); System.out.println("输入3:修改图书 \t\t 输入4:删除图书"); System.out.println("输入5:退出系统"); System.out.println("请输入您的选择:"); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 //文件名为Book //在ManageSystem软件包下 package ManageSystem; public class Book{ private int id; private String name; private String author; private int num; public Book(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Book(int id, String name, String author, int num){ this.id=id; this.name=name; this.author=author; this.num=num; } @Override public String toString() { return "Book{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + ", num=" + num + '}'; } }
2.学生管理系统 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 //文件名为StudentManageSystem //在ManageSystem软件包下 package ManageSystem; package ManageSystem; import java.util.ArrayList; import java.util.Scanner; public class StudentManageSystem extends Thread{ public void run(){ System.out.println("学生管理系统启动"); System.out.println("欢迎来到学生管理系统~~~"); Scanner myscanner = new Scanner(System.in); ArrayList arr = new ArrayList(); arr.add(new Student(1,"张三","男",20,"B2201","22216482")); arr.add(new Student(2,"四儿","男",22,"B2201","34157289")); while(true){ printStudent(); int process = myscanner.nextInt(); switch(process){ case(1):{ System.out.println("查询学生信息"); for(Object o : arr){ Student s = (Student)o; System.out.println(o); } System.out.println("查询成功"); } break; case(2):{ System.out.println("添加学生的Id"); System.out.println("输入你想添加的学生信息,格式:Id,姓名,性别,年龄,班级,学号"); arr.add(new Student(myscanner.nextInt(), myscanner.next(),myscanner.next(), + myscanner.nextInt(),myscanner.next(),myscanner.next())); System.out.println("添加成功"); } break; case(3):{ System.out.println("修改学生信息"); System.out.println("输入你想修改的学生Id"); int studentId = myscanner.nextInt(); for(Object o : arr){ Student si = (Student)o; if(si.getId() ==studentId){ System.out.println("输入需要修改的学生信息,格式:姓名,性别,年龄,班级,学号"); si.setName(myscanner.next()); si.setSex(myscanner.next()); si.setAge(myscanner.nextInt()); si.setStudentClass(myscanner.next()); si.setStudentId(myscanner.next()); System.out.println("修改成功"); break; } else{ System.out.println("没有该学生Id"); } } } break; case(4):{ System.out.println("开除他"); System.out.println("输入你想开除的学生Id"); for(Object o : arr){ Student si = (Student)o; if(si.getId() == myscanner.nextInt()){ arr.remove(o); System.out.println("开除成功"); }else{ System.out.println("没有该学生Id,无法删除"); } } } break; case(5):{ System.out.println("退出系统"); return; } default:{ System.out.println("输入有误,请重新输入"); } } } } public static void printStudent(){ System.out.println("输入1:查询学生信息 \t\t 输入2:添加学生信息"); System.out.println("输入3:修改学生信息 \t\t 输入4:开除他"); System.out.println("输入5:退出系统"); System.out.println("请输入您的选择:"); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 //文件名为Student //在ManageSystem软件包下 package ManageSystem; public class Student { private int id; private String name; private String sex; private int age; private String studentClass; private String studentId; //构造方法 public Student(){ } public Student(int id,String name,String sex, int age, String studentClass, String studentId){ this.id=id; this.name=name; this.sex=sex; this.age=age; this.studentClass=studentClass; this.studentId=studentId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getStudentClass() { return studentClass; } public void setStudentClass(String studentClass) { this.studentClass = studentClass; } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + ", studentClass='" + studentClass + '\'' + ", studentId='" + studentId + '\'' + '}'; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 //文件名为StartMachine //在ManageSystem软件包下 package ManageSystem; import java.util.Scanner; public class StartMachine { public static void main(String[] args) { BookManageSystem BMS = new BookManageSystem(); StudentManageSystem SMS = new StudentManageSystem(); Scanner myscanner = new Scanner(System.in); informationList(); int process = myscanner.nextInt(); switch(process){ case 1:{ BMS.start(); } break; case 2:{ SMS.start(); } break; default:{ System.out.println("输入错误,请重新输入"); } } } public static void informationList(){ System.out.println("-------------------------------------"); System.out.println("欢迎使用管理系统"); System.out.println("1.图书管理系统 \n2.学生管理系统"); System.out.println("你希望登入哪一个管理系统?"); System.out.println("-------------------------------------"); } }
运行代码截图