java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的 Java 程序设计语言和 Java 平台(即 JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
这篇文章主要给大家介绍了 Java 编程学习的几个典型实例,其中包括模拟酒店房间管理系统、螺旋矩阵 例或者百鸡问题的变形等经典实例,具体来一起看详细内容吧,需要的朋友可以参考学习。
一、模拟酒店房间管理系统,需要如下几个功能:
1、1 in 房间号 客人名字 入住功能
1、2 out 房间号 退房功能
1、3 search 房间号 查询房间状态 如果房间号为 - 1 则输出所有房间状态
1、4 quit 或 exit 退出
提示:酒店所有的房间用二维数组来实现
代码实现如下:
- import java.util.Scanner;
- public class HotelDemo {
- //写在类里面,则每个方法都可以访问到,避免了参数传递的繁琐;
- static int h = 5,
- w = 10;
- static String[][] rooms = new String[5][10];
- public static void main(String[] args) {@SuppressWarnings("resource") Scanner s = new Scanner(System. in );
- while (true) {
- System.out.println("请输入 in,out,search,quit:");
- String temp = s.next();
- int room = 0;
- if ("in".equals(temp)) { //防止出现空指针异常;
- System.out.println("输入房间号:");
- room = s.nextInt();
- System.out.println("输入名字:");
- String name = s.next();
- if ( in (room, name)) System.out.println("入住完成!");
- System.out.println("room" + room + "name" + name);
- } else if ("out".equals(temp)) {
- System.out.println("输入房间号:");
- room = s.nextInt();
- if (out(room)) System.out.println("退房完成!");
- System.out.println("out" + room);
- } else if ("search".equals(temp)) {
- System.out.println("输入房间号(-1代表全部):");
- room = s.nextInt();
- search(room);
- } else if ("quit".equals(temp) || "exit".equals(temp)) {
- break;
- } else {
- System.out.println("命令错误!");
- }
- }
- }
- private static boolean search(int room) {
- if (room == -1) {
- //打印所有的信息;
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int room2 = (i + 1) * 100 + j + 1;
- System.out.print(room2 + "\t");
- }
- System.out.println();
- for (int k = 0; k < w; k++) {
- System.out.print(rooms[i][k] == null ? "empty": rooms[i][k]);
- System.out.print("\t");
- }
- System.out.println();
- System.out.println();
- }
- return true;
- } else {
- int r = room / 100 - 1;
- int c = room % 100 - 1;
- if (r < 0 || r >= h || c < 0 || c >= w) {
- System.out.println("房间号错误!");
- return false;
- }
- System.out.println(rooms[r][c] == null ? "empty": rooms[r][c]);
- return true;
- }
- }
- private static boolean out(int room) {
- int r = room / 100 - 1;
- int c = room % 100 - 1;
- if (r < 0 || r >= h || c < 0 || c >= w) {
- System.out.println("房间号错误!");
- return false;
- }
- if (rooms[r][c] == null || "".equals(rooms[r][c])) { //
- System.out.println("此房间没有人!");
- return false;
- }
- rooms[r][c] = null;
- return true;
- }
- private static boolean in (int room, String name) {
- int r = room / 100 - 1;
- int c = room % 100 - 1;
- if (r < 0 || r >= h || c < 0 || c >= w) {
- System.out.println("房间号错误!");
- return false;
- }
- if (rooms[r][c] != null) { //
- System.out.println("此房间已经有人!");
- return false;
- }
- rooms[r][c] = name;
- return true;
- }
- }
二、螺旋矩阵 例
- import java.util.Scanner;
- public class SpiralSquare01{
- public static void main(String[]
- args) {
- @SuppressWarnings("resource")
- Scanner
- s=new Scanner(System.in);
- System.out.println("请输入螺旋方阵的长");
- int indexY=s.nextInt();
- System.out.println("请输入螺旋方阵的宽");
- int indexX=s.nextInt();
- if(indexX<=0||indexY<=0){
- System.out.println("输入的数字不合法!");
- return;
- }
- int[][]
- square=new int[indexX][indexY];
- int x=0;
- int y=0;
- for(int i=1;i<=indexX*indexY;){
- while(y<square[x].length-1&&square[x][y+1]==0){
- square[x][y++]=i++;
- }
- while(x<square.length&&square[x][y]==0){
- square[x++][y]=i++;
- }
- while(y>0&&square[x-1][y-1]==0){
- square[x-1][--y]=i++;
- }
- --x;
- while(x>1&&square[x-1][y]==0){
- square[--x][y]=i++;
- }
- y++;
- }
- for(int i=0;i<square.length;i++){
- for(int j=0;j<square[i].length;j++){
- System.out.print(square[i][j]+"\t");
- }
- System.out.println();
- }
- }
- }
运行结果:
三、经典数学问题:百鸡问题的变形
题目描述:有 36 个人,36 块砖,每人搬了一次,正好搬完。其中男每人每次搬 4 块,女每人每次搬 3 块,小孩两人每次搬一块。问 男、女、小孩各多少人?
- public class TestBrick
- {
- public static void main(String[]
- args) {
- int manNum=0;
- int womanNum=0;
- for(int i=0;i<=9;i++){
- for(int j=0;j<12;j++){
- if(((i*4+j*3+(36-i-j)/2)==36)&&((36-i-j)%2==0)){
- //注意:孩子的人数必须是偶数,否则会出现一个孩子一次也没有搬的情况,不符合题意
- manNum=i;
- womanNum=j;
- System.out.println("男的的人数是"+manNum);
- System.out.println("女的的人数是"+womanNum);
- System.out.println("孩子的人数是"+(36-manNum-womanNum));
- }
- }
- }
- }
- }
四、倒计时的算法:输入一个秒数,要求转换为 XX 小时 XX 分 XX 秒的格式输出出来
- import java.util.Scanner;
- public class TestTime {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- @SuppressWarnings("resource") Scanner s = new Scanner(System. in );
- System.out.println("请输入秒数:");
- int second = s.nextInt();
- int hour = second / 3600;
- int minite = second % 3600 / 60;
- int sec = second % 60;
- System.out.println("转换后为:" + hour + "小时" + minite + "分" + sec + "秒");
- }
- }
五、密码的自动生成器:密码由大写字母 / 小写字母 / 数字组成,生成六位随机密码;
- //密码的自动生成器:密码由大写字母/小写字母/数字组成,生成六位随机密码;
- import java.util.Random;
- public class TestPassword
- {
- /**
- *
- @param args
- */
- public static void main(String[]
- args) {
- //
- TODO Auto-generated method stub
- char[]
- pardStore=new char[62];
- //把所有的大写字母放进去
- for(int i=0;i<20;i++){
- pardStore[i]=(char)('A'+i);
- }
- //把所有的小写字母放进去
- for(int i=26;i<52;i++){
- pardStore[i]=(char)('a'+i);
- }
- //吧0到9放进去
- for(int i=52;i<62;i++){
- pardStore[i]=(char)('0'+(i-52));
- }
- //生成6位随机密码
- Random
- r=new Random();
- for(int i=0;i<6;i++){
- int n=r.nextInt(62);
- System.out.print(pardStore[n]);
- }
- }
- }
六、写一个彩票的生成代码: 1-33 随机选 7 个不重复的数字;
- import java.util.Random;
- //写一个彩票的生成代码: 1-33随机选7个不重复的数字;
- public class TestLuckyTicket {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] luckTickets = new int[7];
- Random r = new Random();
- for (int i = 0; i < luckTickets.length; i++) {
- luckTickets[i] = r.nextInt(8) + 1;
- for (int j = 0; j < i; j++) {
- if (luckTickets[i] == luckTickets[j]) {
- i--;
- break;
- }
- }
- }
- for (int i = 0; i < luckTickets.length; i++) {
- System.out.print(luckTickets[i] + ",");
- }
- }
- }
七、定义一个字符串变量 String str="床前明月光,疑是地上霜。举头望明月,低头思故乡。"。
打印成如下格式的:
低 举 疑 床
头 头 是 前
思 望 地 明
故 明 上 月
乡 月 霜 光
。 , 。 ,
- public class TestPoet
- {
- /**
- *
- @param args
- */
- public static void main(String[]
- args) {
- //
- TODO Auto-generated method stub
- String
- str="床前明月光,疑是地上霜。举头望明月,低头思故乡。";
- char[]
- poet=str.toCharArray();
- int l=18;
- boolean flag=true;
- int i=0;
- while(flag){
- for(int j=l;j>=(0+i);){
- System.out.print(poet[j]);
- j=j-6;
- }
- System.out.println();
- l++;
- i++;
- if(l==24){flag=false;}
- }
- }
- }
八、九宫格的输出:九宫格就是每一行,每一列,斜着的一列和反斜着的一列的所在的数字之和均相等;最基本的是三行三列 = 9 格就是很出名的九宫格;还可以推广到 5*5=25 个格;只要行和列的个数均相等并且是奇数就可以;
- import java.util.Scanner;
- public class JiuGongGe {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- @SuppressWarnings("resource") Scanner s = new Scanner(System. in );
- System.out.println("请输入一个大于等于3的奇数");
- int length = s.nextInt();
- if (length < 3 || length % 2 == 0) {
- System.out.println("输入的数字不合法!");
- return;
- }
- int[][] nineTable = new int[length][length];
- int indexX = 0;
- int indexY = 0;
- indexY = (nineTable.length - 1) / 2;
- nineTable[indexX][indexY] = 1;
- for (int i = 1; i < nineTable.length * nineTable.length; i++) {
- indexX--;
- indexY++;
- if (indexY >= nineTable.length && indexX >= 0) {
- indexY = 0;
- } else if (indexX < 0 && indexY < nineTable.length) {
- indexX = nineTable.length - 1;
- } else if (indexY >= nineTable.length && indexX < 0) {
- indexY--;
- indexX = indexX + 2;
- } else if (nineTable[indexX][indexY] != 0) {
- indexY--;
- indexX = indexX + 2;
- }
- nineTable[indexX][indexY] = i + 1;
- }
- for (int i = 0; i < nineTable.length; i++) {
- for (int j = 0; j < nineTable[i].length; j++) {
- System.out.print(nineTable[i][j] + " ");
- }
- System.out.println();
- System.out.println();
- }
- }
- }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对 PHPERZ 的支持。
来源: http://www.phperz.com/article/17/1219/358271.html