Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java程序中识别三个递增的相邻数字。给出错误的输出和indexoutofbound异常错误_Java - Fatal编程技术网

在Java程序中识别三个递增的相邻数字。给出错误的输出和indexoutofbound异常错误

在Java程序中识别三个递增的相邻数字。给出错误的输出和indexoutofbound异常错误,java,Java,我要做的是:一个Java程序,它接受来自用户的五个整数作为输入。如果数组某处包含三个递增的相邻数字,如…4,5,6,则程序应打印“true”,否则打印 “假” import java.util.*; 五班 { 公共静态void main(字符串[]args) { 国际[]无秩序; anArray=新整数[5]; 对于(inti=0;i您总是在前面检查一个元素(i+1),因此您希望在通常停止之前停止一个元素。 首先检查下一个元素是否大于当前元素,如果大于,则增加计数。 如果不是,则重置计数,除非已

我要做的是:一个Java程序,它接受来自用户的五个整数作为输入。如果数组某处包含三个递增的相邻数字,如…4,5,6,则程序应打印“true”,否则打印 “假”

import java.util.*;
五班
{
公共静态void main(字符串[]args)
{
国际[]无秩序;
anArray=新整数[5];

对于(inti=0;i您总是在前面检查一个元素(i+1),因此您希望在通常停止之前停止一个元素。 首先检查下一个元素是否大于当前元素,如果大于,则增加计数。 如果不是,则重置计数,除非已经达到3个连续递增的数字,在这种情况下,就完成了

尝试

for(int i=0;ianArray[i])
{
计数++;
}
否则,如果(计数==3)中断;
else计数=0;
}
如果(count==3)System.out.println(“真”);
else System.out.println(“假”);
导入java.util.*;
公共班机{
公共静态void main(字符串[]args){
最终扫描仪s=新扫描仪(System.in);
//扫描仪出环,您不需要每次都创建它来读取
//因为你只需要一个,不想改变它
int[]anArray=新的int[5];
//内联创建的数组
对于(int i=0;i<5;i++){
anArray[i]=s.nextInt();
}
整数计数=0;
对于(int j=0;j
您也可以用稍微简短的形式编写程序:

import java.util.Scanner;

public class Five {

  public static void main(String[] args) {
    int[] anArray = new int[5];
这在代码中是两行独立的代码

    Scanner s = new Scanner(System.in);
这不应该在for循环中,因为您不需要每次都使用新的扫描仪。您可以创建一次,然后重新使用它

    for (int i = 0; i < anArray.length; i++) {
我在这里删除了额外的变量,=两边的表达式可以任意复杂

    }

    boolean containsThreeAdjacentNumbers = false;
    for (int j = 0; j + 2 < anArray.length; j++) {
println
方法已经知道如何打印任何内容(包括
boolean
变量),因此您无需自己提及
true
false

  }

}

下面是整个节目,没有任何评论:

import java.util.Scanner;

public class Five {

  public static void main(String[] args) {
    int[] anArray = new int[5];

    Scanner s = new Scanner(System.in);
    for (int i = 0; i < anArray.length; i++) {
      anArray[i] = s.nextInt();
    }

    boolean containsThreeAdjacentNumbers = false;
    for (int j = 0; j + 2 < anArray.length; j++) {
      if (anArray[j] + 1 == anArray[j + 1] && anArray[j + 2] == anArray[j + 2]) {
        containsThreeAdjacentNumbers = true;
      }
    }

    System.out.println(containsThreeAdjacentNumbers);
  }

}
import java.util.Scanner;
公共五班{
公共静态void main(字符串[]args){
int[]anArray=新的int[5];
扫描仪s=新的扫描仪(System.in);
for(int i=0;i
我的非常简单,只使用了一个计数器。 这是:

public boolean tripleUp(int[] nums) {
      //Create a counter starting at 1 for the current character.
      int matchingAmt = 1;
      //Starting, looking at the 2nd character and going onwards, we test to see
      //if the current character is one more than the last.
      for (int i = 1; i < nums.length; i++)
      {
        //If it is larger by one, we increment matchingAmt;
        if (nums[i] - nums[i-1] == 1)
        {
          matchingAmt++;
        }
        //If it isn't we start back at one.
        else
        {
          matchingAmt = 1;
        }
        //If the matchingAmt is 3, we return true;
        if (matchingAmt == 3)
        {
          return true;
        }
      }
      //Otherwise, if we don't find any groups of 3, we return false.
      return false; 
    }
公共布尔三元组(int[]nums){
//为当前角色创建从1开始的计数器。
int匹配mt=1;
//开始,看第二个字符,然后继续,我们测试看
//如果当前字符比最后一个多一个。
对于(int i=1;i
anArray[j+1]
将在j==4时给出一个索引越界错误。那么我应该用什么来代替j+1呢?j+1可以,但需要迭代j<4,而不是j<5。
    }

    boolean containsThreeAdjacentNumbers = false;
    for (int j = 0; j + 2 < anArray.length; j++) {
      if (anArray[j] + 1 == anArray[j + 1] && anArray[j + 2] == anArray[j + 2]) {
        containsThreeAdjacentNumbers = true;
      }
    }

    System.out.println(containsThreeAdjacentNumbers);
  }

}
import java.util.Scanner;

public class Five {

  public static void main(String[] args) {
    int[] anArray = new int[5];

    Scanner s = new Scanner(System.in);
    for (int i = 0; i < anArray.length; i++) {
      anArray[i] = s.nextInt();
    }

    boolean containsThreeAdjacentNumbers = false;
    for (int j = 0; j + 2 < anArray.length; j++) {
      if (anArray[j] + 1 == anArray[j + 1] && anArray[j + 2] == anArray[j + 2]) {
        containsThreeAdjacentNumbers = true;
      }
    }

    System.out.println(containsThreeAdjacentNumbers);
  }

}
public boolean tripleUp(int[] nums) {
      //Create a counter starting at 1 for the current character.
      int matchingAmt = 1;
      //Starting, looking at the 2nd character and going onwards, we test to see
      //if the current character is one more than the last.
      for (int i = 1; i < nums.length; i++)
      {
        //If it is larger by one, we increment matchingAmt;
        if (nums[i] - nums[i-1] == 1)
        {
          matchingAmt++;
        }
        //If it isn't we start back at one.
        else
        {
          matchingAmt = 1;
        }
        //If the matchingAmt is 3, we return true;
        if (matchingAmt == 3)
        {
          return true;
        }
      }
      //Otherwise, if we don't find any groups of 3, we return false.
      return false; 
    }