2014-02-21 54 views
-2

我需要以任意順序測試連續數字。我的程序似乎工作正常,當我按照1,2,38,3,3的順序輸入數字時,但我需要以任何順序讀取數字,例如3,2,4應該返回true。輸入數字的連續數可以任意順序

例子應該返回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; 
    } 
} 
+2

你是什麼意思?有序,以便每個以下數字更大?有序,所以每個都更小? –

+1

您需要先在連續方法中將字符串排序爲整數數組,然後繼續。 – mbroshi

+1

排序字符串?或者你的意思是要對字符列表進行排序... – vikingsteve

回答

1

基於一些假設(你希望他們在任何順序,他們將永遠是逗號分隔),則需要使用一些像檢查consecutivity排序的陣列如下:

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...) 
+0

大聲笑,我應該更新帖子,看看是否有人寫過這個;最後輸入整個事情,然後張貼只發現你已經實現它。 –

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(); 
} 
} 

首先獲取輸入,輸入存儲在數組中。對數組進行排序,然後檢查排序數組中是否有任何相鄰元素的值不等於1的值。 如果存在任何這樣的相鄰對,則表示輸入的元素不是連續的。

希望它有幫助。

+0

這是我前進的方式大聲笑...但非常感謝你的幫助! – Yoana

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 
}