Java 如何组合Switch语句?
我是Java新手,正在做学校作业。目标是创建一个Java类,在unix中模拟Echo工具。如果我输入: java Echo“我爱你” 它将打印: 我爱你 然后有两个命令行选项Java 如何组合Switch语句?,java,input,switch-statement,echo,Java,Input,Switch Statement,Echo,我是Java新手,正在做学校作业。目标是创建一个Java类,在unix中模拟Echo工具。如果我输入: java Echo“我爱你” 它将打印: 我爱你 然后有两个命令行选项 -n:省略尾随的换行符 -e:启用反斜杠转义的解释。 我还需要能够同时使用-n和-e 到目前为止,我已经了解了如何分别实现-n和-e。代码如下: public class Echo { public static void main(String []args) { switch (args[0
-n
:省略尾随的换行符-e
:启用反斜杠转义的解释。我还需要能够同时使用-n和-e 到目前为止,我已经了解了如何分别实现
-n
和-e
。代码如下:
public class Echo {
public static void main(String []args) {
switch (args[0]) {
case "-n":
for (int i=1; i<args.length;i++)
System.out.print(args[i]);
break;
case "-e":
for (int i=1; i<args.length; i++) {
args[i]=args[i].replace("\\t", "\t");
args[i]=args[i].replace("\\n","\n");
System.out.println(args[i]);
}
break;
default:
for (int i=0; i<args.length; i++)
System.out.println(args[i]);
}
}
}
公共类回音{
公共静态void main(字符串[]args){
开关(参数[0]){
案例“-n”:
对于(int i=1;i我会这样做:
public class Echo {
public static void main(String []args) {
if (args[0].equals("-n")){
//-n
if (args[1].equals("-e")) {
//-n -e
}
} else if (args[0].equals("-e")) {
//-e
if (args[1].equals("-n")) {
//-e -n
}
} else {
//the user typed something other than -n or -e
}
}
}
public class Echo {
public static void main(String []args) {
boolean isTrailingActive = false;
boolean isEscapingActive = false;
String inputStr;
int index = 0;
// Validates the amount of arguments passed to Echo
if (args.length == 0 || args.length > 3) {
return;
}
while (index < args.length - 1) {
// Validates trailing active
if (args[index].equals("-n")) {
isTrailingActive = true;
index++;
}
// Validates escaping active
if (args[index].equals("-e")) {
isEscapingActive = true;
index++;
}
}
inputStr = args[index];
if (isEscapingActive) {
inputStr = inputStr.replace("\\t", "\t").replace("\\n","\n");
}
if (isTrailingActive) {
System.out.print(inputStr);
} else {
System.out.println(inputStr);
}
}
}
平心而论,我对Unix中的“Echo”工具一点也不熟悉,所以我不知道这段代码是否有用。注意:如果将“-n”和“-e”都输入到一个单独的方法中,您可能需要编写代码,因为您将调用这段代码两次。尝试这样做:
public class Echo {
public static void main(String []args) {
if (args[0].equals("-n")){
//-n
if (args[1].equals("-e")) {
//-n -e
}
} else if (args[0].equals("-e")) {
//-e
if (args[1].equals("-n")) {
//-e -n
}
} else {
//the user typed something other than -n or -e
}
}
}
public class Echo {
public static void main(String []args) {
boolean isTrailingActive = false;
boolean isEscapingActive = false;
String inputStr;
int index = 0;
// Validates the amount of arguments passed to Echo
if (args.length == 0 || args.length > 3) {
return;
}
while (index < args.length - 1) {
// Validates trailing active
if (args[index].equals("-n")) {
isTrailingActive = true;
index++;
}
// Validates escaping active
if (args[index].equals("-e")) {
isEscapingActive = true;
index++;
}
}
inputStr = args[index];
if (isEscapingActive) {
inputStr = inputStr.replace("\\t", "\t").replace("\\n","\n");
}
if (isTrailingActive) {
System.out.print(inputStr);
} else {
System.out.println(inputStr);
}
}
}
公共类回音{
公共静态void main(字符串[]args){
布尔值isTrailingActive=false;
布尔值isEscapingActive=false;
字符串输入str;
int指数=0;
//验证传递给Echo的参数数量
如果(args.length==0 | | args.length>3){
返回;
}
while(索引
您需要注意标记在args[]中的位置以及字符串所在的位置。由于args[]
的长度可以为1(无标记),因此您不能随意访问args[1]
如中所述,首先查找标志并在以后应用其效果要容易得多。我假设标志必须位于字符串之前,因此可以保证字符串位于最后一个单元格中。从这里开始,向后查找标志
public class Example {
public static void main(String[] args) {
boolean hasE = false;
boolean hasN = false;
String message = args[args.length - 1];
for (int i = args.length - 2; i >= 0; i--) {
switch (args[i]) {
case "-n": hasN = true; break;
case "-e": hasE = true; break;
}
}
if (hasE)
message = message.replace("\\n", "\n").replace("\\t", "\t");
if (hasN)
System.out.print(message);
else
System.out.println(message);
System.out.println("Test");
}
}
注意事项:
- 我没有进行输入验证(您在作业或代码中没有提到它,我不想做整个作业)。提示:检查
args.length
是否为0或大于3
- 我假设你只需要转义制表符和换行符,如果你需要转义更多的东西,那么就需要一个更复杂的正则表达式
- 我在末尾添加了一个打印,以便轻松检查
-n
的效果
有两种解决方案:
1)为命令行使用API,有很多
2)在args上执行foreach…您可以将上一个argument作为字符串进行管理,以便从每个args进行回显或构建
boolean optionNewline = false;
boolean optionE = false;
StringBuilder sb = new StringBuilder();
for(String arg : args) {
if ("-n".equals(arg)) {
optionNewline = true;
}
else if ("-e".equals(arg)) {
optionE = true;
}
else {
// TEXT
if(sb.length() > 0) {
sb.append(' '); //space between text part ?
}
sb.append(arg);
}
} // foreach
// DO MY JOB on SB.toString();
我看到一些错误:args[1]。equals(“-e)| | args[1]。equals(-n)它编译得好吗?您应该分两步进行,第一步检测标志(-n,-e),然后第二步根据标志执行作业(它们很有趣:p)。如果我既不插入-n
也不插入-e
,该怎么办?@user1803551,捕捉得很好。忘记了基本情况。那么修复方法在哪里?@user1803551,昨天我不明白你的意思。更新了我的答案。好吧,现在可以了。但是为什么你需要第二个循环,它会多次运行?另外,命令行参数不能null
。这可能会导致运行时异常。请测试您的代码。如果没有传递任何参数,该怎么办?这将导致ArrayIndexOutOfBoundsException
。正如您所说的,您不能不加区别地访问args
。@OP在文字或代码中没有提到aribeiro输入验证,也不鼓励您这样做完成家庭作业问题的作业代码。此外,您可以随时添加更多检查,以检查问题中未提及的情况。我在回答中添加了另一个注释,明确说明了问题。我昨天对我的回答应用了相同的原则。:@aribeiro不是真的,您的代码会抛出NPE进行有效输入。输入验证表示无效输入。@aribeiro是的,对不起,AIOOBE。可能我不理解“相同原则”指的是什么。我的观点是OP声明输入是0-2个标志,后跟一个字符串。如果代码不适用于此,那么它就是错误的。如果输入不是那样,那么验证才是相关的(OP不需要)。