2013-08-29 46 views
0

我該如何將這個java程序分解成多個方法? 它首先存儲一個字符串作爲輸入,然後通過它循環將所有數字提取到數組列表中。然後打印所有這些數字以及它們的總和和產品。如何將我的程序分解爲多個方法?

public class Assignment1 { 
public static void main(String[] args){ 

    // Creates scanner for storing input string 
    Scanner numScanner = new Scanner(System.in); 
    String input; 
    System.out.println("Enter a string of numbers to compute their sum and product:"); 
    System.out.println("(Enter '.' to terminate program)"); 
    input = numScanner.nextLine(); 


    // Terminates the program if there is no input 
    if (input.length() == 0){ 
       System.out.println("Invalid input: Not enough characters"); 
       System.exit(-1); 
    } 


    // Terminates the program if the first character is '.' 
    if (input.charAt(0) == '.'){ 
     System.out.println("Thank you for using numberScanner!"); 
     System.exit(-1); 
    } 


    // Defines all of the variables used in the loops 
    int index = 0; 
    int sum = 0; 
    int product = 1; 
    Integer start = null; 
    int end = 0; 
    ArrayList<Integer> numbers = new ArrayList<>(); 


    // Loop that extracts all numbers from the string and computes their sum and product 
      while (index < input.length()){ 
       if (input.charAt(index) >= 'A' && input.charAt(index) <= 'Z' && start == null){ 
        index++; 
       }else if (input.charAt(index) >= '1' && input.charAt(index) <= '9' && start == null){ 
        start = index; 
        index++; 
       }else if (Character.isDigit(input.charAt(index))){ 
        index++; 
       }else{ 
         end = index; 
         numbers.add(Integer.parseInt(input.substring(start,end))); 
         sum += Integer.parseInt(input.substring(start,end)); 
         product *= Integer.parseInt(input.substring(start,end)); 
         index++; 
         start = null; 
       } 
      } 


      // For the last number, the end is greater than the length of the string 
      // This prints the last number without using the end 
      if (index == input.length()){ 
      numbers.add(Integer.parseInt(input.substring(start))); 
      sum += Integer.parseInt(input.substring(start)); 
      product *= Integer.parseInt(input.substring(start)); 
      index++; 
      } 


     // Prints the Numbers, Sum and Product beside each other 
     System.out.print("Numbers: "); 
     for (Object a : numbers) { 
      System.out.print(a.toString() + " "); 
      } 
     System.out.println("Sum: " + sum + " Product: " + product); 
} 

}

我只是不知道如何將單一的方法分成多個方法

+1

我認爲你正在做這個要複雜得多比它必須。 – arshajii

+1

一個方法應該儘可能地做一個單一的工作。例如,你可以編寫一個'prompt'方法,它接受一個'String'「提示符」並提示用戶輸入,返回一個值... – MadProgrammer

+1

這屬於[codereview.stackexchange.com](http:// codereview.stackexchange.com)不在這裏。 –

回答

1

我認爲有在main方法的I/O是OK這裏。我將搬出的是numbers列表的構建。你可以有一個方法getNumbers接受一個字符串並返回該字符串中數字的列表。例如:

private static List<Integer> getNumbers(String str) { 
    List<Integer> numbers = new ArrayList<>(); 

    for (int i = 0; i < str.length(); i++) { 
     char c = str.charAt(i); 

     if (Character.isDigit(c)) 
      numbers.add(c - '0'); 
    } 

    return numbers; 
} 

現在,應該是微不足道的環比由getNumbers()返回的列表,並計算元素的總和/產品:

List<Integer> numbers = getNumbers(input); 

System.out.println(numbers); 

int sum = 0; 
int product = 1; 

for (int i : numbers) { 
    sum += i; 
    product *= i; 
} 

System.out.printf("Sum: %d Product: %d%n", sum, product); 
相關問題