循环一次询问太多输入

时间:2015-12-11 21:30:49

标签: java loops

最近我一直在研究Java,我遇到了一个循环的小问题,我希望程序要做的是,例如,如果用户为乘客数量输入2,程序会要求输入名称两者都保存并打印名称。

其中一个主要问题是该计划同时要求两位乘客提供详细信息 - 看起来像这样:

Please enter the number of passengers:
2
Please enter passenger 1 details:
Please enter passenger 2 details:

此外,它只需要一个名字并在最后打印出来但是应该显示该机票上所有乘客的列表:

    count=1;
    do{
        do{
            System.out.println("Please enter passenger " + count +" details:");
            passengerName=keyboard.nextLine();
            count++;

        }while (count<=numOfPassengers);
        keyboard.nextLine();
    }while (count<=numOfPassengers);

    passengerInfo +="\nPassenger "+count+": "+passengerName;
    keyboard.nextLine();

    //ticket printout section
    System.out.println("Ticket");
    System.out.println("======");
    System.out.println("Number of Passengers: "+numOfPassengers);
    System.out.println(passengerInfo);
   }       
}

2 个答案:

答案 0 :(得分:2)

考虑这些变化 - 基于以下事实:您现在似乎对持有所有乘客信息的String感到满意:

    String passengerInfo = "";
    for(int i = 0; i < numOfPassengers; ++i) {
        System.out.println("Please enter passenger " + (i+1) +" details:");
        passengerInfo +="\nPassenger "+(i+1)+": "+keyboard.nextLine();
    }

    //ticket printout section
    System.out.println("Ticket");
    System.out.println("======");
    System.out.println("Number of Passengers: "+numOfPassengers);
    System.out.println(passengerInfo);

作为下一步,您可能需要考虑将乘客收集到列表中:

    List<String> passengers = new ArrayList<String>();
    for(int i = 0; i < numOfPassengers; ++i) {
        System.out.println("Please enter passenger " + (i+1) +" details:");
        String name = keyboard.nextLine();
        passengerInfo +="\nPassenger "+(i+1)+": "+name;
        passengers.add(name);
    }

答案 1 :(得分:0)

尝试这样的事情:

.opinion {
  background-color: #fff;
  font-size: .8em;
  padding-top: 25px;
  padding-bottom: 25px;
}

.opinion:after {
  clear: both;
  content: "";
  display: block;
}

.opinion .atable {
  display: table;
  border-collapse: separate;
  border: 0px;
  padding: 0px;
  outline: none;
  border-spacing: 1em;
  width: 100%;
  table-layout: fixed;
}

.opinion .atable .acell {
  display: table-cell;
  height: 100%;
  width: 25%;
  overflow: hidden;
  border-radius: 3px;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border: 1px solid #ccc;
  background-color: #fff;
  outline: none;
  vertical-align: top;
}

.opinion .atable .acell:hover {
  -webkit-box-shadow: 0px 5px 7px 0px rgba(50, 50, 50, 0.25);
  -moz-box-shadow: 0px 5px 7px 0px rgba(50, 50, 50, 0.25);
  box-shadow: 0px 5px 7px 0px rgba(50, 50, 50, 0.25);
}

.opinion .atable .acell:last-child {
  margin: 0px;
}

div.opinion .atable .acell .contentbox ul {
  padding: 0;
  min-width: 40px;
  widows: 100%;
  margin: 0;
}

div.opinion ul.slides li:hover:after {
  display: block;
  width: calc(100% - 20px);
  height: calc(100% - 20px);
  left: 9px;
  top: 9px;
  position: absolute;
  border: 1px solid white;
  content: ' ';
  pointer-events: none;
}
div.opinion .atable .acell .contentbox ul li a span {
    padding: 20px;
}

.opinion .atable .acell .contentbox ul.slides:hover a>span {
  position: absolute;
  bottom: 0px;
  left: 0px;
  padding: 20px;
  color: #fff;
  z-index: 10;
  display: inline-block;
}

.opinion .atable .acell .contentbox {
  position: relative;
}

.slideImg {
  display: none;
}

.opinion .atable .acell .contentbox ul {
  list-style-type: none;
}

.opinion .atable .acell .contentbox ul li a {
  color: white;
  display: block;
  height: 200px;
  max-width: 100%;
  width: 100%;
  background-size:cover;
  background-position:center;
}

.opinion .atable .acell .contentbox ul li a span {
  position: absolute;
  bottom: 0px;
  left: 0px;
  padding: 10px 10px 20px 10px;
  color: #fff;
  z-index: 10;
  display: inline-block;
  font-size: 1.3em;
  text-shadow: 1px 1px 1px #000;
  -webkit-border-top-left-radius: 4px;
  -moz-border-radius-topleft: 4px;
  border-top-left-radius: 4px;
}

.opinion .atable .acell .contentbox ul li a:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  box-shadow: inset 0 0 0 350px blue;
  width: 100%;
  height: auto;
}