私はArrays
(javaの)に苦しんでいますJava配列 - (並列アレイ)
これは私の人生でJavaを学ぶのは初めてなので、私はそれを起動する方法は考えています。私はちょうど配列を宣言する方法などを学んだ。しかし、これは私にとっては複雑すぎる。私はこの答えを見たらガイドラインを得ることができると思う。誰か助けてくれますか?
プログレ
私はArrays
(javaの)に苦しんでいますJava配列 - (並列アレイ)
これは私の人生でJavaを学ぶのは初めてなので、私はそれを起動する方法は考えています。私はちょうど配列を宣言する方法などを学んだ。しかし、これは私にとっては複雑すぎる。私はこの答えを見たらガイドラインを得ることができると思う。誰か助けてくれますか?
プログレ
java.util.Scanner
(API)を読みます。あなたのコードは動作します。スコアで必要なことを行い、ファイルから読み込み、平均スコアなどを計算するための適切なデータ型(整数)にキャストします。
変数は文字列なので、数値にキャストする必要がありますあなたの計算をしてください。
あなたはチームのためにプリミティブ型であなたのチームやコレクションのTeamMembersのインスタンス変数としてarraylistArrayList<TeamMember>
を使用することができますが、私はあなたがでインスタンス化チーム、TeamMemberとスコアのためのクラスを作る場合最高だと思いますあなたのボーリングクラス。この情報はどこにでもあります。
import java.util.Scanner;
//...
Team usa = new Team();
Team mexico = new Team();
TeamMember person = new TeamMember(usa); //Harry plays for the US
...
Scanner in = new Scanner(System.in);
int num = in.nextInt();
あなたは、すべての第三の入力は、あなたが3で割り切れるどの反復知ってモジュロ3(% 3
)を確認することができますスコアであることがわかっている場合。
私はあなたのリクエストの約40%を行っていますが、これで十分です。あなたは自分でそれを終えることができるはずです。
さらなる質問がある場合は、コメントを残してください。 (それを処理するためにあなたのためにいくつかの隠しバグが、あなたは自分の学習のために、最初のスコープを理解している必要がありあります。)
import java.io.*;
import java.util.*;
// declaration of the class
public class Bowling2 {
// declare arrays below
String Team, Member;
int Score, Scorew, Scoreb;
int[][] teamw = new int[10][3];
int[][] teamb = new int[10][3];
// declaration of main program
public static void main(String[] args) throws FileNotFoundException {
// 1. connect to input file
Scanner fin = new Scanner(new FileReader("bowling.txt"));
// 2) initialize array accumulators to zero
int i, j, Scoreb, Scorew = 0 ;
// 3) display a descriptive message
System.out.println(
"This program reads the lines from the file bowling.txt to determine\n"
+ "the winner of a bowling match. The winning team, members and scores\n"
+ "are displayed on the monitor.\n");
// 4) test Scanner.eof() condition
while (fin.hasNext()) {
// 5) attempt to input next line from file
Member = fin.next();
Team = fin.next();
Score = fin.nextInt();
// 6) test team color is blue
if (Team.toString() == "blue"){
// 7) then store blue member and score
teamb[i][0] = Member;
teamb[i][1] = Team;
teamb[i][2] = Score;
// 8) increase blue array accumulator
sumArray("blue");
i++;
}
// 9) else store white member and score
else {
teamw[j][0] = Member;
teamw[j][1] = Team;
teamw[j][2] = Score;
// 10) increase white array accumulator
sumArray("white");
j++;
}
}
// 11) if blue team score is larger
// 12) then display blue team as winner
// 13) else display white team as winner
// 14 disconnect from the input file
fin.close();
}
// implement method `sumArray()` below
/* 1. initialize accumulator to 0
2. loop over initialized array indices
3. increase accumulator by indexed array element
4. return accumulator
*/
public double sumArray(string color) {
if (color == "blue") {
for (int k = 0;k < teamb.length(); k++) {
//do the calculation here, and return it
}
}else {
for (int h = 0;h < teamw.length(); h++) {
//do the calculation here, and return it
}
}
}
// implement method `printArray()` below
/* 1. display the team name as the winner
2. loop over initialized array indices
3. display member and score for that array index
*/
}
宿題? :) 3つの別々の変数ではなく、各チームメンバーを表すためにクラスを使用することをお勧めします。次に、各チームメンバオブジェクトをファイルから読み込んで作成し、このオブジェクトを配列に追加することができます。あなたの関数は、この配列をループするか(またはラムダを使って)スコアを計算する必要があります(ifステートメントを使ってスコアを追加するチームを決定します)。勝利したチームの色を文字列値として保存し、再度ループしてその色に一致するすべてのチームメンバーを表示します(ここでもifステートメントを使用します)。これは役に立ちますか? – CoolBots
あなたは変数名を大文字にしています。これはJavaではあまり普及していません。それは読むのをより困難にするでしょう、そして、あなたのインストラクターはポイントを取るでしょう。 「チーム、メンバー、スコア」を小文字にします。 (チーム、メンバー、スコア) –
また、コードサンプルではまだ配列を使用していません。 –