[英]java boolean array with size from user input
I am prompting the user to enter binary digits as a String
. 我提示用户输入二进制数字作为
String
。 I am trying to declare and instantiate an array of boolean
elements. 我正在尝试声明和实例化
boolean
元素数组。 The array should be equal to the number of digits in the String
entered by user. 该数组应等于用户输入的
String
的位数。 Not sure what I'm doing wrong here but am thinking I need to use the String
length()
method? 不知道我在这里做错了什么,但是我想我需要使用
String
length()
方法吗?
public class Binary {
public static void main ( String [] args ) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter some binary digits > ");
String value = scan.nextLine();
boolean [] booleanArray = new boolean [value];
}
}
public class Binary {
public static void main ( String [] args ) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter some binary digits > ");
String value = scan.nextLine();
boolean [] booleanArray = new boolean [value.length()];
//note that initially the values stored in the array are all false
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.