程序的COntrol不会进入for循环

Ton*_*ony 0 c#

我在这里粘贴代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace star1
{
    class Program
    {
        static void Main(string[] args)
        {
            Myclass obj = new Myclass();
            obj.getData();
            obj.pattern();
        }
    }
    class Myclass
    {
        int i, n, j;

        public void getData()
        {
            Console.WriteLine("Program for displaying pattern of *.");
            Console.Write("Enter the maximum number of *: ");
            int n = Convert.ToInt32(Console.ReadLine());
        }

        public void pattern()
        {
            Console.WriteLine("\nPattern 1 - Left Aligned:\n");
            for (i = 1; i <= n; i++)  // The Control does not enter the for loop
            {
                for (j = 1; j <= i; j++)
                    Console.Write("*");
                Console.WriteLine();
            }
        }
    }   

}
Run Code Online (Sandbox Code Playgroud)

Erg*_*wun 5

看起来你要重新声明n为本地变量getData().

尝试将该行更改为:

 n = Convert.ToInt32(Console.ReadLine());
Run Code Online (Sandbox Code Playgroud)

即删除int.