سؤال

My program does not end. I am a beginner and am having trouble understanding why. It was working fine before I changed the name, so I copied it to another file, but it still does not end.

import java.util.Scanner;
public class Fan
{
    public static void main (String[] args)
    {
        Scanner s = new Scanner(System.in);
        //first input
        System.out.println("Enter your first input: ");
        String first = s.nextLine();
        String[] firstsplit = first.split(", ");
        //second input
        System.out.println("Enter your second input: ");
        String second = s.nextLine();
        String[] secondsplit = second.split(", ");
        //third input
        System.out.println("Enter your third input: ");
        String third = s.nextLine();
        String[] thirdsplit = third.split(", ");
        //fourth input
        System.out.println("Enter your fourth input: ");
        String fourth = s.nextLine();
        String[] fourthsplit = fourth.split(", ");
        //fifth input
        System.out.println("Enter your fifth input: ");
        String fifth = s.nextLine();
        String[] fifthsplit = fifth.split(", ");

        for (int a = 0; a<=firstsplit.length-1; a++)
        {
        //skipping over values that say how many pieces are on board
            for (int i = 3; i <= 12; i++)
            {
                //compatible with piece numbers up to 12(max)
                if (Integer.parseInt(firstsplit[0])==i) {
                     while (i >= 1 && i <= Integer.parseInt(firstsplit[i])) {
                     continue;
                     }
                     System.out.println(firstsplit[i]); 
                }
            }

         } 
    }
}

I would be grateful for any advice.

هل كانت مفيدة؟

المحلول

The problem is here:

while (i >= 1 && i <= Integer.parseInt(firstsplit[i])) {
    continue;
}

This is an infinite loop since you never change the value of i inside it. Just comment it to make your application finish. Then, spend some time thinking about how to fix it or what are you trying to accomplish with this loop.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top