- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序是使用 Java 创建一个火车路线查找器,首先作为命令行程序,然后转换为 GUI。这是我目前最不担心的事情。我目前停留在系统的一项功能上,那就是显示用户之前输入的两个车站之间的车站列表。
现在,我已经能够提示用户选择他们的停靠点(代码中的第 3,149 - 3,171 行):
if(deptChoice == 1 && arrChoice == 2){
List<String> stopList1_2 = new ArrayList<String>();
Scanner stopPick = new Scanner(System.in);
stopPick.useDelimiter(System.getProperty("line.separator"));
do {
System.out.println("\nCurrent list of stops between Leicester to Loughborough is:\n\n" + stopList1_2);
System.out.println("\nWould you like to add a new stop? (Please enter 'Yes' or 'No')\n");
if (stopPick.next().startsWith("Yes")) {
System.out.println("\nPlease type in the stop you wish to add to the route:\n");
stopList1_2.add(stopPick.next());
}
else {
break;
}
} while (true);
String[] stopArr1_2 = stopList1_2.toArray(new String[0]);
System.out.println("\nCurrent stops between Leicester to Loughborough is:\n\n" + Arrays.toString(stopArr1_2));
}
他们输入的停靠点将添加到数组中。当他们对车站之间的车站数量感到满意时,循环就会结束,并显示车站 X 和车站 Y 之间的车站数组。
但是,问题来了:
然后,我想访问之前创建的数组,并在我的代码中包含所有精确的停止点。在这个“if”语句中,如果满足,那么我希望显示数组(stopArr1_2)(第3,019 - 3,021行):
if(deptChoice == 1 && arrChoice == 2){
System.out.println(""); //this should be where I call the array back to display itself
}
因为这些站点将对应于用户选择 deptChoice == 1(车站 1 = 莱斯特)到 arrChoice == 2(车站 2 = 拉夫堡)。
我希望这一点很清楚:
我基本上希望用户输入的车站数组在他们选择火车路线时重新出现。
这是我在 Notepad++ 中编写的完整代码,我认为对你们来说,看到我的所有代码而不是小段代码会更好,并且它已被注释。
此外,为了理解我的问题,请运行我的程序。要了解我的问题,请执行以下操作:
1)编译Train.java(javac Train.java)
2)运行程序(java Train)
3) 选择管理菜单(编号 5)
4) 选择输入菜单(编号 1)
5) 选择任意两个电台
6) 输入几个站点,直到您满意为止
7)当它显示这两个车站的最终停靠站数组时,程序似乎结束了,希望这不是问题
8)再次运行程序(java Train)
9)选择火车路线(第3号)
10) 按照与之前相同的顺序选择相同的电台,这样它就会找到您刚刚创建的数组
11)现在什么也不会出现,因为,这就是我的问题。我不知道如何重新获得我刚刚为其制作的数组。
下载我的 Train.java 文件(以及 Train.txt 文件,如果需要)的链接:
https://www.dropbox.com/s/7dy0pp9vyykwhrk/TrainJava.rar
有什么建议吗?
最佳答案
你的第二个代码块的位置是什么? (您所说的需要访问 List<String> stopList1_2
的代码)?
1 如果它在同一个类中,但在不同的方法中,则
移动此声明 List<String> stopList1_2 = new ArrayList<String>();
(当前在您的第一个代码块中)到类字段。
但是
2 如果它与您的第一个代码块在同一个类中并且在同一个方法中,那么
移动此声明 List<String> stopList1_2 = new ArrayList<String>();
(当前在 if
代码语句内)到 if
之外条件。
但请将此视为解决您手头问题的快速方法。更重要的是 - 请阅读variable scoping rule and access specifiers in java .
关于java - 无法访问我的阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22490092/
我有一个关于将字符串分配给数组编号的问题。 我已经声明了字符串数组,例如。 String[] answer = {"yes", "no", "maybe"}; 如何在不使用这种方法的情况下将每个字符串
我正在为云数据库使用 Firebase 编写一个 Android 应用程序。它基本上是一个多项选择调查问题应用程序。导入到我的 Firebase { "multiple_choice" : {
我想将输入文件中的以下行存储到 3D 数组中(不包括第一行。)第一行表示后续行的数量。 3 4 9368 86 843 23224 4 7323 2 2665 2665 8447 47 843 527
这是我关于容器的小大问题,尤其是数组。 我正在编写一个物理代码,主要操纵一大组(> 1 000 000)“粒子”(每个粒子有 6 个 double 坐标)。我正在寻找最佳方式(在性能方面)来实现一个类
我有一个超链接,我需要在 Angular 4 中创建一个路由器链接。我有很多部分指向 url,其中一部分是一个数组。我不确定如何让数组将自己拆分成 routerlink 数组的部分。 以这个人为的例子
大家好,我有一个轮子选择器在工作,但目前它正在为所有轮子提取 0-9 的数字。我希望能够设置值而不是 0-9 我希望它是从数组或字符串中提取的单词,所以我可以输入它们 myslef 因为我不确定目前从
我正在尝试使用 Spotify API 并进入数组。 const App = () => { const [isLoading, setIsLoading] = useState(true);
我尝试创建 Tic Tac Toe,我能够填满我的棋盘,并且能够检查行和列以确定谁获胜。然而,我需要一些帮助来检查对角线,看看谁赢了。这是我到目前为止所拥有的。我是初学者,所以请不要让代码太难。 检查
--in the package type t_array is array (natural range <>) of std_logic_vector (7 downto 0); type p_a
我在访问字符串数组时遇到困难。它被声明为私有(private)数组并填充在类的构造函数中。我定义了一个 Get 函数。问题是当我在编译时调用此函数时出现错误,提示我无法访问在类中声明的私有(priva
无法弄清楚推送到 Moose 数组的语法(我确信这很明显,而且我很愚蠢)。这是 this question 的延续.在我看来,对于我的具体情况,我需要的不仅仅是一个简单的值。尝试使用 Moose 式的
我有一个 3d 数组,我正在尝试从中获取刺伤列表。换句话说,给定数组: t = np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]]) arr
我正在寻找绘制一个 3 维数组。有没有一种方法可以直接输入数组,绘制体素并在 3d 数组中的位置产生的坐标处绘制实际值(颜色)?到目前为止我发现的所有方法(例如 ax.voxels、mlab.poin
我正在尝试使用 Knockout 创建一个简单的电子表格。我试图让每个单元格都可观察,以便在发生变化时,我可以评估值并进行相应的计算。因此,如果他们在单元格中输入 6+7,我可以评估并将该值更改为总数
我有当前时间和这组时间。我想计算出下一次与当前时间最接近的时间。 let date = NSDate() let calendar = NSCalendar.currentCalendar() let
我想在我的小程序中创建一个二维图像数组。我需要一个 4x4 网格,其中有 4 个图像,每个图像 4 个随机分布在阵列中。这里有一些答案,但我不明白如何使用它们。 最佳答案 您可以声明 Image[][
基本上,此代码列出了“可用”挑战,其中 complete = 0 并在每个列表中都有一个接受submit 按钮。到目前为止,我一次只能列出一项,因为列出的多个按钮无法识别匹配 ID $echo 任何人
我正在尝试创建一个带有动态变量的过滤数组。我创建一个包含过滤器键的数组,然后创建一个过滤后的数组,该数组只应返回与第一个数组中的键匹配的项目。 带有过滤器键的数组:$scope.participant
我是一个相对年轻的开发人员,我对一些事情感到困惑。 这是我的代码: function pairElement(str) { var arr = []; var pairs = [
我正在 Angular 中创建一个函数,我想抓取所有博客文章,其类别与单击的按钮相匹配,我的 Firebase 中有 3 个不同的字段,标题为类别 1、类别 2 和类别 3。例如,当用户单击新闻通讯时
我是一名优秀的程序员,十分优秀!