gpt4 book ai didi

Java String 在 Scanner 中的 Boolean.parseBoolean 之前替换多个字符串?

转载 作者:行者123 更新时间:2023-11-30 10:21:38 26 4
gpt4 key购买 nike

我正在构建一个 Java 程序来跟踪用户的肠道功能。我在一个循环中有一段代码,它通过扫描仪询问用户是否有抽筋。我有以下代码工作:

String cramps = userInput.nextLine();
String replacecramps = cramps.replace("Y","true");
boolean mycramps = Boolean.parseBoolean(replacecramps);

这实际上采用“Y”答案并将其替换为“true”,然后采用字符串“true”并将其更改为 boolean 值 true(我认为 - 无论如何,它似乎是这样工作的)。但是,我想更改其他可能的常见输入,例如“y”、“yes”、“Yes”、“yeah”、“Yeah”、“yep”、“Yep”等。

有什么办法吗?我是 Java 的新手,所以事实证明这很困难。我已经阅读了这里关于它的每一篇文章,但我仍然不知道如何最好地解决这个问题。

以下是一些可能有用的代码片段:

它所在的循环分支是(如果需要我可以粘贴完整的代码,但它很长):

} else if (myBristol == 7) {
System.out.println("You appear to have severe diarrhea. ");
//Integrate Diarrhea.java class
System.out.println("\nDid you experience cramps or bloating? (Y/N)");
String cramps = userInput.nextLine();
String replacecramps = cramps.replace("Y","true");
boolean mycramps = Boolean.parseBoolean(replacecramps);
System.out.println("Did you experience flatulence? (Y/N)");
String gas = userInput.nextLine();
String replacegas = gas.replace("Y","true");
boolean mygas = Boolean.parseBoolean(replacegas);
Diarrhea b = new Diarrhea(poopColor, poopSize, mycramps, mygas);
//Print data to log file
pw.println(b.toString());

它正在使用的子类:

public class Poop{
protected String color;
protected String size;

public Poop(String poopColor, String poopSize)
{
color=poopColor;
size=poopSize;
}
public void setcolor(String c)
{
color = c;
}
public String getcolor(){
return color;
}
public void setsize(String s)
{
size = s;
}
public String getsize(){
return size;
}
@Override
public String toString() {
String tmp = "This poop's color was: " + this.getcolor() + ". Poop was: " + this.getsize() + ".";
return tmp;
}

public class Diarrhea extends Poop{
protected boolean cramps;
protected boolean gas;

public Diarrhea(String poopColor, String poopSize, boolean cramps, boolean gas) {
super(poopColor,poopSize);
this.cramps=cramps;
this.gas=gas;
}
public void setcramps(boolean mycramps) {
cramps=mycramps;
}
public boolean getcramps(){
return cramps;
}
public void setgas(boolean gas) {
gas=this.gas;
}
public boolean getgas(){
return gas;
}
@Override
public String toString() {
String tmp = "This diarrhea's color was: " + this.getcolor() + ". Was this a little or a lot of diarrhea: " + this.getsize() + ". Did you experience cramps or bloating? (true/false) " + this.cramps + ". Did you experience flatulence? (true/false) " + this.gas + ".";
return tmp;
}

到目前为止,这就是我处理 y/n 问题的方式,它一直很好地工作,因为它只查找 y 或 n 作为第一个字符,不关心大小写,也不关心关于其他任何事情(如错别字)。弄清楚如何让它为 boolean 值工作有点困难,但我认为我走在正确的道路上 - 但也许我正在做这一切都是错误的。

char answer1 = userInput.next().charAt(0);
answerString = Character.toString(answer1);
userInput.nextLine();
if (answerString.equalsIgnoreCase("Y")) {
//Integrate Poop.java class
System.out.println("Was this a large, medium, small, or average sized bowel movement (describe size): ");
String poopSize = userInput.nextLine();
System.out.println("How would you describe the color of this poop (brown, greenish, light, dark, etc): ");
String poopColor = userInput.nextLine();
Poop a = new Poop(poopColor, poopSize);
//Print data to log file
pw.println(a.toString());

最佳答案

您可以像这样使用 replaceALL 和一些正则表达式:

String input = "you are saying : y yes Yes yeah Yeah yep Yep";
input = input.replaceAll("\\b(?i)y(es|eah|ep)?\\b", "true");

System.out.println(input); //you are saying : true true true true true true true

关于正则表达式的细节:

  • \b 用于单词边界
  • (?i) 不区分大小写
  • y(es|eah|ep)? y 后跟 (eseahep) 哪些是可选的 ?

关于Java String 在 Scanner 中的 Boolean.parseBoolean 之前替换多个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47767386/

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