Java输入读取字符串以进行验证

huangapple go评论61阅读模式
英文:

Java input read String for validation

问题

怎样使用扫描器验证两个描述为Y和N的字符串之间的输入?

System.out.println("您确定吗?");
String strInput = scan.nextLine();

if (strInput.equals("Y") || strInput.equals("N")) {
    System.out.println("不错的选择");
} else {
    System.out.println("请输入Y或N");
}
英文:

How to validate input beetween 2 String described as Y and N using scanner ?

System.out.println("Are you sure?");
String StrInput = scan.nextLine();

if(strInput = Y && StrInput = N){
    System.out.println("Nice choice");
}else{
    System.out.println("Please Input only with Y or N");
}

答案1

得分: 1

如Rono所说,同时使用Equals并在输入检查之间使用||条件,可以是Y也可以是N。下面的代码片段将有所帮助。

System.out.println("你确定吗?");
String strInput = scan.nextLine();

if(strInput.equals("Y") || strInput.equals("N")){
    System.out.println("不错的选择");
}else{
    System.out.println("请输入Y或N");
}
英文:

As said by Rono as well Use Equals and use || condition between input checking, it can be either Y or N. Below snippet would help

	System.out.println("Are you sure?");
    String strInput = scan.nextLine();

    if(strInput.equals("Y") || strInput.equals("N")){
        System.out.println("Nice choice");
    }else{
        System.out.println("Please Input only with Y or N");
    }

huangapple
  • 本文由 发表于 2020年9月14日 23:37:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定