簡體   English   中英

為什么我的主要方法不斷重復而不是繼續下一個方法? 我嘗試刪除 for 循環,但仍然是同樣的問題

[英]why does my main method keep repeating itself rather than moving on to the next method? I tried removing for loops and still the same problem

方法無限重復而不是繼續下一個方法的問題。 具體來說,group 方法會無限重復,而不是在 1 次迭代后中斷。

    public static void main(String[] args) 
{
    int time = 0;
    int  gearCount = 0;
    int group = 0;
    int  total = 0;
    int answer = 1;
    int rent = 0;
    int period = 0;
    int total1 = 0;
    
    group = group();
    rent = rent();
    
    
    

}

“組”方法的開始(無限重復的方法)

public static int group()
{
    String input = " ";
    int [] group = {0};
    
    
        
        for(int i = 0 ; i <1 ; i++)
        {
        input = JOptionPane.showInputDialog(null, "Enter the number of people in your group, minimum of 5 people required: ");
        group[i] = Integer.parseInt(input);
        
        
        if(group[i]<5) 
        {
            int again = 0;
        again = JOptionPane.showConfirmDialog(null, "You have " +group[i]+ " people in your group, that is below the requirement, would you like to try again? " , "Error!", JOptionPane.YES_NO_OPTION);
        if(again == JOptionPane.YES_OPTION)
        {
        group();
        
        
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Ok good bye!");
        }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "OK! You have a total of " +group[i]+ " people on your team, you meet the requirement!");
        
            
            
        }
        }
        
        
        
    
    
    
    
    return group();
}

'rent' 方法的開始(即使在 main 中調用它也不會運行的方法

public static int rent()
{
    
    int [] rent = {0, 5};
    for(int i = 0 ; i < rent.length; i++)
    {
    rent[i] = JOptionPane.showConfirmDialog(null, "Would you like to rent gear for $5? ", "Rent", JOptionPane.YES_NO_OPTION);
    if(rent[i] == JOptionPane.YES_OPTION)
    {
    JOptionPane.showMessageDialog(null, "Your additional rent fee is: $" +rent[1]+ " per person!" );
    
    }
    else
    {
        JOptionPane.showMessageDialog(null, "Your additional rent fee is: $" +rent[0]);
    }
    
    }
    
    
    return rent();
}

因為我在你的group()方法上看到的是,當你結束你返回 group() 的方法時(使其遞歸,這就是它循環的原因)。 我想你想獲得每個團隊的團隊成員數量,而不是返回 'group()' 你應該返回你的數組group並將你的 function 類型從int更改為int[]像這樣

public static int[] group(){
    int [] group = {0};
    // Do your for operation and all
    return group;
}

您還需要將主方法group = group()更改為group[] = group()並使 group 成為整數數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM