while循环中的三个连续数字

时间:2015-10-27 08:08:39

标签: java

编写一个从用户读取整数的程序,直到他输入-1,如果有三个连续的数字则打印“连续”,否则打印“无连续”;您在读取的数字列表中按顺序排列,使得有一些整数k表示数字值为k,k + 1和k + 2.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int x = scan.nextInt();
    int y = x + 1;
    int z = x + 2;
    boolean areConsecutive = false;

    while (x != -1) {
        if (x == y) {
            x = scan.nextInt();
            if (x == z)
                areConsecutive = true;
        }
        x = scan.nextInt();
    }
    if (areConsecutive)
        System.out.print("Consecutive");
    else
        System.out.print("None Consecutive");
}

有人可以告诉我这段代码有什么问题吗?

5 个答案:

答案 0 :(得分:1)

你已经接近了,但你没有正确地保留这些数字的历史。

首先,为了澄清,规范要求您从用户输入任意数量的任意数字,并检查其中任何三个是否是连续的。因此,下面的第一行将有一个连续的序列(1 2 3位从第三个数字开始)但第二行不会:

9 9 1 2 3 9
3 1 4 1 5 9

执行此操作的一种方法是简单地维护 minimal 信息以检测连续序列。为此,您需要保留仅输入的最后三个数字的副本。这种野兽的伪代码(很容易转换成任何程序语言)将是:

# Get first number, ensure no chance of consecutive
# sequence until at least three are entered.

num3 = getint()
num2 = num3       
num1 = num3
consecutive = false

# Loop until -1 entered.

while num3 != -1:
    # Check for consecutive sequence.

    if (num1 + 1 == num2) and (num2 + 1 == num3):
        consecutive = true

    # Shift numbers "left".

    num1 = num2
    num2 = num3
    num3 = getint()

if consecutive:
     print "Consecutive"
else
    print "None Consecutive"

答案 1 :(得分:1)

在使用y检查之前获取下一个整数,然后检查z。如果其中一个失败,则更新y和z并再次检查。

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int x = scan.nextInt();
    int y = x + 1;
    int z = x + 2;
    boolean areConsecutive = false;

    while (x != -1) {
        x = scan.nextInt();
        if (x == y) {
            x = scan.nextInt();
            if (x == z)
                areConsecutive = true;
        }
        y = x + 1;
        z = x + 2;
    }
    if (areConsecutive)
        System.out.print("Consecutive");
    else
        System.out.print("None Consecutive");
}

答案 2 :(得分:1)

在扫描新x之前,您需要将y和z递增1。 这就是你要找的东西:

$CI = new ExampleClass()

答案 3 :(得分:0)

我会这样做,我们必须检查三个数字是否连续,无论用户输入的顺序如何:

  public static void main(String[] args){ 

    Scanner scan = new Scanner(System.in);
    int[] numbers = new int[3];

    for( int i = 0; i < numbers.length; ++i)
        numbers[i] = scan.nextInt();

    Arrays.sort(numbers);

    boolean areConsecutive = true;

    for( int i = 0; i < numbers.length - 1; ++i)
        if(numbers[i+1] - numbers[i] != 1)
              areConsecutive = false;

    if(areConsecutive)
     System.out.print("Consecutive");
    else
     System.out.print("None Consecutive");

}

答案 4 :(得分:0)

这就是你要找的东西:

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
int z = scan.nextInt();
boolean areConsecutive = false;

while ((x != -1)&&(y != -1)&&(z != -1)){
    if ((z == y + 1)&&(y == x + 1)
            areConsecutive = true;
if (areConsecutive)
    System.out.print("Consecutive");
else
    System.out.print("None Consecutive");
}