我正在寻找用户输入要输入到数组中的名称,然后程序将提供输入另一个名称的选项,因此我希望将第二个名称输入到数组中的下一个索引中。目前我只能获取名称来填充数组的所有索引,或者只是第一个覆盖它的数据。
以下代码将其写入所有索引:
while (nameArrayCount < 10) {
studentNamesArray[nameArrayCount] = studentName;
nameArrayCount++;
}
我理解为什么会这样做,因为nameArraycount
每次都会增加,并且符合声明的条件,但我不知道如何让它退出while
循环并递增值。如果我尝试以下代码,程序会在输入名称后挂起 - 我假设是由于它永远递增nameArrayCount
?
while (nameArrayCount < 10) {
studentNamesArray[nameArrayCount] = studentName;
}
nameArrayCount++;
如果该条件为真,我怎么能让程序只输入一个新索引,然后递增nameArraycount
并退出while循环? (如果用户然后选择输入另一个名称,我想检查的名称是否已输入少于10)
我尝试过使用if
语句但我只能填充数组中的第一个索引,如果用户输入第二个名字,它会一直覆盖。
答案 0 :(得分:2)
你应该真的避免这样做,而是采取OOP方法。
public class Student
{
private String firstName;
private String secondName;
public Student(String firstName, String secondName)
{
this.firstName = firstName;
this.secondName = secondName;
}
}
然后你可以简单地创建一个Student
类型的数组,并根据需要填充它。
示例
Student[] students = new Student[10];
int position = 0;
String firstName = scanner.readLine();
String secondName = scanner.readLine();
students[position++] = new Student(firstName, secondName);
更简单的是使用List
实现。
示例
List<Student> students = new ArrayList<Student>();
String firstName = scanner.readLine();
String secondName = scanner.readLine();
students.add(new Student(firstName, secondName));
如果必须使用数组
您需要某种形式的占位符来保持您在阵列中的位置。
int position = 0;
您还需要一个输入机制,我们假设它是scanner
。
studentNamesArray[position++] = scanner.readLine();
这样做会返回position
的值(此刻为0),然后递增它,使其成为1
。
这将阻止它覆盖数组中的名称,因为索引总是在赋值后直接增加。
答案 1 :(得分:0)
我没有彻底阅读你的帖子,但我觉得最好使用List<String>
并在输入时添加学生姓名。之后,您只需查询列表的大小即可获得输入的名称数量。
这就像这个伪代码:
List<String> names = new ArrayList<>();
while( names.size() < 10 ) {
String name = //ask the user for another name
if( meetsCondition(name) ) {
names.add( name );
}
//maybe also add an option to enter less names, e.g. ask the user
if( userWantsToFinish() ) {
break;
}
}
int numNamesEntered = names.size();
如果确实必须使用数组,请将列表替换为数组并分别跟踪名称数量:
String[] names = new String[10];
int numNamesEntered = 0 ;
while( numNamesEntered < names.length ) {
String name = //ask the user for another name
if( meetsCondition(name) ) {
names[numNamesEntered] = name;
numNamesEntered++; //for clarity, could also be done in the line above
}
//maybe also add an option to enter less names, e.g. ask the user
if( userWantsToFinish() ) {
break;
}
}
答案 2 :(得分:0)
您可以使用ArrayList
并检查大小。这样,如果用户在10之前停止,则不会停留空值。
ArrayList<String> names = new ArrayList<String>();
Scanner scanner = new Scanner(System.in);
String input = "";
String name;
while (names.size() < 10) {
if (input.equalsIgnoreCase("no") break;
System.out.println("Enter an name: ");
name = input.nextLine();
names.add(name);
System.out.println("Would you like to continue?");
input = scanner.nextLine();
}
答案 3 :(得分:0)
当然,我会做你的作业!
在此文本框中编码 - 我将为您修复它。
public class HomeWorkAssignment {
public static void main( String... args ) throws Exception {
String arrayOfNames = new ArrayOfNames[10];
Scanner scanner = new Scanner(System.in)
for( int i = 0; i < arrayOfNames.length; i++ ) {
if( scanner.hasNextLine() ) {
String studentName = scanner.nextLine();
arrayOfNames[i] = studentName;
} else {
break;
}
}
System.out.printf( "Names: %s", Arrays.toString( studentNames ) )
}
}
答案 4 :(得分:0)
因此,如果我理解正确,您想要做的是关于以下内容:
if (containsName(studentNamesArray, studentName) == false) {
addStudent(studentNamesArray, studentName);
}
除此之外,这可以通过这样的Java Set轻松解决:
studentSet.add(studentName); // automatically ignores duplicates
您需要为第一个代码部分做的就是编写函数containsName
和addStudent
boolean containsName(String[] studentNamesArray, String studentName) {
for (int i = 0; i < studentNamesArray.length; ++i) { // specialized while loop over all array elements
if (studentName.equals(studentNamesArray[i])) { // use equals for String comparison! This will as well not match on "null"
return true; // we found a match
}
}
return false; // no match was found
}
和
void addStudent(String[] studentNamesArray, String studentName) {
int i = 0;
while ((studentNamesArray[i] != null) && // this student entry is already filled by someone else
(i < studentNamesArray.length)) { // we are still within the array bounds
++i;
}
if (i < studentNamesArray.length) { // is i still within the array bounds?
studentNamesArray[i] = studentName; // add the student
} else {
// we have too many students. What to do now?
}
}
此代码中未检查以下几项内容:
null
怎么办?这种编写代码的方法称为Top-down approach,如果你知道通用程序逻辑,那么编码会更容易,但还不知道细节。
修改:完整代码:
import java.util.Scanner;
public class Test {
static boolean containsName(String[] studentNamesArray, String studentName) {
for (int i = 0; i < studentNamesArray.length; ++i) { // specialized while loop over all array elements
if (studentName.equals(studentNamesArray[i])) { // use equals for String comparison! This will as well not match on "null"
return true; // we found a match
}
}
return false; // no match was found
}
static void addStudent(String[] studentNamesArray, String studentName) {
int i = 0;
while ((studentNamesArray[i] != null) && // this student entry is already filled by someone else
(i < studentNamesArray.length)) { // we are still within the array bounds
++i;
}
if (i < studentNamesArray.length) { // is i still within the array bounds?
studentNamesArray[i] = studentName; // add the student
} else {
// we have too many students. What to do now?
}
}
public static void main(String[] args) {
final String[] studentNamesArray = new String[10];
String studentName;
final Scanner scanner = new Scanner(System.in);
do {
System.out.println("Please insert your name:");
studentName = scanner.nextLine();
if (studentName.length() > 0) {
if (containsName(studentNamesArray, studentName) == false) {
addStudent(studentNamesArray, studentName);
System.out.println(studentName + " added.");
} else {
System.out.println(studentName + " was already in the list.");
}
}
} while (studentName.length() > 0);
for (int i = 0; i < studentNamesArray.length; ++i) {
System.out.println("Student #" + i + ": " + studentNamesArray[i]);
}
}
}
使用示例输入,我得到以下内容:
Please insert your name: Two Two added. Please insert your name: Test Test added. Please insert your name: Test Test was already in the list. Please insert your name: test test added. Please insert your name: test test was already in the list. Please insert your name: Student #0: Two Student #1: Test Student #2: test Student #3: null Student #4: null Student #5: null Student #6: null Student #7: null Student #8: null Student #9: null
答案 5 :(得分:0)
最后这是我用过的解决方案,以防其他人做类似的事情,我会说,如果你有选择,你应该明确地按照@Chris给出的答案。
String [] studentNamesArray = new String [10];
int nameArrayCount = 0;
String studentName;
if (nameArrayCount < 10) {
System.out.println("Enter the student's name in the following format - surname, forename: ");
studentName = input.next();
studentNamesArray[nameArrayCount] = studentName;
nameArrayCount = nameArrayCount + 1;
}