Java 如果用户输入不正确';不符合条件,返回消息
所以我对Java非常陌生,我尝试让用户输入一个关键字,它将被传递到不同的条件。如果关键字以“c”开头,它将执行cmethod。如果关键字以“n”开头,它将执行nmethod。 问题是,如果用户没有输入以“c”或“n”开头的内容,我还想显示一条消息。以下是我所能想到的(我知道这段代码不起作用)Java 如果用户输入不正确';不符合条件,返回消息,java,if-statement,next,Java,If Statement,Next,所以我对Java非常陌生,我尝试让用户输入一个关键字,它将被传递到不同的条件。如果关键字以“c”开头,它将执行cmethod。如果关键字以“n”开头,它将执行nmethod。 问题是,如果用户没有输入以“c”或“n”开头的内容,我还想显示一条消息。以下是我所能想到的(我知道这段代码不起作用) EDIT:尝试使用else if,但要执行nmethod(),用户必须输入“n”两次。else关键字正是您需要的。您应该使用else if来解决问题。要避免在if之间读取另一个输入,只需将选项.next()
EDIT:尝试使用else if,但要执行nmethod(),用户必须输入“n”两次。else关键字正是您需要的。您应该使用
else if
来解决问题。要避免在if
之间读取另一个输入,只需将选项.next()
存储在变量中,并在if
语句中调用此变量即可
String entry = choices.next();
if (entry.startsWith("c")) {
cmethod();
}
else if (entry.startsWith("n")) {
nmethod();
}
else {
// default behaviour if wrong entry
}
只有当输入不是“c”且是“n”时,才会调用nmethod(),如果这些条件都不满足,它将进入
else
部分。您必须将next()
提取到变量中。如果不执行此操作,next()
将始终返回当前关键字并转到下一个关键字。
此外,如果要检查其他关键字,则必须使用else if
public static void main(String[] args) {
System.out.println("This program can randomize characters or numbers.");
System.out.println("Please input \"c\" for characters, and \"n\" for numbers. ");
Scanner choices = new Scanner(System.in);
String keyword = choices.next();
if (keyword.startsWith("c")) {
cmethod();
} else if (keyword.startsWith("n")) {
nmethod();
} else {
System.out.println("Keyword is wrong.");
}
}
尽管这里提供了一些有效的解决方案,但似乎奇怪的是,没有人谈论真正简单的
开关
语句
基本上:
public static void main(String[] args) {
System.out.println("This program can randomize characters or numbers.");
System.out.println("Please input \"c\" for characters, and \"n\" for numbers. ");
Scanner choices = new Scanner(System.in);
switch(choices.next().charAt(0)) {
case 'c':
cmethod();
break;
case 'n':
nmethod();
break;
default:
System.out.println("Keyword is wrong.");
}
}
这与其他答案一样,因为
next()
函数只计算一次。在可以传递方法名称的地方使用反射,如果方法存在,则会找到它,否则它将在可以显示消息的地方引发异常
java.lang.reflect.Method method;
try {
method = this.getClass().getMethod(methodName, param1.class,
param2.class, ..);
} catch (SecurityException e) { ... }
catch (NoSuchMethodException e) { ... }
尝试在下面的测试示例中输入正确和错误的方法名称的示例
public class Test {
public static void main(String[] args) {
Object obj = new Test();
java.lang.reflect.Method method;
try {
method = obj.getClass().getMethod("tryone1");
System.out.println("Found");
} catch (SecurityException e) {
System.out.println(" SecurityException Found");
} catch (NoSuchMethodException e) {
System.out.println("Not Found");
}
}
public void tryone() {
// TODO Auto-generated method stub
}
public void trytwo() {
// TODO Auto-generated method stub
}
}
根据您的需要定制上述示例。例如,如果c作为参数出现,则字符串methodname=arg[0]+“method”并作为getMethod(“tryone1”)方法中的方法参数名传递。尝试:
public static void main(String[] args) {
System.out.println("This program can randomize characters or numbers.");
System.out.println("Please input \"c\" for characters, and \"n\" for numbers. ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
String s = br.readLine();
if (s.startsWith("c")) {
System.out.println("c");
System.exit(1);
}
if (s.startsWith("n")){
System.out.println("n");
System.exit(1);
}else {
System.out.println("Keyword is wrong.");
}
}catch(Exception e){
e.printStackTrace();
}
}
对我有用,希望对你有用。另一种方法:使用开关盒
String s =choices.next();
char first = s.charAt(0);
switch(first){
case 'c': cmethod(); break;
case 'n':nmethod(); break;
default : System.out.println("Keyword is wrong.");
}
其中是
else if
用else if替换第二个else,检查输入的字符是否是n
…我建议尝试将问题分解为多个步骤。。。第一步:读单词。步骤2:检查“c”。步骤3:如果不是“c”,检查“n”,步骤4:Sysout。您需要将读取的值存储到一个变量中,因为两次调用choices.next()将导致有趣的结果。关于您的编辑,这是因为您再次调用choices.next()
,再次提示它们。将结果存储在变量中,然后您可以根据需要对其进行多次测试。@Eustion您将问题的一个重要部分替换为“问题已解决”,因此您的问题及其答案不再有意义。您应该保留您说过您尝试了一个else if
并且正在读取两个字符的部分,因为这是真正的问题(调用next()
两次),您不能有多个else
语句。中间线必须是否则如果是。伙计们。。。他不想将第一个关键字与“c”对比,然后将第二个关键字与“n”对比……现在您的答案完美了,谢谢您的回答!终于有人得到了。谢谢!工作起来很有魅力。很多人都对发疯了,否则如果准确地说,开关的使用可能更符合逻辑,因为它属于“应用不同的行为并在不起作用的情况下获得后备方案”的范畴,他们想知道如何使用开关语句来实现这一点。谢谢你的信息!想一想将来你想在哪里运行任何可用的方法,如果找不到,就显示错误。我相信你不理解他的问题。他只有两个必须选择的选项,不需要动态地适应参数。R
String s =choices.next();
char first = s.charAt(0);
switch(first){
case 'c': cmethod(); break;
case 'n':nmethod(); break;
default : System.out.println("Keyword is wrong.");
}