Java 字符数据类型的扫描仪
是否有任何方法可以在不使用字符串的情况下输入字符数据类型。 例如: 如果必须初始化字符数组,则通常采用以下格式: c[i]=string.next().charAt(i) 我不想使用next()方法 你能告诉我其他的方法吗Java 字符数据类型的扫描仪,java,Java,是否有任何方法可以在不使用字符串的情况下输入字符数据类型。 例如: 如果必须初始化字符数组,则通常采用以下格式: c[i]=string.next().charAt(i) 我不想使用next()方法 你能告诉我其他的方法吗 //System.out.println("Enter the String"); c[i]=s1.next().charAt(i); int n; Scanner s1=new Scanner(System.in);
//System.out.println("Enter the String");
c[i]=s1.next().charAt(i);
int n;
Scanner s1=new Scanner(System.in);
System.out.println("Enter the number of string arrays");
n=s1.nextInt();
char[] c=new char[n];
for(int i=0;i<n;i++)
{
//System.out.println("Enter the String");
c[i]=s1.next().charAt(i); // I dont want to use this .
}
for(int i=0;i<n;i++)
{
System.out.print(c[i]);
}
//System.out.println(“输入字符串”);
c[i]=s1.next().charAt(i);
int n;
扫描仪s1=新的扫描仪(System.in);
System.out.println(“输入字符串数组的数量”);
n=s1.nextInt();
char[]c=新字符[n];
为了(inti=0;i在java中接受字符作为输入,我们没有任何特定的输入流,比如整数{nextInt()
}。
因此,我们剩下的选择是使用next()
,其中有一点扭曲,如下所示-
Scanner sc = new Scanner(System.in);
char inputCharacter = sc.next().charAt(0);
在您的情况下,如果您想读取多个字符,您可以在索引0处读取它们,并继续将它们附加到您的字符数组中。
下面是代码片段-
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
char arr[] = new char[n];
for(int i=0;i<n;i++) {
arr[i] = sc.next().charAt(0);
}
for(int i = 0; i< n; i++){
System.out.println(arr[i]);
}
}
publicstaticvoidmain(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
char arr[]=新字符[n];
对于(int i=0;iithith
字符串的字符,您正试图读取它?谢谢,它起作用了….,但是为什么我们在字符中传递0,而不是传递整数i。像字符(i)..???因为您正试图从第i个索引输入的字符中读取字符。不管怎样,无论何时输入新字符,它都将始终位于第0个索引。