输入数字的连续数字可以是任何顺序

时间:2014-02-21 19:21:41

标签: java

我需要以任何顺序测试连续数字。当我按照1,2,38,3,3的顺序键入数字时,我的程序似乎工作正常,但我需要这个以任何顺序读取数字,例如3,2,4应该返回true。 / p>

应返回true的示例:

  • (1,2,3)
  • (3,2,4)
  • (-10,-8,-9)

应返回false的示例:

  • (3,5,7)
  • (1,2,2)
  • (7,7,9)

import java.util.*;

public class Consecutive {

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("Enter three numbers");
        String numbers = console.nextLine();

        System.out.println("The numbers (" + numbers + ") is '" + consecutive(numbers) + "'");
    }

    private static boolean consecutive(String str) {
        char c = str.charAt(0);
        for (int cc = 1; cc < str.length(); cc++)
            if ((c + 1) != str.charAt(cc))
                return false;
            else
                c++;
        return true;
    }
}

3 个答案:

答案 0 :(得分:1)

基于一些假设(您希望它们以任何顺序排列,它们将始终以逗号分隔),您需要检查排序数组的连续性,其中包括以下内容:

    String[] split = str.split(",");
    int[] numbers = new int[split.length];
    for (int i = 0; i < split.length; i++)
        numbers[i] = Integer.parseInt(split[i]);
    Arrays.sort(numbers);
    (...now check for consecutivity...)

答案 1 :(得分:0)

尝试这种方法:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Consecutive {

private ArrayList<Integer> numberList;

public Consecutive() {
    numberList = new ArrayList<Integer>();

    fetchInput();

    sortNumberList();

    System.out.println(isConsecutive());
}

private void fetchInput() {
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter any amount of numbers"
            + " separated by space.\n" + "Exit by typing any letter and"
            + " pressing Enter key.");

    while (scanner.hasNextInt()) {
        numberList.add(scanner.nextInt());
    }

    scanner.close();
}

// Sort the list in ascending order
private void sortNumberList() {
    Collections.sort(numberList);
}

private boolean isConsecutive() {
    // Loop through the sorted number list
    for (int index = 0, length = numberList.size() - 1; index < length; index++) {
        // Check if the two adjacent numbers are differing by the value 1 or
        // not.
        if (numberList.get(index + 1) - numberList.get(index) != 1)
            return false;
    }

    return true;
}

public static void main(String[] args) {
    new Consecutive();
}
}

首先获取输入,并将输入存储在Array中。对数组进行排序,然后检查排序的数组是否有任何相邻的元素,它们的值不相同。 如果有任何这样的相邻对,那么这意味着输入的元素不是连续的。

希望它有所帮助。

答案 2 :(得分:0)

使用stendika建议的排序方法;首先转换为数组,删除所有逗号,然后排序和比较元素。

import java.util.*;
public class Consecutive{
    public static void main (String [] args){
        Scanner console= new Scanner(System.in);
        System.out.println("Enter three numbers");
        String numbers = console.nextLine();
        System.out.println( "The numbers (" + numbers + ") is '" + consecutive( numbers ) + "'" );
    }//end of main


    private static boolean consecutive(String str) {
        String[] numbers = str.split(",");
        Arrays.sort(numbers);
        for (int index = 0; index < numbers.length-1; index++){
            if (Integer.parseInt(numbers[index]) > Integer.parseInt(numbers[index+1])){
                return false;
            }
        }
        return true;
    }//end of consecutive method
}