无法为最新项目设置基于用户输入的阵列

时间:2017-11-03 18:17:57

标签: c# arrays

目前正在学习C#并且无法开始下一个项目...

我有这个烂摊子://我不知道数组长度所以试图使用char来退出数组的循环......(如果可能的话)

public class ControllerForwardFilter extends OncePerRequestFilter {

    @Autowired
    public CategoryService categoryService;
    @Autowired
    public ProductService productService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {

        //get servletPath
        String servletPath = request.getServletPath();

        //Assuming that categoryService.findByName(categoryName) exist
        CategoryModel category = categoryService.findByName(servletPath);
        if(category != null) {
            String forwardUrl = "/c/" + category.getCode();
            request.getRequestDispatcher(forwardUrl).forward(request, response);
            return;
        }

        //Assuming that productService.findByName(productName) exist
        ProductModel product = productService.findByName(servletPath);
        if(product != null) {
            String forwardUrl = "/p/" + product.getCode();
            request.getRequestDispatcher(forwardUrl).forward(request, response);
            return;
        }

        //otherwise it's a Home page or Account page continue
        filterChain.doFilter(request, response);
    }
}

3 个答案:

答案 0 :(得分:0)

如果使用char并不是绝对重要,可以尝试使用字符串

void Main()
{
    string response;

            string[] workerName = new string[2]; //100
            for (int i = 0; i < workerName.Length; i++)
        {

            Console.WriteLine("Do You Want To Enter A Worker's Name? Y or N: ");
            response = Console.ReadLine().Substring(0,1).ToLower();
            if (response == "y"){
                Console.WriteLine("Please Enter The Worker's Name: ");
                workerName[i] = Console.ReadLine();
                }
            else continue;
        }
        Console.WriteLine("Worker Names entered are: ");
        for(int i = 0; i< workerName.Length; i++) {
            Console.WriteLine(workerName[i]);
        }
}

希望这有帮助:)代码是自我解释的,如果您有任何问题,请告诉我。

答案 1 :(得分:0)

  static void Main(string[] args)
    {
        string[] workerName = new string[100];
        var i = 0;

        do
        {
            Console.Write("Please Enter The Worker's Name: ");
            workerName[i++] = Console.ReadLine();

            Console.Write("Do You Want To Enter A Worker's Name? Y or N: ");

        } while (Console.ReadKey().Key == ConsoleKey.Y);
    }

但也许使用像List这样的集合?当你事先知道有多少元素时,数组就是真的。

同样,For循环适用于您知道要提前循环多少次的情况。因此,请使用Do循环来解决此问题。

有很多方法可以重写逻辑以退出。我提供了一个简单的按键,但我的回答是带回家使用Do循环:)

答案 2 :(得分:0)

使用GET关键字提前退出循环。我还添加了几条评论:

break