2016-11-08 10 views
1

。私は配列wrongAnswersを宣言する行に問題があります。私は前に私のコードを動作させることができましたが、問題は、一部の人が自分のファイルをすべて削除するためにそれを取ったということです。私はListまたはArrayListを使わずに動作させることができました。私はちょうど私がこれらの他の方法のどちらかを試してみる前に、これをどうやって得ることができるのか理解したい。私は、Java配列が不変であることを理解しています。しかし、私はまだ何らかの形でそれを働かせることができました。誰かが私が以前にしたことを理解するのを助けることができたら、私は最も偉大な人になるでしょう。配列サイズの宣言

private Scanner keyboard = new Scanner(System.in); 

private final String[] testAnswers = { 
     "B","D","A","A","C", 
     "A","B","A","C","D", 
     "B","C","D","A","D", 
     "C","C","B","D","A"}; 
private String[] studentAnswers = new String[20]; 
/* 
private String[] studentAnswers = { 
     "B","D","A","A","C", 
     "A","B","A","C","D", 
     "B","C","D","A","D", 
     "C","C","B","D","A"}; 
*/ 
private int[] wrongAnswers; 
private int answeredCorrectly = 0; 

public void getStudentAnswers() { 
    for(int x = 0; x < 20; x++) { 
     do { 
      System.out.print("Enter answer for #" + (x + 1) + " : "); 
      this.studentAnswers[x] = keyboard.next().toUpperCase(); 
      if (!"A".equals(this.studentAnswers[x]) && 
        !"B".equals(this.studentAnswers[x]) && 
        !"C".equals(this.studentAnswers[x]) && 
        !"D".equals(this.studentAnswers[x])) { 
       System.out.println("Invalid input."); 
      } 
     } while(!"A".equals(this.studentAnswers[x]) && 
       !"B".equals(this.studentAnswers[x]) && 
       !"C".equals(this.studentAnswers[x]) && 
       !"D".equals(this.studentAnswers[x])); 
    } 
} 

public int totalCorrect() { 
    int arrayLocation = 0; 

    for(int x = 0; x < 20; x++) { 
     if (this.studentAnswers[x].equals(this.testAnswers[x])) 
      this.answeredCorrectly++; 
     else 
      this.wrongAnswers[arrayLocation++] = x; 
    } 

    return this.answeredCorrectly; 
} 

public int totalIncorrect() { 
    return 20 - this.answeredCorrectly; 
} 

public boolean passed() { 
    return this.answeredCorrectly >= 15; 
} 

public void questionsMissed() { 
    if(this.answeredCorrectly != 20) { 
      for(int x = 0; x < this.wrongAnswers.length; x++) { 
      System.out.println(this.wrongAnswers[x]); 
     } 
    } 
} 
+0

[wrongAnswers =新しいint [someNumber]; 'しかし、代わりにリストを使うほうが良いと思います – litelite

+0

wrongAnswersの配列サイズは正しい答えの数に依存しているので、 。私がプログラムを走らせるたびに変わるだろう。番号はフレキシブルにする必要があります –

+1

手動で新しいものを手動で作成し、古いものと新しい誤った答えをコピーするたびにコピーする必要があります。あるいは、ArrayListを – litelite

答えて

1

コードがうまく書かれていれば、スペースを節約すること(通常は実行しようとしている)はパフォーマンスが低下し、その逆もあります。あなたはあなたが望むものを達成することができますが、あなたが見るように、あなたはパフォーマンスを失います。

私は同様の問題を解決する際に控除が有用であることがわかりました。条件:どのようにあなたが必要とどのくらいのスペースを知っています:

1)配列は 2)あなたはポイント2は疑問を提起

必要なスペースの正確な量を割り当てたい不変ですか?明白な答え:あなたが持っている正解の数を知っている。そこから、次の操作を実行でき:

public int totalCorrect() { 
    for(int x = 0; x < 20; x++) { 
     if (this.studentAnswers[x].equals(this.testAnswers[x])) 
      this.answeredCorrectly++; 
    } 

    this.wrongAnswers = int[20 - this.answeredCorrectly]; 

    // Here you want to create your wrongAnswers, but you have to go over 
    // the same array twice... 
    int arrayLocation = 0; 
    for(int x = 0; x < 20; x++) { 
     if (!this.studentAnswers[x].equals(this.testAnswers[x])) 
      this.wrongAnswers[arrayLocation++] = x; 
    } 


    return this.answeredCorrectly; 
} 

が同様の何かをすると、あまりにも優れたパフォーマンスを達成するために、おそらくより多くの方法があります。一見民間のint [] wrongAnswers = [20]新しいint型

+0

私はクラスに入ると、これを試してみます。私はついに陥り、ArrayListを学びました。しかし、私は教師が先に私がそれを使用している章を飛び越すためにポイントを取ることを確信しています –

0

...彼らは悪いアプローチのように私には思えるし、既に提案されているように私は、リストを使用したい、またはおそらく設定が、誰が知っています;

+0

それは間違っています –

関連する問題