资源描述
/**
大家好,我现在正在学习java,虽然在这之前我已经学习过一遍了,但是现在再重新来学,才发现以前学的太肤浅了,而且学的质量也很不好,所以,现在我又重新站在了新的起跑线上,开始了我的java学习之旅,喜欢java的朋友和想学习java的朋友来和我一起前进吧。我会及时的把自己学的一些东西总结出来,并传送到文库中和大家一起分享的。
所以Make The Change的时候到了,Everyone,Come On!
(我的QQ号jiaziming1990@,愿意交流的同学可以加我呦)
*/
/*
1. 螺旋方阵:例:
1
2
3
4
5
16
17
18
19
6
15
20
7
14
……
22
21
8
13
12
11
10
9
代码实现如下:
*/
import java.util.Scanner;
public class SpiralSquare {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("请输入螺旋方阵的边长");
int index=s.nextInt();
if(index<=0){
System.out.println("输入的数字不合法!");
return;
}
int[][] square=new int[index][index];
int col=-1;//define column
int row=0;//define row
for(int i=1;i<=index*index;){
while(col+1<index&&square[row][col+1]==0)
square[row][++col]=i++;
while(row+1<index&&square[row+1][col]==0)
square[++row][col]=i++;
while(col-1>=0&&square[row][col-1]==0)
square[row][--col]=i++;
while(row-1>=0&&square[row-1][col]==0)
square[--row][col]=i++;
}
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();
}
}
}
/*
2. 螺旋矩阵:例:
1
2
3
4
5
14
15
16
17
6
13
20
19
18
7
12
11
10
9
8
代码实现如下:
*/
import java.util.Scanner;
public class SpiralSquare01{
public static void main(String[] args) {
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();
}
}
}
/*
3、模拟酒店房间管理系统,需要如下几个功能:
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) {
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;
}
}
展开阅读全文