gpt4 book ai didi

java - if else 语句

转载 作者:行者123 更新时间:2023-12-02 07:19:37 24 4
gpt4 key购买 nike

我有一个包含多个 if/else 语句的代码。然而,每当抛出“else”情况时,所有后续语句也会导致“else”。这是代码,希望得到一些帮助!

public List<Integer> htmlHelper(String arg) throws IOException,
XPatherException, ParserConfigurationException,
XPathExpressionException {
CleanerProperties props = new CleanerProperties();

props.setTranslateSpecialEntities(true);
props.setTransResCharsToNCR(true);
props.setOmitComments(true);

HtmlCleaner cleaner = new HtmlCleaner(props);
rootNode = cleaner.clean(arg);


TagNode tagNode = new HtmlCleaner(props).clean(new URL(
"http://www.athletics.psu.edu/psustrength/index_rec.asp"));
org.w3c.dom.Document doc = (org.w3c.dom.Document) new DomSerializer(
new CleanerProperties()).createDOM(tagNode);

XPath jpath = XPathFactory.newInstance().newXPath();
String recinfo = (String) jpath
.evaluate(

"/html/body/div/div[1]/div/div/table/tbody/tr[3]/td[2]/p[2]/text()",
doc, XPathConstants.STRING);
String recnum = recinfo.replaceAll("\\D+", "");

if (recnum != null && recnum.length() == 0) {

recnumint = 500;
grec = 0;
} else {
recnumint = Integer.parseInt(recnum);
grec = recnumint;
}

XPath ipath = XPathFactory.newInstance().newXPath();
String iminfo = (String) ipath
.evaluate(

"/html/body/div/div[3]/div/div/table/tbody/tr[2]/td[2]/p[2]/text()",
doc, XPathConstants.STRING);
String imnum = iminfo.replaceAll("\\D+", "");

if (imnum != null && recnum.length() == 0) {

imint = 400;
gim = 0;
} else {
imint = Integer.parseInt(imnum);
gim = imint;
}

XPath xpath = XPathFactory.newInstance().newXPath();
String whiteinfo = (String) xpath
.evaluate(

"/html/body/div/div[3]/div/div/table/tbody/tr[1]/td[2]/p[2]/text()",
doc, XPathConstants.STRING);
String whitenum = whiteinfo.replaceAll("\\D+", "");

if (whitenum != null && recnum.length() == 0) {

whitenumint = 450;
gwhite = 0;
} else {
whitenumint = Integer.parseInt(whitenum);
gwhite = whitenumint;
}

XPath fpath = XPathFactory.newInstance().newXPath();
String fitinfo = (String) fpath
.evaluate(

"/html/body/div/div[3]/div/div/table/tbody/tr[4]/td[2]/p[2]/text()",
doc, XPathConstants.STRING);
String fitnum = fitinfo.replaceAll("\\D+", "");

if (fitnum != null && recnum.length() == 0) {

fitint = 470;
gfit = 0;
} else {
fitint = Integer.parseInt(fitnum);
gfit = fitint;
}



List<Integer> list = new ArrayList<Integer>();
list.add(recnumint);
list.add(whitenumint);
list.add(imint);
list.add(fitint);

return list;
}

}

如前所述,当抛出第一个“else”语句时就会出现问题。如果第一个情况是“if”,第二个情况是“else”,那么第三个和第四个情况也将是“else”

最佳答案

所有的 if 都具有相同的第二个条件,也许这就是原因?

你的意思是

if (imnum != null && imnum .length() == 0) {
^^^^^^

而不是

if (imnum != null && recnum.length() == 0) {

同样

if (whitenum != null && whitenum.length() == 0) {
^^^^^^^^^

当然还有..

if (fitnum != null && fitnum.length() == 0) {
^^^^^^

关于java - if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14431474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com