누군가이 코드를 살펴보고 for 루프가 종료되었을 때 합계가 출력되지 않는 이유를 결정하는 데 도움이 될 수 있습니까? 대신 형식 문자열에 대한 추가 정보를 원하시면 here를 참조 n은 형식 문자열 후 Console.WriteLine
에 n 번째 매개 변수가 {n}
, 즉C# 프로그램의 합계에 대한 출력 없음
Console.WriteLine("The sum is: {0}", sum);
를 사용
Console.WriteLine("The sum is: []", sum);
을하고
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*This program finds 100 3 digit numbers between 100 and 1000, prints the message: "Cha, Cha, Cha!" after every 10th number, and outputs the sum of the numbers */
namespace MikeVertreeseRandom
{
class RandomNumbers //using the random class for number generation
{
static void Main(string[] args)
{
Random r = new Random();
int number = 0; //r.Next() finds the next random # bet 100 and 1000
int sum = 0; //declaring the variable "numberTotal" for the sum
int i = 1; //i is the index counter
for (i = 1; i < 100; i++) //the program will run through 100 iterations
{
number = r.Next(100, 1000);
Console.WriteLine(number); //program prints the next random #
sum += number; //need to keep a running sum of the numbers found
if ((i % 10) == 0) //every 10th iteration, do something
{
Console.WriteLine("Cha, Cha, Cha!"); //prints this message every 10th number
}
}
Console.WriteLine("The sum is: []", sum);
Console.ReadLine();
}
}
}
'class RandomNumbers {'의 여는 중괄호가 없습니다. –
'i'를 두 번 초기화합니다. 대신에 for 루프 자체에서 선언해야합니다 : for (int i = 1; i <100; i ++) {...}' –
Thanks Tim ~~ 'RandomNumbers 클래스의 여는 중괄호 '는 그 자체의 라인에있다. 인덱스 변수를 적절히 초기화하는 것을 염두에 두겠습니다 (전에 대신 for 루프 내에서). -m – user1527399