【发布时间】:2015-11-13 19:05:17
【问题描述】:
我正在为一组我不知道的动物编写一个 while-Loop 程序。用户可以输入他们想要的数量,但在输入“灭绝”时终止它。
这是我需要为该程序做的事情: 编写一个程序,使用 while 循环反复询问用户 命名一种动物,然后询问野外还剩下多少。当输入特殊代码 EXTERMINATE 时它应该停止,并命名最濒危的动物。
命名一种动物?科莫多巨蜥 野外还剩多少? 5000 给动物起名字?海牛 野外还剩多少? 8000 给动物起名字?鸮鹦鹉 野外还剩多少? 91 给动物起名字?佛罗里达豹 野外还剩多少? 100 给动物起名字?消灭
濒危程度最低的动物是海牛。 野外还有8000只。
我有两个问题。首先,我无法让程序存储所有动物名称以输出最后一个濒临灭绝的动物。所以我在想是否应该使用数组来跟踪它们?
其次,当我输入 exterminate 时,程序在用户插入 exterminate 后完成 while 循环而不中断。
请不要让它复杂,因为我是 java 新手,不知道多维数组或 do-while 循环等。
这是我到目前为止写的;
public static void animals()
{
String animalname = "";
String animalsleft = "";
int noanimals = 0;
while(!animalname.equals("exterminate"))
{
animalname = JOptionPane.showInputDialog("Name an animal");
animalsleft = JOptionPane.showInputDialog("How many are left in the wild");
noanimals = Integer.parseInt(animalsleft);
System.out.println(animalname + " " + animalsleft);
}
JOptionPane.showMessageDialog(null, "The least endangered animal is the " + animalname + ".");
JOptionPane.showMessageDialog(null, "There are " + animalsleft + " left in the wild.");
}
【问题讨论】:
-
可能会编辑您的标题和您的问题,试图使其简短地解释确切您在哪里卡住,删除“我是初学者”等可能会带来更好的答案(或一些)并防止投票。并且还发布一个Runnable example 可能会很好
-
"So i was thinking if i should be using arrays to keep track of them?"- 听起来很合理。你试过吗?"the program finishes the while loop without breaking"- 它应该坏掉吗?有一个break;声明可以做到这一点。
标签: java arrays while-loop