2013-11-26 29 views
0

我正在尋找用戶輸入一個名稱以輸入到數組中,然後程序會給出選項來輸入另一個名稱,所以我想第二個名字被輸入到下一個索引中陣列。目前,我只能獲得名稱來填充數組的所有索引,否則只能填充它覆蓋的第一個索引。遞增數組索引輸入

下面的代碼是將其寫入到所有指數的:

下我的立場,它爲什麼以此爲nameArraycount被每次遞增,它符合語句的條件,但我不知道如何我可以讓它退出while循環並遞增值。如果我嘗試下面的代碼,程序會在名稱輸入後掛起 - 我認爲這是由於它永遠增加了nameArrayCount

while (nameArrayCount < 10) { 
    studentNamesArray[nameArrayCount] = studentName; 
} 
nameArrayCount++; 

我怎樣才能讓程序只輸入一個新的索引如果條件爲真,但要然後增加nameArraycount和退出while循環? (如果用戶然後選擇輸入另一個名稱的選項,我想檢查少於10個已輸入)

我試過使用if語句,但我只能得到它填充數組中的第一個索引如果用戶輸入第二個名字,則保持覆蓋。

+0

您只給出部分代碼。此代碼之前的實際輸入是否讀取?如果是這樣的話,它在根本上被打破了,因爲在循環期間沒有新的數據被讀取,使得它一遍又一遍地使用相同的字符串。 – Smallhacker

+0

輸入讀數確實在顯示的代碼之前。將輸入的值傳遞給一個單獨的函數將它們插入到數組中會更好嗎? – Colin747

回答

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; 
    } 
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; 
    } 
}  
+0

這也適用 - 但他的教授指定了一個數組:P – lscoughlin

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

這將防止它覆蓋數組中的名稱,因爲索引在賦值後總是直接增加。

+0

不幸的是,我只限於非OO方法,但是我會將您的答案投給其他任何可能會發現這種方法的人,因爲我同意這是首選方法。 – Colin747

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(); 

} 
+0

'while(true && names.size()<10)'這裏不需要那個'true'。 – Thomas

+0

@Thomas,真的:) –

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)) 

    } 

} 
+0

不是我正在尋找的,在輸入一個名字後,我想讓它退出將用戶帶回菜單的功能。只有當他們選擇再次輸入一個名字時,我希望它們被帶回到該函數中,然後將名稱寫入下一個索引 – Colin747

0

所以,如果我理解正確的話,你需要做的是瞭解以下信息:

if (containsName(studentNamesArray, studentName) == false) { 
    addStudent(studentNamesArray, studentName); 
} 

除此之外,這可能與Java Set可以輕鬆解決這樣的:

studentSet.add(studentName); // automatically ignores duplicates 

所有你需要做的第一個代碼部分是編寫函數containsNameaddStudent

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? 
    } 
} 

有這個代碼不檢查幾件事情:

  • 如果有更多的學生比數組的大小?
  • 如果studentName是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 
+0

我試圖實現您的解決方案,但它似乎仍然覆蓋數組中的第一個索引。 – Colin747

+0

看來'containsName'總是返回'false' – Colin747

+0

請將我的完整代碼與您的代碼進行比較。這些函數工作正常,所以在你的實現中必須有一個錯誤。 – TwoThe