在哪里以及如何编码我的"无效输入"

时间:2016-05-29 04:22:14

标签: java arrays loops joptionpane

我需要创建一个程序来执行以下操作:     1.)要求用户从S,M,L,X中进行选择     2.)显示所选尺寸以及相应的价格。     3.)如果输入与size数组中的输入不完全相同,则必须打印错误消息。到目前为止,我能够获得用户输入并显示它的相应价格。但是,我在哪里以及如何为我的错误消息放置语法时遇到问题。

我只允许使用import javax.swing.JOptionPane来实现这一切。

List<NameValuePair> params = new ArrayList<NameValuePair>(2);

    params.add(new NameValuePair("_sendCopyToSender", "on"));

    params.add(new NameValuePair("adId", "")); 

   params.add(new NameValuePair("bbToken", tok)); 

    params.add(new NameValuePair("bbUserInput", output)); 

    params.add(new NameValuePair("ca.kijiji.xsrf.token", xsrfTok)); 

    params.add(new NameValuePair("emailRequiresVerification", "true"));

    params.add(new NameValuePair("adId", "true"));

    params.add(new NameValuePair("from", emailBox.asText())); 

    params.add(new NameValuePair("fromName", nameBox.asText())); 

    params.add(new NameValuePair("message", message)); 

    requestSettings.setRequestParameters(params);

    requestSettings.setAdditionalHeader("Accept", "application/json, text/javascript, */*; q=0.01");
    requestSettings.setAdditionalHeader("Accept-Encoding", "gzip, deflate");
    requestSettings.setAdditionalHeader("Accept-Language", "en-US,en;q=0.5");
    requestSettings.setAdditionalHeader("Connection", "keep-alive");
    requestSettings.setAdditionalHeader("Content-Length", "437");
    requestSettings.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    requestSettings.setAdditionalHeader("Host", "www.kijiji.ca");
    requestSettings.setAdditionalHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0");
    requestSettings.setAdditionalHeader("X-Requested-With", "XMLHttpRequest");              

    Page redirectPage = webClient.getPage(requestSettings); //Crashes here

}

1 个答案:

答案 0 :(得分:0)

您可以使用flag来确定用户是否输入了正确的输入。

您的for循环可能是这样的

int flag=0;  //initial value for your flag

 for(int i=0; i<size.length; i++){
        if(userInput == size[i]){
            JOptionPane.showMessageDialog(null, userInput+" = "+total[i]);
            flag=1; //states that user entered correct value
        }
    }

  //check here if user input was correct or not
  if(flag==0){
    //display the error message here
  }

希望这会有所帮助:)