How can I fix my loop to stop when i enter number less than 1 and greater than 50 and to stop when more than 20 inputs?

huangapple go评论64阅读模式
英文:

How can I fix my loop to stop when i enter number less than 1 and greater than 50 and to stop when more than 20 inputs?

问题

我需要的代码是,如果用户输入的数字小于1或大于50,则停止运行。到目前为止,我已经编写了以下内容。该循环还进入了无限循环,我希望在输入20次后停止。有任何建议吗?

KeyboardReader reader = new KeyboardReader();
Ex4Method object = new Ex4Method();
int occurences[] = new int[51];
int[] nums = new int[20];
int i = 0;
System.out.println("输入一个数字(1-50):");
nums[i] = reader.readInt();
while (nums[i] >= 1 && nums[i] <= 50) {
    i++;
    if (i >= 20) {
        break;  // 增加条件来控制最多输入20次
    }
    System.out.print("输入一个数字(1-50):");
    nums[i] = reader.readInt();
}

注意:由于您要求只返回翻译后的部分,以上是您提供的代码的翻译部分。如果您有关于这段代码的进一步问题或解释,请随时提问。

英文:

I need the code to stop if the user enters a number less than 1 or greater than 50. I have written what is below so far. The loop also goes in an infinite loop and I need it to stop after 20 inputs. Any suggestions?

KeyboardReader reader = new KeyboardReader();
Ex4Method object = new Ex4Method();
int occurences [] = new int [51];
int [] nums = new int[20];
int i=0;
System.out.println(&quot;Enter a number (1-50): &quot;);
nums[i] = reader.readInt();
while(nums[i]&gt;= 1 &amp;&amp; nums[i]&lt;=50)
{
	while(i&lt;19) 
	{
		
		System.out.print(&quot;Enter a number (1-50): &quot;);
		nums[i] = reader.readInt();
		
	
	}

答案1

得分: 1

更改 while(nums[i]&gt;= 1 &amp;&amp; nums[i]&lt;=50)while(nums[i]&gt;= 1 &amp;&amp; nums[i]&lt;=50 &amp;&amp; i&lt;19),将 i++ 放在循环内部,并移除 while(i&lt;19) 循环。

示例:

KeyboardReader reader = new KeyboardReader();
Ex4Method object = new Ex4Method();
int occurences[] = new int[51];
int[] nums = new int[20];
int i = 0;
System.out.println("输入一个数字 (1-50):");
nums[i] = reader.readInt();
while (nums[i] >= 1 && nums[i] <= 50 && i < 19) {
    i++;
    System.out.print("输入一个数字 (1-50):");
    nums[i] = reader.readInt();
}
英文:

Change while(nums[i]&gt;= 1 &amp;&amp; nums[i]&lt;=50) with while(nums[i]&gt;= 1 &amp;&amp; nums[i]&lt;=50 &amp;&amp; i&lt;19), put i++ inside the loop and remove the while(i&lt;19) loop.

Sample:

KeyboardReader reader = new KeyboardReader();
Ex4Method object = new Ex4Method();
int occurences [] = new int [51];
int [] nums = new int[20];
int i=0;
System.out.println(&quot;Enter a number (1-50): &quot;);
nums[i] = reader.readInt();
while(nums[i]&gt;= 1 &amp;&amp; nums[i]&lt;=50 &amp;&amp; i&lt;19)
{
    i++;
    System.out.print(&quot;Enter a number (1-50): &quot;);
    nums[i] = reader.readInt();
}

答案2

得分: 1

你可以像这样编写代码:

while (nums[i] >= 1 && nums[i] < 50 && i < 20) {
    i++;
    System.out.print("Enter a number (1-50): ");
    nums[i] = reader.readInt();
}
英文:

you can code like this .

while(nums[i]&gt;= 1&amp;&amp;nums[i]&lt;50&amp;&amp;i&lt;20)
{
     i++;
    System.out.print(&quot;Enter a number (1-50): &quot;);
    nums[i] = reader.readInt();

}

huangapple
  • 本文由 发表于 2020年10月2日 01:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64160130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定