双色球
/*
双色球 6颗红球(33选1) 1颗蓝球(16选1)
*/
import java.util.Random;
import java.util.Arrays;
class StringDemo2
{
public static void main(String[] args)
{
String[] balls = getBalls();
System.out.println(Arrays.toString(balls));
}
public static String[] getBalls(){
String[] pool = {
"01","02","03","04","05","06","07","08","09","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32","33"
};
//用一个数组存放六个蓝色的球
String[] balls = new String[6];
int length = 0; //用来计数
/*从pool数组中随机选取6个球,放入数组中,并且不能有重复
1.如何随机的取
Random r = new Random();
r.nextInt(n);//取得0-1之间的随机的整数,只要获取随机的pool的下标就代表制定的球
2.如何不重复
一个下标的球被使用我们给它加个标示
boolean[] flag = new boolean[pool.length];数组中元素默认值为false
*/
boolean[] used= new boolean[pool.length];
Random r = new Random();
while(true)
{
int index = r.nextInt(pool.length);
if(used[index])//说明该下标已经被使用过
continue;
balls[length++] = pool[index];//把选中的球放入balls数组
used[index]=true;//把选中的球标示为已经使用过
if (length == balls.length)break;
}
//红色的球已经选完。
Arrays.sort(balls);
//扩容一个空间,最后一个放蓝球
balls = Arrays.copyOf(balls,balls.length+1);
//蓝色的球16选1即可
balls[balls.length-1] = pool[r.nextInt(16)];
return balls;
}
}
酒店前台客房管理系统
酒店前台客房管理系统
*/
import java.util.Scanner;
class HotelManagerSystem
{
public static void main(String[] args)
{
/*代表楼层所有的房间
房间编号0101 0102
0201
1001
*/
//字符串数组如果没有初始化元素,每个元素都null
String[][] rooms = new String[12][10];
System.out.println("欢迎来到XXX大酒店");
System.out.println("请输入要操作的命令:search查询,in办理入住,out办理退房,quit退出");
Scanner s = new Scanner(System.in);
//比较字符串是否相同 "in".equals(command)
while(true)
{
String command = s.next();
if("init".equals(command))
{
init(rooms);
}else if ("search".equals(command)){
search(rooms);
}else if ("in".equals(command))
{
in(rooms);
}else if ("out".equals(command))
{
out(rooms);
}else if ("quit".equals(command))
{
System.out.println("系统退出");
//break;
return;
}else{
System.out.println("输入有误,请重新输入");
}
}
}
/*办理退房(输入判断房间是否存在,是否有入住)
将来需要大家补充:除房间号还需要输入入住者的名称,要核对
*/
public static void out(String[][] rooms){
System.out.println("请输入房间号:");
Scanner s = new Scanner(System.in);
int roomNo = s.nextInt();
int floor = roomNo / 100;//得到楼层
int no = roomNo % 100;//得到房间号
if (floor<1 || floor >12 || no <1 || no>10)
{
System.out.println("输入的房间号有误,请继续输入out命令继续操作!");
return;
}
if("EMPTY".equals(rooms[floor-1][no-1]))
{
System.out.println("该房间没有入住,不需要退房,请继续输入out命令继续操作");
return;
}
rooms[floor-1][no-1]= "EMPTY";
System.out.println("该房间退房成功");
}
//办理入住-->直接输入房间号,进行入住
//将来可以先列出可入住的房间,再让用户去输入房间号
public static void in(String[][] rooms){
System.out.println("请输入房间号");
Scanner s = new Scanner(System.in);
int roomNo = s.nextInt();
//需要把房间号,转换成楼层和房间-->使其和数组的下标去对应
int floor = roomNo / 100; //根据房间号得到楼层 1202
int no = roomNo%100; //得到楼层的房间号
if (floor < 1 || floor>12|| no<1 || no>10 ){
System.out.println("输入的房间号有误请输入in命令继续输入!");//do..while实现
//如何结束函数,函数遇到return就结束
return;
}
//继续判断房间是否有人入住
if(!"EMPTY".equals(rooms[floor-1][no-1])){
System.out.println(roomNo+"已经有人入住,请输入in命令继续操作");//0的处理
return;
}
System.out.println("请输入您的姓名:");
String name = s.next();
rooms[floor-1][no-1] = name;
System.out.println("恭喜您,入住成功");
}
//初始化所有房间
public static void init(String[][] rooms)
{
for (int i = 0;i<rooms.length ;i++ )
{
for (int j =0;j <rooms[i].length ;j++ )
{
rooms[i][j]="EMPTY";
}
}
System.out.println("房间初始化完毕!");
}
//查询所有房间-->(自己在做的时候输入具体房间号,或者房间的状态进行查询)
public static void search(String[][] rooms)
{
for (int i =0;i<rooms.length ;i++ )
{
//打印房间号
for (int j = 0;j<rooms[i].length ;j++ )
{
if(i< 9){
System.out.print("0");
}
int roomNo=(i+1)*100+j+1;
System.out.print(roomNo+"\t");
}
System.out.println();
//打印房间的状态
for (int k =0;k<rooms[i].length ;k++ )
{
System.out.print(rooms[i][k]+"\t");
}
System.out.println();
}
}
}
方法的声明和使用
所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块。
一般情况下,定义一个方法的语法是:
访问修饰符 返回值类型 方法名(){
方法体
}
其中:
1、 访问修饰符:方法允许被访问的权限范围, 可以是 public、protected、private 甚至可以省略 ,其中 public 表示该方法可以被其他任何代码调用。
2、 返回值类型:方法返回值的类型,如果方法不返回任何值,则返回值类型指定为 void ;如果方法具有返回值,则需要指定返回值的类型,并且在方法体中使用 return 语句返回值
3、 方法名:定义的方法的名字,必须使用合法的标识符。
方法命名规范要求
在定义类时,全部单词的首字母必须大写,那么在定义时也有命名规范要求,即第一个单吃的首字母小写,之后每个单词的首字母大写,如printInfo()方法。
4、 参数列表:传递给方法的参数列表,参数可以有多个,多个参数间以逗号隔开,每个参数由参数类型和参数名组成。