看看我的代码帮助我

时间:2015-09-04 10:51:18

标签: java jsp switch-statement

<html>
<body>
    <h1> This will get the day</h1>


            <b>The Day is:</b>
            <%= request.getParameter("day")%><br>
    <%        

  switch(request.getParameter("day"))
 {
      case 0:
          out.println("It\'s Sunday.");
                  break;
      case 1:
          out.println("It\'s Monday.");
                  break;
      case 2:
          out.println("It\'s Tuesday.");
                  break;
      case 3:
          out.println("It\'s Wednesday.");
                  break;
      case 4:
          out.println("It\'s Thursday.");
                  break;
      case 5:
          out.println("It\'s Friday.");
                  break;
      case 6:
          out.println("It\'s Saturday.");
                  break;            
  }
 %>

我是编程的新手请帮助,我需要得到它输入的那一天然后它的星期一,如果用户输入3输出将是星期三谢谢你们!

1 个答案:

答案 0 :(得分:-1)

您正在切换案例而不是request.getParameter("day")传递字符串(int)。试试这段代码。

String dayStr = request.getParameter("day");
if (dayStr != null && !"".equals(dayStr)) {
    int day = 0;
    try {
        day = Integer.parseInt(dayStr);
    } catch (NumberFormatException nfe) {
        // Invalid day and handle exception accordingly.
        nfe.printStackTrace();
        day = -1;
    }

    switch(day) {
        case 0:
            out.println("It\'s Sunday.");
            break;
        // similarly other cases
        default:
            out.println("Invalid day.");
            break;
    }
}