扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
你跟我刚才回答的那个问题是同学吧,我刚贴出来。下次提问之前可以先搜一下
创新互联2013年至今,先为江城等服务建站,江城等地企业,进行企业商务咨询服务。为江城企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
---------------分割线---下面是代码,纯手打-----------------
//自己没有验证没有测试,可能会有错误,你自己看着改一下
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
HashMapString, ArrayList seat=new HashMap();
int row;
int column;
//创建座位
for(int i=1;i11;i++) {
ArrayList desk=new ArrayList();
for(int ii=0;ii8;ii++) {
desk.add("0");
}
seat.put("第"+i+"排", desk);
}
//开始选座
for() {
System.out.println("========开始选票===========");
//循环输出座位
for(int i=1;iseat.size();i++) {
System.out.print("第"+i+"排"+" ");
for(int ii=0;iseat.get("第"+i+"排").size();) {
System.out.print(seat.get("第"+i+"排").get(ii));
}
}
//开始选座
for() {
System.out.println("选择排数");
row=s.nextInt();
System.out.println("选择第几个,只有0可以选,x是已经被选了");
column=s.nextInt();
if(seat.get("第"+row+"排").get(column).equals("0")) {
seat.get("第"+row+"排").set(column, "X");
System.out.println("选座成功,您的座位是:第"+row+"排,第"+column+"列");
break;
}
}
}
}
给你写个简单的例子 并没有对参数做验证 理解下数组的用法够用了
真实的选座比例子复杂的多,包括同步等方面知识
public class Main {
public static void main(String[] args) {
//你描述不够清晰,我就假设 电影院 6排 每排8座
//座位数
int num = 48;
int[] arr = new int[num];
Main obj = new Main();
//调用选座方法
obj.selection(arr,1,1);
//同一个位置调用2次验证一下
obj.selection(arr,1,1);
}
/**
*
* @param arr 座位表
* @param x 排
* @param y 位
*/
public void selection(int[] arr, int x, int y) {
int index = (x - 1) * 6 + y - 1;
if (arr[index] == 0) {
//对应位置的元素设置为1,表示有人
arr[index] = 1;
System.out.println("选座成功:" + "第" + x + "排,第" + y + "座 ");
} else {
System.out.println("第" + x + "排,第" + y + "座 已经被人选啦");
}
}
}
import java.util.*;
public class a {
public static void main(String[] args) {
int i;
char c;
String[] a = {"座位1","座位2","座位3","座位4","座位5"};
/*生成座位list*/
ListString seat = new ArrayListString();
/*向其中添加座位*/
for(i = 0;i 5;i++){
seat.add(a[i]);
}
/*调用函数,将顺序打乱*/
Collections.shuffle(seat);
/*显示结果*/
for(i = 0,c = 'a'; c = 'e';c++,i++){
System.out.println("学生"+c+"坐在"+seat.get(i));
}
}
}
有问题追问我
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Kyo
{
private static final String TXT = "booking.txt";
private static final String LINE = System.getProperty("line.separator");
private static String readLine(int line, int count, String file)
{
try
{
Scanner scan = new Scanner(new File(file));
scan.skip("([^" + LINE + "]+[" + LINE + "]+)" + "{" + (line + 1) + "}");
String result = "";
while(count 0 scan.hasNextLine())
{
result += scan.nextLine().trim();
if(count 1)
{
result += LINE;
}
count--;
}
scan.close();
return result;
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
return "";
}
private static void setBestSeat(String[] seat, String tag, String members, int num)
{
int[] seats = new int[0];
int[] indexs = new int[0];
for(int i = 0; i seat.length; i++)
{
String s = seat[i];
boolean isExisted = s.contains(tag);
if(s.contains(tag))
{
for(int j = i + 1; j i + num; j++)
{
if(j seat.length - 1)
{
isExisted = false;
break;
}
String b = seat[j];
if(!b.contains(tag))
{
isExisted = false;
break;
}
}
int[] dest = new int[seats.length + 1];
System.arraycopy(seats, 0, dest, 0, seats.length);
dest[dest.length - 1] = i;
seats = dest;
}
// 是否有足够的连续数组元素(座位数)
if(isExisted)
{
int[] dest = new int[indexs.length + 1];
System.arraycopy(indexs, 0, dest, 0, indexs.length);
dest[dest.length - 1] = i;
indexs = dest;
}
}
String[] ms = members.split("[" + LINE + "]+");
if(indexs.length == 0)
{
indexs = seats;
String tmp = ",";
for(int i = 0; i ms.length; i++)
{
int rand = (int) (Math.random() * indexs.length);
int ir = indexs[rand];
if(tmp.indexOf("," + ir + ",") == -1)
{
seat[ir] = "Seat " + (ir + 1) + ": " + ms[i];
tmp += ir + ",";
}
else
{
i--;
}
}
}
else
{
int rand = (int) (Math.random() * indexs.length);
int ir = indexs[rand];
for(int k = ir; k ir + ms.length; k++)
{
seat[k] = "Seat " + (k + 1) + ": " + ms[k - ir];
}
}
}
public static void main(String[] args)
{
String[] seat = {
"Seat 1: -empty-",
"Seat 2: -empty-",
"Seat 3: -empty-",
"Seat 4: -empty-",
"Seat 5: -empty-",
"Seat 6: -empty-",
"Seat 7: -empty-",
"Seat 8: -empty-"
};
String[][] booking = new String[0][];
Scanner input = null;
int count = 0;
int number = 0;
try
{
input = new Scanner(new File(TXT));
while(input.hasNextLine())
{
String line = input.nextLine().trim();
if(line.matches("^\\d+$"))
{
int num = Integer.parseInt(line);
if(num != seat.length)
{
String result = readLine(count, num, TXT);
setBestSeat(seat, "-empty-", result, num);
}
num = num == seat.length ? 2 : num + 1;
String[][] dest = new String[booking.length + 1][];
System.arraycopy(booking, 0, dest, 0, booking.length);
dest[dest.length - 1] = new String[num];
booking = dest;
booking[number][0] = line;
number++;
}
count++;
}
booking[0][1] = Arrays.toString(seat).replaceAll("[\\[\\]]", "");
input.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
for(int i = 0; i seat.length; i++)
{
System.out.println(seat[i]);
}
System.out.println(Arrays.deepToString(booking));
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流