2016-09-19 46 views
0

我是一個初學者,我正在編寫一個程序,它將是一個簡單的路由器模擬器。如何將字符串切片爲兩個並將子字符串轉換爲不同的函數?

我想輸入命令「ipA 192.0.0.1」(不同的IP),並希望切換語句以檢測「ipA」或「ipB」等。在每一種情況下分配給命令我想傳輸IP(字符串)到其他功能。這是我的簡單代碼。任何解決方案它的工作不:(

public class Main { 

private static Scanner scan; 
private static Commands com; 

public static void main(String[] args) { 

    com = new Commands();  
    scan = new Scanner(System.in); 

    while(true){ 


     String s = scan.nextLine(); 

     switch(s){ 
     case("help"): 
      com.help(); 
     break; 

     case(s.substring(0, 3).equals("ipA")): 
      com.ipA(s.substring(5)); 
     break; 

     case(s.substring(0, 4).equals("ipB1")): 
      com.ipA(s.substring(6)); 
     break; 

     case(s.substring(0, 4).equals("ipB2")): 
      com.ipA(s.substring(6)); 
     break; 

     case(s.substring(0, 3).equals("ipC")): 
      com.ipA(s.substring(5)); 
     break; 

     default: 
      System.out.println("Wrong!!!"); 
     break; 
     } 







    } 
} 

}

+1

你不能像Java那樣使用'switch'。你可以用'if'語句代替。 – khelwood

+0

好的,我會用if語句來試試。謝謝! –

+1

查看String中的'startsWith'。 – Mark

回答

1

這應該讓你開始:

public static void main(String[] args) { 


    com = new Commands(); 
    scan = new Scanner(System.in); 


    while (true) { 

     String o = scan.nextLine(); 
     String s = o.split(" ")[0]; 

     switch (s) { 
     case ("help"): 
      com.help(); 
      break; 

     case ("ipA"): 
      com.ipA(o.substring(4)); 
      break; 

     case ("ipB1"): 
      com.ipA(o.substring(5)); 
      break; 

     case ("ipB2"): 
      com.ipA(o.substring(5)); 
      break; 

     case ("ipC"): 
      com.ipA(o.substring(4)); 
      break; 

     default: 
      System.out.println("Wrong!!!"); 
      break; 
     } 

    } 
} 

你內心case使用的是布爾表達式的表達式,但你的交換機是基於字符串與。 switch(s)。所以你的案例也應該是String。

1

這就是if語句的解決方案,你可以使用if()else if() {}和最後else {}覆蓋所有可能的條件。第一個true結果將被執行,其餘的不會在運行時發生。

package stackoverflow_Question39568263; 

import java.util.Scanner; 

public class Main { 

    public static void main(final String[] args) { 
     final Scanner scan; 
     final Commands com; 

     com = new Commands(); 
     scan = new Scanner(System.in); 

     while (true) {  
      final String s = scan.nextLine(); 

      if (s == "help") { 
       com.help(); 
      } 
      else if (s.substring(0, 3).equals("ipA")) { 
       com.ipA(s.substring(5)); 
      } 
      else if (s.substring(0, 4).equals("ipB1")) { 
       com.ipA(s.substring(6)); 
      } 
      else if (s.substring(0, 4).equals("ipB2")) { 
       com.ipA(s.substring(6)); 
      } 
      else if (s.substring(0, 3).equals("ipC")) { 
       com.ipA(s.substring(5)); 
      } else { 
       System.out.println("Wrong!!!"); 
      } 
     } 
    } 
} 
0

您可以實施流處理。最主要的好處 - 實施替代的switch-case後,你的處理代碼不會改變:

Action是的匹配是你的行動適用於輸入字符串:

abstract class Action { 
    public static final Action WRONG_ACTION = new Action() { 
     @Override 
     public void apply(String s) { 
      System.out.println("Wrong"); 
     } 
    }; 

    private Pattern pattern; 

    public Action() { 
    } 
    public Action(String pattern) { 
     this.pattern = Pattern.compile(pattern); 
    } 
    public boolean isApplicable(String s) { 
     return pattern.matcher(s).matches(); 
    } 

    abstract public void apply(String s); 

}

處理看起來像:

private List<Action> actions; 

actions.stream() 
     .filter(x -> x.isApplicable(s)) 
     .findFirst() 
     .orElse(Action.WRONG_ACTION) 
     .apply(s); 

Action例如:

class IpAAction extends Action { 
    @Override 
    public void apply(String s) { 
     ... 
    } 
} 

new IpAAction("^ipA");