+ 2
About the challenge Level Points
I passed it 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 int total = (levels + 1) * levels / 2; return total; } i was wondering if there was a way to use for or foreach method, i had try but cant figure it our. thanks if reply.
4 ответов
+ 5
In description you have Total: 1+2+3 = 6 points where 1, 2 and 3 are levels. Therfore easier way is to use a for loop:
int total = 0
for (int i = levels; i > 0; i--){
total += i;
}
return total;
A foreach loop has no sense because for it will be needed an array. In such as loop will be accessed the array elements directly.
+ 2
JaScript wow thanks! I tried the same code as you! Just dont know why i cant run it well at the first time...... must missing something i guess. Happy to know having the right concept.
and you are right about the foreach way, totally unnessary.
+ 2
Congratulation and happy coding furthermore.
+ 2
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==0){
return 0;
}else{
return levels + Points(levels-1);
}
}
}
}
hooooraaaaaaa!!!!!!