数组不能正确打印出所有答案

时间:2016-04-14 04:24:36

标签: java arrays

我正在创建一个主要方法来创建一个数组来跟踪一些总统候选人。用户输入他们想要的候选人数,然后在选举中输入他们想要的候选人姓名,然后打印出候选人的位置(1,2,3不是0,1,2)。现在,该方法仅打印出第二个和第三个名称,并且不会打印第一个名称。

public class RunElection


/**
 * 
 */
public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);
    System.out.println("Please input the number of candidates you would like in this election.");  
    int numCandidates = scan.nextInt();
    while (numCandidates <= 0) {
        System.out.println("Please input a number that is greater than zero.");   
        numCandidates = scan.nextInt(); 
    } 
    String[] candidates;
    candidates = new String[numCandidates];  

    System.out.println("Please input the name of the candidates in the election."); 
    String newCandidate = scan.nextLine(); 
    String newString = " "; 
    String finalString = " "; 

    for (int i = 0; i<candidates.length; i++) {
        candidates[i] = newCandidate; 
        newCandidate = scan.nextLine();    
    }

    for(int i = 0; i<candidates.length; i++) {
        newString = "the candidates names are: " + " " + i + " ) " + candidates[i];
        finalString = finalString+ newString;  
    }
    System.out.println(finalString); 


}

5 个答案:

答案 0 :(得分:3)

这里,以下for循环是问题

for (int i = 0; i<candidates.length; i++) {
        candidates[i] = newCandidate; 
        newCandidate = scan.nextLine();    
}

即使您觉得自己正在输入5个候选人,但实际上您已经在阵列中存储了4个。要解决此问题,只需将这些行交换为

即可
for (int i = 0; i<candidates.length; i++) {
    newCandidate = scan.nextLine();
    candidates[i] = newCandidate;     
}

答案 1 :(得分:0)

在您的第一个for循环中,您正在阅读numCandidates -1值而不是numCandidates,因此您可以在for循环中交换行:

for (int i = 0; i<candidates.length; i++) {
    newCandidate = scan.nextLine();
    candidates[i] = newCandidate;     
}

我猜你甚至可以删除变量newString并只使用变量finalString

import java.util.Scanner;

public class RunElection {
  public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.println("Please input the number of candidates you would like in this election.");
      int numCandidates = scan.nextInt();

      while (numCandidates <= 0) {
          System.out.println("Please input a number that is greater than zero.");
          numCandidates = scan.nextInt();
      }

      String[] candidates;
      candidates = new String[numCandidates];

      System.out.println("Please input the name of the candidates in the election.");
      String newCandidate = scan.nextLine();
      String finalString = "";

      for (int i = 0; i<candidates.length; i++) {
          newCandidate = scan.nextLine();
          candidates[i] = newCandidate;
      }

      for(int i = 0; i<candidates.length; i++) {
          finalString += "The candidates names are: " + " " + i + ") " + candidates[i] + "\n";
      }

      System.out.println(finalString);
    }
}

答案 2 :(得分:0)

循环问题,代码必须如下。

public class RunElection



public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);
    System.out.println("Please input the number of candidates you would like in this election.");  
    int numCandidates = scan.nextInt();
    while (numCandidates <= 0) {
        System.out.println("Please input a number that is greater than zero.");   
        numCandidates = scan.nextInt(); 
    } 
    String[] candidates;
    candidates = new String[numCandidates];  

    System.out.println("Please input the name of the candidates in the election."); 

    String newString = " "; 
    String finalString = " "; 

    for (int i = 0; i<candidates.length; i++) {
newCandidate = scan.nextLine();  
            candidates[i] = newCandidate; 

    }

    for(int i = 0; i<candidates.length; i++) {
        newString = "the candidates names are: " + " " + i + " ) " + candidates[i];
        finalString = finalString+ newString;  
    }
    System.out.println(finalString); 


}

答案 3 :(得分:0)

这是一个反复出现的问题。 nextInt()遇到非数字时会停止,并在输入流中保留非数字。

int numCandidates = scan.nextInt();
String newCandidate = scan.nextLine();

如果输入流包含:

3
Alice
Bob
Carol

它实际上看起来像扫描仪:

3 \n A l i c e \n B o b \n C a r o l \n

调用nextInt()后(按预期返回3),输入流保留:

\n A l i c e \n B o b \n C a r o l \n

调用nextLine()时,将读取并返回直到下一个换行符的所有字符,并消耗换行符。因此,""返回newCandidate,而不是"Alice"

如果在循环中完成:

int numCandidates = scan.nextInt();
for(int i=0; i<numCandidates; i++) {
    String newCandidate = scan.nextLine();
    //...
}

返回的3名候选人是:

  • “”
  • “爱丽丝”
  • “鲍勃”

回家是要记得在其他nextLine()来电部分读取线路后,调用nextXXX()删除尾随的换行符。

答案 4 :(得分:0)

首先你有一些不必要的字符串变量。试试这个,至少它在我的机器上工作正常!

:os.system_time(:milli_seconds)

编辑:示例

假设 String[] candidates = new String [numCandidates]; System.out.println("Please input the name of the candidates in the election, one on each line"); Scanner scan = new Scanner(System.in); StringBuilder finalCandidates = new StringBuilder(); for (int i = 0; i < candidates.length; i++) { candidates[i] = scan.nextLine(); } scan.close(); for(int i = 0; i < candidates.length; i++) { finalCandidates.append(i + 1 + ") " + candidates[i] + " "); } System.out.println("The candidates names are: " + " " + finalCandidates.toString()); 和输入的名称为 numCandidates = 3 ,则输出应为“Will Linger, Andy Putty, Jimmy