How do I fix this error to output the correct data? I get an error when I use disp.

2 ビュー (過去 30 日間)
Hunter Fritsch
Hunter Fritsch 2020 年 11 月 22 日
コメント済み: Hunter Fritsch 2020 年 11 月 22 日
%Take user input for the weights (kg) of all fish from the haul as a vector
fishWeight = input('Enter the weights of the fish with [ ] around and a space between the values.');
%Use a loop to determine the total weight for each grade of fish in the haul, and the total income for each grade of fish
wholeFishWeight = 0;
wholeFishIncome = 0;
filletFishWeight = 0;
filletFishIncome = 0;
canningFishWeight = 0;
canningFishIncome = 0;
petFoodFishWeight = 0;
petFoodFishIncome = 0;
fishExtractsFishWeight = 0;
fishExtractsFishIncome = 0;
for(i = 1 : length(fishWeight))
if(fishWeight(i)>=30)
wholeFishWeight = wholeFishWeight + fishWeight(i)
wholeFishIncome = wholeFishIncome + 22*fishWeight(i)
elseif(fishWeight(i)>=20)
filletFishWeight = filletFishWeight + fishWeight(i)
filletFishIncome = filletFishIncome + 16*fishWeight(i)
elseif(fishWeight(i)>=10)
canningFishWeight = canningFishWeight + fishWeight(i)
canningFishIncome = canningFishIncome + 5*fishWeight(i)
elseif(fishWeight(i)>=5)
petFoodFishWeight = petFoodFishWeight + fishWeight(i)
petFoodFishIncome = petFoodFishIncome + 3*fishWeight(i)
else
fishExtractsFishWeight = fishExtractsFishWeight + fishWeight(i)
fishExtractsFishIncome = fishExtractsFishIncome + 1*fishWeight(i)
end
end
%Determine the total income resulting from selling the haul
totalIncome = wholeFishIncome + filletFishIncome + canningFishIncome + petFoodFishIncome + fishExtractsFishIncome;
%Output a detailed invoice including the weight of each grade of fish, the total income for each grade of fish, and the total income from the entire haul
disp('You caught ' + wholeFishWeight + ' kg of fish for Whole Fish, to be sold for $' + wholeFishIncome)
disp('You caught ' + filletFishWeight + ' kg of fish for Fillet, to be sold for $' + filletFishIncome);
disp('You caught ' + canningFishWeight + ' kg of fish for Canning, to be sold for $' + canningFishIncome);
disp('You caught ' + petFoodFishWeight + ' kg of fish for Pet Food, to be sold for $' + petFoodFishIncome);
disp('You caught ' + fishExtractsFishWeight + ' kg of fish for Fish Extracts, to be sold for $' + fishExtractsFishIncome);
disp('Your total income for this haul is $' + totalIncome);

回答 (1 件)

Nora Khaled
Nora Khaled 2020 年 11 月 22 日
if the error in "disp", use
or maybe this works
X=['You caught ' , wholeFishWeight , ' kg of fish for Whole Fish, to be sold for $' , wholeFishIncome];
disp(X)
  1 件のコメント
Hunter Fritsch
Hunter Fritsch 2020 年 11 月 22 日
I tried that but I stil get this error message
Error using +
Matrix dimensions must agree.
Error in fhunter_C4_P2 (line 40)
X=['You caught ' + wholeFishWeight + ' kg of fish for Whole Fish, to be sold for $'
, wholeFishIncome];

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePackage MATLAB Functions についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by