【问题标题】:Java programming Beginner While-LoopJava 编程初学者 While-Loop
【发布时间】: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


【解决方案1】:

每次循环时都会覆盖字符串值。

你想要做的是使用一个字符串列表。

例如。

 ArrayList<String> animals = new ArrayList<String>();

 while(!animalname.equals("exterminate"))
 {

     animalname = JOptionPane.showInputDialog("Name an animal"); 
     animals.add(animalname);
     ...
 }

【讨论】:

    【解决方案2】:

    如果你想使用一个数组,你必须首先知道你将要进行的循环的重复次数。例如,如果您要循环运行 100 次,请将您的数组初始化为

    String[] animalNames = new String[100];
    

    然后在你的while循环中

    int i = 0;
    while(i < animalNames.length) {
         animalNames[i] = JOptionPane.showInputDialog("Name an animal"); 
         i = i + 1;
    }
    

    这样,您的数组仍将包含所有动物名称。

    【讨论】:

    • 我意识到这一点,但由于他是初学者,我假设他刚刚在学校学习了数组。他可能还没有进入名单。
    【解决方案3】:

    问题很简单。并且有多种方法可以解决这个问题。 但首先你需要重新审视你的问题(它是如何被问到的)。

    但是你会及时了解的。现在的解决方案:显然你可以使用数组,但更好的方法是使用列表。但是所有这些都是不必要的,您可以轻松避免使用所有这些内存。只需使用临时缓冲区并每次检查新动物是否濒临灭绝。只需在代码中添加以下几行即可。

    public static void animals() { 
        String animalname = ""; 
        String animalsleft = ""; 
        int noanimals = 0;
        String prevanimalname = ""; 
        String prevanimalsleft = ""; 
        int prevnoanimals = 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);
           if (noanimals > prevnoanimals) {
                swap(prevnoanimals, noanimals);
                swap(prevanimalname, animalname);
            }
        }
        JOptionPane.showMessageDialog(null, "The least endangered animal is the " +  prevanimalname + ".");
        JOptionPane.showMessageDialog(null, "There are " + prevanimalsleft + " left in the wild.");
    }
    

    您可以在 Google 上交换两个变量,并在同一 if 语句中对动物的名称应用相同的交换。

    mostAnimalCount 保存了数量最多的动物的数量。最后,您可以简单地显示带有这些数字和名称的对话框。

    【讨论】:

    • 您的解决方案过于复杂
    • @redFIVE 我提供的解决方案是最简单的。请记住,编程不仅仅是实现愿望任务,而是在使用尽可能少的资源的同时实现。
    • 你真的要给我上一堂编程课吗?
    • @redFIVE 响应您之前的投诉,我已经更新了代码。它不是在 IDE 上运行的,因此可能存在一些错误。但你需要做的就是在那里。当你加入一个社区并在你的作业上寻求帮助时,作为对你的第二个回应,这是你通常在任何地方犯错的地方通过讲座得到的。
    • 呃,我不是问这个问题的人,我想这些天阅读理解很少......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多