使用並行數組和用戶輸入 (Using Parrallel array and user input)


問題描述

使用並行數組和用戶輸入 (Using Parrallel array and user input)

對於我的程序,我必須創建一堆方法,但我堅持啟動它。基本上是一個存儲樹木類型和種子數量的程序。我有一個數組中的樹列表,並希望將每棵樹的種子數放在一個平行數組中,我覺得我採取了正確的步驟,但並沒有真正得到正確的邏輯。

import javax.swing.JOptionPane;

public class seedFarmer {
   public static void main(String[] args) {
         }
   public static int getNumEachTree() { 
      String[] treeTypes = {"Fir" , "Pine" , "Spruce"};
      int[] numTrees = new int[treeTypes.length];
      int numofTrees = getNumEachTree();

       for (int index = 0; index < numTrees.length; index++) {

             numTrees[index] = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of seeds you want to plant for: " + treeTypes[index]));
      }
     return numTrees;
    }
  }

參考解法

方法 1:

You're main problem is here:

   public static int getNumEachTree() { 
      String[] treeTypes = {"Fir" , "Pine" , "Spruce"};
      int[] numTrees = new int[treeTypes.length];
      int numofTrees = getNumEachTree();                  <‑‑HERE

You are calling getNumEachTree() causing a recursive habit that never ends. The function will never stop calling itself as it has no check for whether to call itself or not and so will loop forever.

Also you are never using numofTrees so why break your code with it? I assume it is supposed to be the return value? Maybe you intended on calling that function from main? You are returning numTrees when you should be returning an int, not an array of ints.

I don't understand what the purpose of this code is entirely but just a few syntax point outs. Your logic seems okay.

(by JavabeginPhyreprooph)

參考文件

  1. Using Parrallel array and user input (CC BY‑SA 2.5/3.0/4.0)

#java #jgrasp #arrays






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論