我的答案是不同步的,不能为下次运行N. [英] My answer is not in sync and cannot run N for next
问题描述
您好,有人可以帮我解决这些问题。有2个问题。
对于乘客身份,我写了0然后BI是国家代码,095是航班号。 01是没有。行李
然后我跑完后会说:
bi0950
[0行李(s) )[bi0950]]
1)问题:如果我再次输入B并输入下一位乘客为1的详细信息,则BI为国家代码,095为航班号。 02是没有。行李。
我跑完后会说:
Hello, can somebody help me with the code. Have 2 questions.
For the passenger identity, i wrote 0 then BI is the country code, 095 is the flight no. 01 is the no. of baggage
then after I run, it will says:
bi0950
[0 with baggage(s) [bi0950]]
1) Questions: If I enter B again and enter the detail with the next passenger of 1, BI is the country code, 095 is the flight no. 02 is the no. of baggage.
after I run, it will says:
bi0950
bi0951
[0 with baggage(s) [bi0950, bi0950, bi0951], 1 with baggage(s) [bi0950, bi0950, bi0951]]
如你所见,它乘客不同步0假设有1个行李箱是bi0950,乘客1应该是bi0951,bi0952但毕竟乘客0也受影响。
问题2:
,下一行将是:输入命令B-baggage,n-next,q-quit
我输入n并运行,错误弹出如下:
As you can see, it is not sync like passenger 0 suppose to be have 1 luggage which is bi0950, passenger 1 should be bi0951,bi0952 but after all, passenger 0 is also affected.
Question 2:
and the next line will be: Enter command B-baggage, n-next, q-quit
I put in "n" and run, and the error pops out as follows:
java.util.NoSuchElementException
at java.util.LinkedList$ListItr.next(Unknown Source)
at baggageSys.next(baggageSys.java:43)
at baggageSys.main(baggageSys.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
这里有什么要解决的..?谢谢
我尝试过:
anything to fix here..? thanks
What I have tried:
import java.util.*;
public class baggageSys{
public static String getUser_command(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter command B-baggage, n-next, q-quit");
String s = keyboard.nextLine();
return s;
}
public static String getUser_flight(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the flight number");
String s = keyboard.nextLine();
return s;
}
public static String getPassenger(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter passenger name");
String s = keyboard.nextLine();
return s;
}
public static String getUser_country(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the country code");
String s = keyboard.nextLine();
return s;
}
public static int getUser_number(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter number of baggage");
int s = keyboard.nextInt();
return s;
}
public static String next(ListIterator<passenger> passenger){
String k = "";
passenger.next();
return k;
}
public static String makeBaggage(String country, String flight, int num){
return country + flight + num;
}
public static void main(String args[]) {
LinkedList<passenger> passenger = new LinkedList<passenger>();
ListIterator<passenger> iterator = passenger.listIterator();
LinkedList<string> baggage = new LinkedList<string>();
String command = "";
while (!command.equals("q")){
command = getUser_command();
if(command.equals("B") || command.equals("b")){
String p = "";
p = getPassenger();
passenger.add(new Passenger(p));
// command = getUser_command();
String country = "";
country = getUser_country();
String flight = "";
flight = getUser_flight();
int amount = 0;
amount = getUser_number();
String[] bg = new String[amount];
for(int i = 0; i < amount; i++){
bg[i] = makeBaggage(country, flight, i);
baggage.add(bg[i]);
System.out.println(bg[i]);
passenger.getLast().setBaggages(baggage);
}
System.out.println(passenger);
} else if(command.equals("n")){
next(iterator);
}else
System.out.println("Enter 'q' to end the program");
}
}
public static class Passenger {
String passengers;
List<string> baggage;
public Passenger(String passengers) {
this.passengers = passengers;
baggage = Collections.emptyList();
}
public void setBaggages(List<string> baggage) {
this.baggage = baggage;
}
@Override
public String toString() {
return passengers + " with baggage(s) " + baggage;
}
}
}
推荐答案
ListItr.next(未知来源)
at baggageSys.next(baggageSys.java:43)
at baggageSys.main(baggageSys.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect .NativeMethodAccessorImpl.invoke(未知来源)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice .cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
ListItr.next(Unknown Source) at baggageSys.next(baggageSys.java:43) at baggageSys.main(baggageSys.java:97) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
这里要解决的问题..?谢谢
我尝试过:
anything to fix here..? thanks
What I have tried:
import java.util.*;
public class baggageSys{
public static String getUser_command(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter command B-baggage, n-next, q-quit");
String s = keyboard.nextLine();
return s;
}
public static String getUser_flight(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the flight number");
String s = keyboard.nextLine();
return s;
}
public static String getPassenger(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter passenger name");
String s = keyboard.nextLine();
return s;
}
public static String getUser_country(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the country code");
String s = keyboard.nextLine();
return s;
}
public static int getUser_number(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter number of baggage");
int s = keyboard.nextInt();
return s;
}
public static String next(ListIterator<passenger> passenger){
String k = "";
passenger.next();
return k;
}
public static String makeBaggage(String country, String flight, int num){
return country + flight + num;
}
public static void main(String args[]) {
LinkedList<passenger> passenger = new LinkedList<passenger>();
ListIterator<passenger> iterator = passenger.listIterator();
LinkedList<string> baggage = new LinkedList<string>();
String command = "";
while (!command.equals("q")){
command = getUser_command();
if(command.equals("B") || command.equals("b")){
String p = "";
p = getPassenger();
passenger.add(new Passenger(p));
// command = getUser_command();
String country = "";
country = getUser_country();
String flight = "";
flight = getUser_flight();
int amount = 0;
amount = getUser_number();
String[] bg = new String[amount];
for(int i = 0; i < amount; i++){
bg[i] = makeBaggage(country, flight, i);
baggage.add(bg[i]);
System.out.println(bg[i]);
passenger.getLast().setBaggages(baggage);
}
System.out.println(passenger);
} else if(command.equals("n")){
next(iterator);
}else
System.out.println("Enter 'q' to end the program");
}
}
public static class Passenger {
String passengers;
List<string> baggage;
public Passenger(String passengers) {
this.passengers = passengers;
baggage = Collections.emptyList();
}
public void setBaggages(List<string> baggage) {
this.baggage = baggage;
}
@Override
public String toString() {
return passengers + " with baggage(s) " + baggage;
}
}
}
我无法弄清楚问题1,我担心代码不容易了解。第二个问题仅仅是你没有初始化 ListIterator
。即使你有,它似乎没有任何目的。我建议你去 Java™教程 [ ^ ]并进行更多研究。
I cannot figure out problem 1, I'm afraid the code is not easy to understand. The second problem is merely that you have not initialise the ListIterator
. And even if you had, it does not seem to serve any purpose. I would suggest going to The Java™ Tutorials[^] and doing some more study.
这篇关于我的答案是不同步的,不能为下次运行N.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!