gpt4 book ai didi

java - 如何判断用户输入是否有x个字符

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:55 24 4
gpt4 key购买 nike

尝试创建另一个 && 来查看 lock_combination 是否正好是 9 个字符长

import java.util.Scanner;

public class charlespeppers {

public static void main(String[] args) {

Scanner user_input = new Scanner (System.in);

String lock_combination;
System.out.print ("Please enter a lock combination ( ddRddLddR ) : ");
lock_combination = user_input.next();


if (lock_combination.charAt(0) >= '0' && lock_combination.charAt(0)<= '9'
&& lock_combination.charAt(1) >= '0' && lock_combination.charAt(1)<= '9'
&& lock_combination.charAt(3) >= '0' && lock_combination.charAt(3)<= '9'
&& lock_combination.charAt(4) >= '0' && lock_combination.charAt(4)<= '9'
&& lock_combination.charAt(6) >= '0' && lock_combination.charAt(6)<= '9'
&& lock_combination.charAt(7) >= '0' && lock_combination.charAt(7)<= '9'
&& lock_combination.charAt(2) == 'R'
&& lock_combination.charAt(8) == 'R'
&& lock_combination.charAt(5) == 'L'
&& lock_combination == 9);
{
System.out.println("This is a valid lock combination.");
}

}
}

最佳答案

您可以使用length() String 的方法:

public int length(): Returns the length of this string. The length is equal to the number of Unicode code units in the string.

if (... && lock_combination.length() == 9)
System.out.println("This is a valid lock combination.");

关于java - 如何判断用户输入是否有x个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21973163/

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