Level Points Task C# - I need help with this. | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 4

Level Points Task C# - I need help with this.

static void Main(string[] args) { int levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(levels)); } static int Points(int levels) { //your code goes here if(levels < 1){ levels = levels + 0; } else if(levels < 2){ levels = levels + 1; } else if(levels < 3){ levels = levels + 2; } else if(levels == 3){ levels = levels + 3; } } I used multiple else if statements within the method as the user enters the level which increases their Level Points if the condition is true however its not working. I need help on this.

17th Dec 2020, 12:14 PM
aka :)
aka :) - avatar
16 Antworten
+ 18
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int Levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(Levels)); } static int Points(int Levels) { //your code goes here if (Levels == 1) return 1; return Levels + Points(Levels - 1); } } }
9th Jan 2021, 5:25 PM
08. Dharma Soegiantoro
08. Dharma Soegiantoro - avatar
+ 4
static int Points(int levels) { //your code goes here if(levels==1){ return 1; } return levels+Points(levels-1); } I tried this logic and it worked for me and m sure it will work for everyone.This is same as the recursive example given in sololearn ,only small change is there "levels+Points(levels-1)" for calculate total no of points.
20th Mar 2021, 9:09 AM
Lisa
+ 1
That's a solution, only if the input is between 1 and 3. However, the input range is not given in the question. It only mentions that the point starts from 1. What if the input is 4, 5, 10, 1000? Apparently, multiple if-else statements isn't ideal.
17th Dec 2020, 12:23 PM
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż - avatar
+ 1
It should be a recursive function and increment by 1 for each levels: ----------------------------------------------------------------------------------- using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int Levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(Levels)); } static int Points(int Levels) { //your code goes here if (Levels == 1) return 1; return Levels + Points(Levels - 1); } } }
2nd Mar 2021, 4:51 PM
Gashaye Anley
+ 1
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int Levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(Levels)); } static int Points(int Levels) { //your code goes here if (Levels == 1) return 1; return Levels + Points(Levels - 1); } } } this is the answer
30th Mar 2021, 5:31 AM
ANIKET SINGH
ANIKET SINGH - avatar
+ 1
One line solution using lambdas and ternary operator static Func<int, int> Points = levels => levels == 1 ? 1 : levels + Points(levels - 1);
20th Aug 2021, 7:45 AM
David GarcĂ­a Prados
0
what u want to do?
17th Dec 2020, 12:18 PM
durian
durian - avatar
0
This scenario : Passing the first level of a video game awards the player 1 point. For each subsequent level passed, the points awarded increment by 1 (2 for the 2nd level, 3 for the 3rd, and so on). The program you are given takes the number of passed levels as input. Complete the given function to take that number as an argument, and recursively calculate and return the total number of points given for all passed levels. Sample Input 3 Sample Output 6 Explanation Level 1: 1 point Level 2: 2 points Level 3: 3 points Total: 1+2+3 = 6 points.
17th Dec 2020, 12:19 PM
aka :)
aka :) - avatar
0
With the else if statements or something else? I don't really understand.
17th Dec 2020, 12:23 PM
aka :)
aka :) - avatar
0
why dont try for loop? get current level,and add points from cur point to 1
17th Dec 2020, 12:23 PM
durian
durian - avatar
0
Alright then. Wow that sounded simple. Lily Mea
17th Dec 2020, 12:24 PM
aka :)
aka :) - avatar
0
I finally did it by using recursion. Thank you CarrieForle and Lily Mea
17th Dec 2020, 1:06 PM
aka :)
aka :) - avatar
0
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int Levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(Levels)); } static int Points(int Levels) { //your code goes here if (Levels == 1) return 1; return Levels + Points(Levels - 1); } } }
7th Feb 2021, 5:07 PM
Md Momin Uddin Hridoy
Md Momin Uddin Hridoy - avatar
0
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(levels)); } static int Points(int levels) { //your code goes here int points1 = 0; for (; levels > 0;levels--) { points1 += levels; } return points1; } } }
16th Apr 2022, 8:10 PM
Martin Chakarov
0
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(levels)); } static int Points(int levels) { int n=1; int points=0; //your code goes here while(n<=levels) { Points=n+points; n++; } return points; } } }
18th Sep 2022, 5:52 PM
zaki Habeshiye
zaki Habeshiye - avatar
- 3
static int Points(int levels) { int pts = 0; while (levels >0) { pts = pts + levels; levels = levels - 1; } return pts; }
8th Mar 2021, 6:54 PM
Ömer Karagöz
Ömer Karagöz - avatar