コマンドを扱う方法はわかったのですが、コマンドラインの argumentの扱い方がわかりません。ユーザーがs もしくはshuffle とそのargumentである 2 3 4 5と打った場合、OverhandShuffler クラスの中にあるshuffle(int [] blocks) の引数にこの2 3 4 5を配列に入れてこのshuffleメソッドに渡したいのですが、いまいちわかりません。Scanner class のnextLine を使い 2 3 4 5を取り出せたのですが、そこからどうしていいのかがわかりません。自分としては、nextInt()を使い全ての数字を取り出したいのですが、どのようにループを作ればいいのかもわかりません。すみません、色々わからなくて。
java
1import java.util.*; 2 3public class OverhandApp{ 4 public static void main(String [] args){ 5 StringBuilder s = new StringBuilder(); 6 Scanner input = new Scanner(System.in); 7 // an instance of OverhandShuffler 8 while(input.hasNextLine()){ 9 handleLine(s, input.nextLine()); 10 } 11 } 12 13 private static int[] getNums(Scanner input){ 14 List<Integer> numlist = new ArrayList<Integer>(); 15 while(input.hasNextInt()){ 16 numlist.add(input.nextInt()); 17 } 18 int[] num = new int[numlist.size()]; 19 for(int i = 0; i<num.length; i++){ 20 num[i] = numlist.get(i); 21 } 22 return num; 23 } 24 25 public static void handleLine(StringBuilder str, String input){ 26 Scanner scan = new Scanner(input);// user type 27 OverhandShuffler shuffler = new OverhandShuffler();// an instance OverhandShuffler 28 if (scan.hasNext()){// if input is not empty 29 String command = scan.next();// string part "make-new" or "m" 30 switch(command){ 31 case "make-new": case "m":// make-new or just m 32 if (scan.hasNextInt()){// if command has integer. 33 shuffler.makeNew(scan.nextInt());// scan.nextInt is a integer part. 34 System.out.println(shuffler.toString());// showing the deck that is made. 35 }else{// if command does not have integer 36 // remind user an error. 37 System.err.println("integer expected after make-new command"); 38 } 39 break; 40 case "print":case "p": 41 /** Print a string representation of the current deck*/ 42 System.out.println(shuffler.currentDeck()); 43 break; 44 case "shuffle": case "s": 45 if (scan.hasNextInt()){// if next token is integer 46 47 48 49 } 50 } 51 } 52 } 53 54} 55
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/03 01:09
2017/05/03 01:12
2017/05/03 01:20
2017/05/03 01:21
2017/05/03 01:34