一、模擬酒店房間管理系統(tǒng),需要如下幾個(gè)功能:
1、1 in 房間號(hào) 客人名字 入住功能
1、2 out 房間號(hào) 退房功能
1、3 search 房間號(hào) 查詢房間狀態(tài) 如果房間號(hào)為-1 則輸出所有房間狀態(tài)
1、4 quit 或 exit 退出
提示:酒店所有的房間用二維數(shù)組來(lái)實(shí)現(xiàn)
代碼實(shí)現(xià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
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 HotelDemo { //寫在類里面,則每個(gè)方法都可以訪問(wèn)到,避免了參數(shù)傳遞的繁瑣; 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( "請(qǐng)輸入 in,out,search,quit:" ); String temp=s.next(); int room= 0 ; if ( "in" .equals(temp)){ //防止出現(xiàn)空指針異常; System.out.println( "輸入房間號(hào):" ); 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( "輸入房間號(hào):" ); room=s.nextInt(); if (out(room)) System.out.println( "退房完成!" ); System.out.println( "out" +room); } else if ( "search" .equals(temp)){ System.out.println( "輸入房間號(hào)(-1代表全部):" ); room=s.nextInt(); search(room); } else if ( "quit" .equals(temp)|| "exit" .equals(temp)){ break ; } else { System.out.println( "命令錯(cuò)誤!" ); } } } 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( "房間號(hào)錯(cuò)誤!" ); 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( "房間號(hào)錯(cuò)誤!" ); 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( "房間號(hào)錯(cuò)誤!" ); return false ; } if (rooms[r][c]!= null ){ // System.out.println( "此房間已經(jīng)有人!" ); return false ; } rooms[r][c]=name; return true ; } } |
二、螺旋矩陣 例
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
|
import java.util.Scanner; public class SpiralSquare01{ public static void main(String[] args) { @SuppressWarnings ( "resource" ) Scanner s= new Scanner(System.in); System.out.println( "請(qǐng)輸入螺旋方陣的長(zhǎng)" ); int indexY=s.nextInt(); System.out.println( "請(qǐng)輸入螺旋方陣的寬" ); int indexX=s.nextInt(); if (indexX<= 0 ||indexY<= 0 ){ System.out.println( "輸入的數(shù)字不合法!" ); 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(); } } } |
運(yùn)行結(jié)果:
三、經(jīng)典數(shù)學(xué)問(wèn)題:百雞問(wèn)題的變形
題目描述:有36個(gè)人,36塊磚,每人搬了一次,正好搬完。其中男每人每次搬4塊,女每人每次搬3塊,小孩兩人每次搬一塊。問(wèn) 男、女、小孩各多少人?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 )){ //注意:孩子的人數(shù)必須是偶數(shù),否則會(huì)出現(xiàn)一個(gè)孩子一次也沒有搬的情況,不符合題意 manNum=i; womanNum=j; System.out.println( "男的的人數(shù)是" +manNum); System.out.println( "女的的人數(shù)是" +womanNum); System.out.println( "孩子的人數(shù)是" +( 36 -manNum-womanNum)); } } } } } |
四、倒計(jì)時(shí)的算法:輸入一個(gè)秒數(shù),要求轉(zhuǎn)換為XX小時(shí)XX分XX秒的格式輸出出來(lái)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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( "請(qǐng)輸入秒數(shù):" ); int second =s.nextInt(); int hour=second/ 3600 ; int minite=second% 3600 / 60 ; int sec=second% 60 ; System.out.println( "轉(zhuǎn)換后為:" +hour+ "小時(shí)" +minite+ "分" +sec+ "秒" ); } } |
五、密碼的自動(dòng)生成器:密碼由大寫字母/小寫字母/數(shù)字組成,生成六位隨機(jī)密碼;
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
|
//密碼的自動(dòng)生成器:密碼由大寫字母/小寫字母/數(shù)字組成,生成六位隨機(jī)密碼; 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 ]; //把所有的大寫字母放進(jìn)去 for ( int i= 0 ;i< 20 ;i++){ pardStore[i]=( char )( 'A' +i); } //把所有的小寫字母放進(jìn)去 for ( int i= 26 ;i< 52 ;i++){ pardStore[i]=( char )( 'a' +i); } //吧0到9放進(jìn)去 for ( int i= 52 ;i< 62 ;i++){ pardStore[i]=( char )( '0' +(i- 52 )); } //生成6位隨機(jī)密碼 Random r= new Random(); for ( int i= 0 ;i< 6 ;i++){ int n=r.nextInt( 62 ); System.out.print(pardStore[n]); } } } |
六、寫一個(gè)彩票的生成代碼: 1-33隨機(jī)選7個(gè)不重復(fù)的數(shù)字;
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.Random; //寫一個(gè)彩票的生成代碼: 1-33隨機(jī)選7個(gè)不重復(fù)的數(shù)字; 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]+ "," ); } } } |
七、定義一個(gè)字符串變量String str="床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉(xiāng)。"。
打印成如下格式的:
低 舉 疑 床
頭 頭 是 前
思 望 地 明
故 明 上 月
鄉(xiāng) 月 霜 光
。 , 。 ,
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
|
public class TestPoet { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str= "床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉(xiāng)。" ; 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 ;} } } } |
八、九宮格的輸出:九宮格就是每一行,每一列,斜著的一列和反斜著的一列的所在的數(shù)字之和均相等;最基本的是三行三列=9格就是很出名的九宮格;還可以推廣到5*5=25個(gè)格;只要行和列的個(gè)數(shù)均相等并且是奇數(shù)就可以;
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
|
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( "請(qǐng)輸入一個(gè)大于等于3的奇數(shù)" ); int length=s.nextInt(); if (length<3||length%2==0){ System.out.println( "輸入的數(shù)字不合法!" ); 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(); } } } |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。