public class searchExpress { public static void searchExpressinget(String trackingNumber) { boolean found = false; try (BufferedReader in = new BufferedReader(new FileReader("C:/eclipse-workspace/dragon/getexpress.txt"))) { String line; while ((line = in.readLine()) != null) { String[] data = line.split(","); if (data[0].equals(trackingNumber)) { found = true; break; } } if (found) { JOptionPane.showMessageDialog(null, "快递已在快递点"); } else { // 处理未找到快递的情况 } } catch (IOException e) { e.printStackTrace(); } } public static void searchExpressinsend(String trackingNumber) { boolean found = false; try (BufferedReader in = new BufferedReader(new FileReader("C:/eclipse-workspace/dragon/sendexpress.txt"))) { String line; while ((line = in.readLine()) != null) { String[] data = line.split(","); if (data[0].equals(trackingNumber)) { found = true; break; } } if (found) { JOptionPane.showMessageDialog(null, "快递已发出"); } else { JOptionPane.showMessageDialog(null, "快递未找到,请联系相应客服处理"); } } catch (IOException e) { e.printStackTrace(); } } } 如何修改代码,使得其在快递点中找到快递,就不执行searchexpressinsend
时间: 2024-04-15 14:27:14 浏览: 190
要实现在找到快递时不执行`searchExpressInSend`方法,你可以在`searchExpressGet`方法中添加一个条件判断。如果找到快递,就直接返回,而不执行后续的代码。修改后的代码如下:
```java
public class SearchExpress {
public static void searchExpressGet(String trackingNumber) {
boolean found = false;
try (BufferedReader in = new BufferedReader(new FileReader("C:/eclipse-workspace/dragon/getexpress.txt"))) {
String line;
while ((line = in.readLine()) != null) {
String[] data = line.split(",");
if (data[0].equals(trackingNumber)) {
found = true;
break;
}
}
if (found) {
JOptionPane.showMessageDialog(null, "快递已在快递点");
return; // 找到快递时直接返回
}
} catch (IOException e) {
e.printStackTrace();
}
// 如果未找到快递,则执行searchExpressInSend方法
searchExpressInSend(trackingNumber);
}
public static void searchExpressInSend(String trackingNumber) {
boolean found = false;
try (BufferedReader in = new BufferedReader(new FileReader("C:/eclipse-workspace/dragon/sendexpress.txt"))) {
String line;
while ((line = in.readLine()) != null) {
String[] data = line.split(",");
if (data[0].equals(trackingNumber)) {
found = true;
break;
}
}
if (found) {
JOptionPane.showMessageDialog(null, "快递已发出");
} else {
JOptionPane.showMessageDialog(null, "快递未找到,请联系相应客服处理");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在`searchExpressGet`方法中,当找到快递时,使用`return`语句直接返回,不再执行后续的代码。这样就可以实现在快递点中找到快递时不执行`searchExpressInSend`方法。
阅读全文