我不知道为什么我的第二个 for 循环不会执行。它编译但是当我运行它时它不起作用〜_〜
import java.util.Scanner;
public class ranges
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
int size;
int input;
int count = 0;
int occurence = 0;
System.out.print ("Please enter the size of your array: ");
size = scan.nextInt();
int[] list = new int[size];
for (int index = 0 ; index < list.length ; index++)
{
System.out.print ("Please enter an integer between 0 to 50: ");
input = scan.nextInt();
if ( input >= 0 && input <= 50 )
{
list[index] = input;
}
else
{
System.out.println ("Invalid output, please try again");
index--;
}
}
int right = (list.length)-1;
int left = 0;
for (int counter = left ; counter < list.length ; counter++)
{
while ( right >= left )
{
if ( list[left] == list[right] )
{
occurence++;
right--;
}
}
right = (list.length)-1;
System.out.println ("The number " + list[left] + " was added " + occurence + "4 times");
}
for (int value : list)
{
System.out.print (value + " ");
}
;
}
}
我更新的 for 循环以评估发生情况
for (int left = 0 ; left < list.length ; left++)
{
while ( right >= left )
{
if ( list[left] == list[right] )
{
occurence++;
}
right--;
}
System.out.println ("The number " + list[left] + " was added " + occurence + " times");
right = (list.length)-1;
occurence = 0;
}
我已经清理了一下,现在出现的情况与输入相同