简体   繁体   中英

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

Problem with method repeating infinitely rather than moving on to the next method. Specifically, the group method is being repeated infinitely rather than breaking after 1 iteration.

    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();
    
    
    

}

start of 'group' method (method that repeats infinitely)

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();
}

Start of 'rent' method(the method that does not run even though it is being called in 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();
}

For what I can see on your group() method is that at the end when you end the method you return group() (making it recursive and that's the reason it loops). I imagine you want to get the number of team members on each team so instead of returning 'group()' you should return your array group and as well change your function type from int to int[] like this

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

As well you need to change on your main method group = group() to group[] = group() and make group an array of integers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM