Why is this giving me two outputs instead of one and how do I fix it?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
John Jamieson
il 6 Ott 2020
Commentato: John Jamieson
il 6 Ott 2020
if BirthM == 1
if BirthD == 1
fprintf (fileID,'\n you are %d years %d month and %d day old', BirthY,BirthM,BirthD);
else
fprintf (fileID,'\n you are %d years %d month and %d days old', BirthY,BirthM,BirthD);
end
fprintf (fileID,'\n you are %d years %d month and %d days old', BirthY,BirthM,BirthD);
end
if BirthM ~= 1
if BirthD == 1
fprintf (fileID,'\n you are %d years %d months and %d day old', BirthY,BirthM,BirthD);
else
fprintf (fileID,'\n you are %d years %d months and %d days old', BirthY,BirthM,BirthD);
end
fprintf (fileID,'\n you are %d years %d months and %d days old', BirthY,BirthM,BirthD);
end
I want this to only give me one message thats says depending on the month (You are something years, something months and something days old depending on whether or not the days or month equal one or not so I can change the message.
Every time I run this, it outputs the same message twice. Why?
Risposta accettata
Walter Roberson
il 6 Ott 2020
if BirthM == 1
if BirthD == 1
fprintf (fileID,'\n you are %d years %d month and %d day old', BirthY,BirthM,BirthD);
else
fprintf (fileID,'\n you are %d years %d month and %d days old', BirthY,BirthM,BirthD);
end
fprintf (fileID,'\n you are %d years %d month and %d days old', BirthY,BirthM,BirthD);
end
Look at the code again, formatted:
if BirthM == 1
if BirthD == 1
fprintf (fileID,'\n you are %d years %d month and %d day old', BirthY,BirthM,BirthD);
else
fprintf (fileID,'\n you are %d years %d month and %d days old', BirthY,BirthM,BirthD);
end
fprintf (fileID,'\n you are %d years %d month and %d days old', BirthY,BirthM,BirthD);
end
and it becomes more obvious that you do the two first fprintf() depending on BirthD, and that after having done that, then either way you proceed to do another fprintf()
You do not want that last fprintf() that is done unconditionally.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!