[英]Java basic loop
我不知道为什么我的第二个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;
}
我已经整理了一下,现在出现的情况与输入相同
第二,您for
还在努力。 问题出在while
循环条件下,即while ( right >= left )
。 如果list[left] == list[right]
不相等,则在这种情况下,它会以无限循环的形式right
或left
。
我想,你需要改变你的while
,如下(移动right--
外if
条件):
while ( right >= left )
{
if ( list[left] == list[right] )
{
occurence++;
}
right--;
}
还有两个问题:
请重新初始化 occurence =0;
在while循环之前,以便它计算每个数字的出现次数,并从System.out.println()
删除4
,例如:
for (int counter = left ; counter < list.length ; counter++)
{
occurence = 0; //< initialize to 0
while ( right >= left )
{
if ( list[left] == list[right] )
{
occurence++;
}
right--;
}
right = (list.length)-1;
//remove 4 after "occurance +"
System.out.println ("The number " + list[left] +
" was added " + occurence + " times");
}
编辑:使用HashMap的工作示例:
Map<Integer, Integer> scannedNums = new HashMap<Integer, Integer>();
for (int counter = left ; counter < list.length ; counter++)
{
if(scannedNums.get(list[counter]) == null){
scannedNums.put(list[counter], 1);
}else{
int currentCount = scannedNums.get(list[counter]);
scannedNums.put(list[counter], currentCount+1);
}
}
Set<Integer> nums = scannedNums.keySet();
Iterator<Integer> numIter = nums.iterator();
while(numIter.hasNext()){
int number = numIter.next();
System.out.println ("The number " + number +
" was added " + scannedNums.get(number) + " times");
}
请快速浏览一下,并对未来提出建议。
这段代码:
while ( right >= left )
{
if ( list[left] == list[right] )
{
occurence++;
right--;
}
}
建议如果list [left]!= list [right],而right仍然> = left,则此循环将永远不会停止。 您可能想在if语句之外向右移动,如果您只想统计发现的次数。
最后,当您说“循环不起作用”时,它会有所帮助。 也许显示您的输出,或者显示不起作用的方式会更好。
这会带来什么错误?
我看到的一个迫在眉睫的问题是,如果没有出现,则while循环将永远运行。 同样,第一个for循环可能会使索引超出范围。
对于第一个循环,而不是:
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--;
}
}
我会做:
for (int index = 0 ; index < list.length ; ++index)
{
while(true)
{
System.out.print ("Please enter an integer between 0 to 50: ");
input = scan.nextInt();
if ( input >= 0 && input <= 50 )
{
list[index] = input;
break;
}
System.out.println ("Invalid output, please try again");
}
}
对于第二个循环:
for (int counter = left ; counter < list.length ; counter++)
{
occurence = 0;
for( int i = counter; i <= right; ++i)
{
if( list[left] == list[i] )
++occurence;
}
System.out.println ("The number " + list[left] + " was added " + occurence + " times");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.