我不知道爲什麼我的第二個for循環將不執行。它編譯,但是當我運行這個它不起作用〜_〜Java的基本循環
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 + " ");
}
;
}
}
我更新的循環來evaulate OCCURENCES
對(INT左= 0;左< list.length;左++)
{
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;
}
我已經洗乾淨了一下,現在出現次數是相同的輸入
你到底做的事:與HashMap的工作樣本? – smk
「它編譯但是當我運行它時它不起作用〜_〜」 - 那麼會發生什麼?你有例外嗎? –
@Mihai Stancu:作業標籤宣佈已過時! – Philipp