在我正在处理的程序中,我创建了一个循环来接收20个单独的字符作为用户输入,转换为char,存储在array2中,并将array2返回到main.当我运行我编写的程序时,似乎我编写的代码没有正确地存储array2中的字符.
主要:
// Create array to hold user's answers, and pass answers to the array.
char array2[ ] = new char[20];
getAnswers(array2);
Run Code Online (Sandbox Code Playgroud)
在getAnswers()中:
// getAnswers method requests user input and passes to array2.
public static char[ ] getAnswers(char array2[ ])
{
String input; // Holds user input.
Scanner keyboard = new Scanner(System.in);
// Request user input.
System.out.println("Enter the answers for the the multiple choice exam.");
// Loop to receive input into array.
for (int index = 0; index < 20; index++)
{
System.out.print("Enter number " + (index + 1) +": ");
input = keyboard.nextLine();
array2 = input.toCharArray();
}
return array2;
}
Run Code Online (Sandbox Code Playgroud)
尝试
array2[index] = input.charAt(0);
Run Code Online (Sandbox Code Playgroud)
在将值输入到输入变量之后,而不是每次通过循环为其分配一个新的char数组.
| 归档时间: |
|
| 查看次数: |
493 次 |
| 最近记录: |