如何将用户输入的字符串作为单个字符的数组?
How to get a user-input string as an array of individual characters?
我希望用户输入一个字符串,然后将该字符串的每个字符分配给数组中的一个元素。
到目前为止,这是我的代码:
import java.util.Scanner;
public class Apples {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = userInput.nextLine();
int arrayLength = name.length();
String ArrayName[] = new String [arrayLength];
for(int counter = 0; counter < arrayLength; counter++){
ArrayName[counter] = name.substring(counter);
System.out.println("Element No" + counter + ": " + name.substring(counter));
}
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = userInput.nextLine();
int arrayLength = name.length();
String ArrayName[] = new String [arrayLength];
char[] chars = name.toCharArray();
for(int counter = 0; counter < arrayLength; counter++) {
ArrayName[counter] = chars[counter] + "";
System.out.println("Element No" + counter + ": " + name.substring(counter));
}
}
进口java.util.Scanner;
public class苹果{
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = userInput.nextLine();
int arrayLength = name.length();
int temp = 1;
String ArrayName[] = new String [arrayLength];
for(int counter = 0; counter < arrayLength; counter++){
ArrayName[counter] = name.substring(counter, name.length()-name.length()+temp);// HAD TO ADD THIS TO GET IT WORKING
temp++;
System.out.println("Element No" + counter + ": " + ArrayName[counter]);
}
您需要使用字符串的 toCharArray()
方法。如果您在字符串上调用此方法,它将 return 一个 chars
的数组,完全符合您的需要:
import java.util.Scanner;
public class Apples {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = userInput.nextLine();
char[] nameArray = name.toCharArray();
for(int counter = 0; counter < nameArray.length; counter++){
System.out.println("Element No" + counter + ": " + nameArray[counter]);
}
}
}
查看上面链接的文档以查看其工作原理以及您可以在 String 对象上调用的其他方法的完整说明。
我希望用户输入一个字符串,然后将该字符串的每个字符分配给数组中的一个元素。
到目前为止,这是我的代码:
import java.util.Scanner;
public class Apples {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = userInput.nextLine();
int arrayLength = name.length();
String ArrayName[] = new String [arrayLength];
for(int counter = 0; counter < arrayLength; counter++){
ArrayName[counter] = name.substring(counter);
System.out.println("Element No" + counter + ": " + name.substring(counter));
}
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = userInput.nextLine();
int arrayLength = name.length();
String ArrayName[] = new String [arrayLength];
char[] chars = name.toCharArray();
for(int counter = 0; counter < arrayLength; counter++) {
ArrayName[counter] = chars[counter] + "";
System.out.println("Element No" + counter + ": " + name.substring(counter));
}
}
进口java.util.Scanner;
public class苹果{
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = userInput.nextLine();
int arrayLength = name.length();
int temp = 1;
String ArrayName[] = new String [arrayLength];
for(int counter = 0; counter < arrayLength; counter++){
ArrayName[counter] = name.substring(counter, name.length()-name.length()+temp);// HAD TO ADD THIS TO GET IT WORKING
temp++;
System.out.println("Element No" + counter + ": " + ArrayName[counter]);
}
您需要使用字符串的 toCharArray()
方法。如果您在字符串上调用此方法,它将 return 一个 chars
的数组,完全符合您的需要:
import java.util.Scanner;
public class Apples {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = userInput.nextLine();
char[] nameArray = name.toCharArray();
for(int counter = 0; counter < nameArray.length; counter++){
System.out.println("Element No" + counter + ": " + nameArray[counter]);
}
}
}
查看上面链接的文档以查看其工作原理以及您可以在 String 对象上调用的其他方法的完整说明。