Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 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.
16 Answers
+ 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);
}
}
}
+ 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.
+ 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.
+ 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);
}
}
}
+ 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
+ 1
One line solution using lambdas and ternary operator
static Func<int, int> Points = levels => levels == 1 ? 1 : levels + Points(levels - 1);
0
what u want to do?
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.
0
With the else if statements or something else? I don't really understand.
0
why dont try for loop?
get current level,and add points from cur point to 1
0
Alright then. Wow that sounded simple. Lily Mea
0
I finally did it by using recursion. Thank you CarrieForle and Lily Mea
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);
}
}
}
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;
}
}
}
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;
}
}
}
- 3
static int Points(int levels)
{
int pts = 0;
while (levels >0) {
pts = pts + levels;
levels = levels - 1;
}
return pts;
}