一、模擬酒店房間管理系統(tǒng),需要如下幾個(gè)功能:
成都創(chuàng)新互聯(lián)公司總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號(hào)搭建、小程序制作、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動(dòng)行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
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ù)組來實(shí)現(xiàn)
代碼實(shí)現(xiàn)如下:
import java.util.Scanner;
public class HotelDemo {
//寫在類里面,則每個(gè)方法都可以訪問到,避免了參數(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;
}
}二、螺旋矩陣 例
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)輸入螺旋方陣的長");
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é)問題:百雞問題的變形
題目描述:有36個(gè)人,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)){
//注意:孩子的人數(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秒的格式輸出出來
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ī)密碼;
//密碼的自動(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ù)字;
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) 月 霜 光
。 , 。 ,
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ù)就可以;
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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。
分享文章:Java編程學(xué)習(xí)的幾個(gè)典型實(shí)例詳解
網(wǎng)站URL:http://www.chinadenli.net/article34/gpcpse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、虛擬主機(jī)、網(wǎng)站維護(hù)、企業(yè)網(wǎng)站制作、網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)