배열을 쓰려고합니다. 그 설명은 가장이에 의해 설명 :트러블 쓰는 배열
import java.util.*;
import java.text.*;
public class DailyCatch
{
private int fishermanID, fisherID;
private String dateOfSample, date;
private double[] weights;
private double[] fishCaught = new double[10];
private int currWeight = 0;
public DailyCatch (int fishermanID, String dateOfSample, String weightsAsString)
{
fisherID = fishermanID;
date = dateOfSample;
// Parse the the weigths string and store the list of weights in this array.
weights = readWeights(weightsAsString);
}
public DailyCatch (int fishermanID, String dateOfSample)
{
fisherID = fishermanID;
date = dateOfSample;
}
public void addFish(double weight)
{
if (currWeight > 10)
{
// array full
}
else
{
fishCaught[currWeight] = weight;
currWeight += 1; // update current index of array
}
}
private double[] readWeights(String weightsAsString)
{
String[] weightsArr = weightsAsString.split("\\s+");
double[] weights = new double[weightsArr.length];
for (int i = 0; i < weights.length; i++) {
double weight = Double.parseDouble(weightsArr[i]);
}
return weights;
}
public void printWeights()
{
for (int i = 0; i < fishCaught.length; i++)
{
System.out.println(fishCaught[i]);
}
}
public String toString()
{
return "Fisherman ID: " + fisherID + "\nDate: " + date + "\nWeights: " + Arrays.toString(weights);
}
}
이 나는이 프로젝트에 함께 일하고 있어요 테스트 파일 :
Each day a fisherman will weigh in at most 10 fish, the weight of which you are required to store in an array of double values.
이것은 내가 지금까지 무엇을 가지고
public class BigBass
{
public static void main (String[]args)
{
DailyCatch monday1 = new DailyCatch(32, "4/1/2013", "4.1 5.5 2.3 0.5 4.8 1.5");
System.out.println(monday1);
DailyCatch monday2 = new DailyCatch(44, "4/1/2013");
monday2.addFish(2.1);
monday2.addFish(4.2);
System.out.println(monday2);
}
}
도움을 주시면 대단히 감사하겠습니다.
먼저 기본 생성자가 필요합니다. 그리고 그것은 물고기 배열을 정의하는 것 같습니다. – David
예. 하지만 그것은 이중을 요구하고 문자열을 발견하면 오류가 발생합니다. –
그의 물고기에 대한 double 값의 배열을 할당하는 남자를 가르치면 그는 일생 동안 먹을 것이다. – Patashu