簡體   English   中英

陣列無法正確打印出所有答案

[英]Array not printing out all answers correctly

我正在制作一種主要方法,該方法創建一個數組來跟蹤某些總統候選人。 用戶輸入他們想要的候選人數量,然后輸入他們想要的選舉候選人名稱,然后打印出候選人所處的位置(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); 


}

在這里,下面的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;     
}

在您的第一個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);
    }
}

循環問題,代碼必須如下。

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


}

這是一個經常發生的問題。 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名候選人為:

  • “”
  • “愛麗絲”
  • “鮑勃”

帶回家的地方是記住在其他nextXXX()調用已部分讀取一行后,調用nextLine()刪除尾隨的換行符。

首先,您有一些不必要的字符串變量。 試試看,至少在我的機器上可以正常工作!

        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 ,則輸出應為“ The candidates names are: 1) Will Linger 2) Andy Putty 3) Jimmy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM