0
[SOLVED] (Thank you) Acing the class challenge.
I'm doing Acing the class code coach challenge. the idea is, the output will check against the name and score from the array and output that name alongside a pass or fail grade. (pass is a score of 70+) I've written the code to do the check and output whether its a pass or fail, but I'm struggling to figure out how to reference the name alongside the grade in the writeline command. can anyone give me a nudge in the right direction please? https://code.sololearn.com/c4a12A89a111
9 Respuestas
+ 5
Andrew Hall
value should be >= 70.
And also you have printed wrong value. You have to print Name with Passed or Failed.
so do this
//here exam.Keys will return all keys of List
//using keys you can get value
foreach (string s in exam.Keys) {
if (exam[s] >= 70)
Console.WriteLine(s + ": Passed");
else
Console.WriteLine(s + ": Failed");
}
+ 4
Alternatively, you can refer each element in the collection as a `KeyValuePair` struct like this
foreach( KeyValuePair<string, int> v in exam )
{
if ( v.Value > 69 )
{
Console.WriteLine( quot;{v.Key}: Passed" );
}
else
{
Console.WriteLine( quot;{v.Key}: Failed" );
}
}
You can shorten the block by using ternary conditional
foreach( KeyValuePair<string, int> v in exam )
{
Console.WriteLine( "{0}: {1}", v.Key, ( v.Value > 69 ? "Passed" : "Failed" ) );
}
And you can also use `var` keyword in place of `KeyValuePair<string, int>` struct in the `foreach` loop construct
foreach( var v in exam )
{
Console.WriteLine( "{0}: {1}", v.Key, ( v.Value > 69 ? "Passed" : "Failed" ) );
}
In this case, the `KeyValuePair` struct's `Key` property represents name, and `Value` property represents score.
+ 3
Atul
Sorted Map and HashMap are same. Both have key-value pair but there is difference in ordering. SortedMap maintain sorting order of keys but HashMap doesn't. There is no guarantee of order in HashMap.
Also SortedMap is an interface which extends Map interface and HashMap ia a class which implement Map interface..
+ 2
🅰🅹 🅐🅝🅐🅝🅣 Is Hashmaps in java equivalent to SortedList in c#?
+ 2
Andrew Hall
Let me ask you why should be there > 69 if in problem given that pass score is atleast 70?
What is the reason behind > 69?
+ 1
Atul
Yes it is like SortedMap in Java. Both have key-value pair.
+ 1
Thanks so much for replies. its much appreciated.
I've amended the code so that it now passes the challenge for anyone else who encounters a similar issue.
incidentally, to AJ - you mention that it should be >=70 rather than >69. is there any reason why it should be that way other than just preference?
0
Are sorted maps and hash maps are different 🅰🅹 🅐🅝🅐🅝🅣
0
As far as i can tell, both ways perform exactly the same function, but >69 is just that little bit easier to type.
Right back when i first started learning i encountered an issue where for some reason it wouldnt accept >=, but it did accept > and a number less. So i started using that instead.
So unless theres a valid reason beyond simple preference, ill probably continue to use >