C# 程序的控制不进入for循环

C# 程序的控制不进入for循环,c#,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.getD

我在这里粘贴代码:

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();
            }
        }
    }   

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间star1
{
班级计划
{
静态void Main(字符串[]参数)
{
Myclass obj=新的Myclass();
obj.getData();
对象模式();
}
}
类Myclass
{
int i,n,j;
public void getData()
{
Console.WriteLine(“用于显示*模式的程序”);
Console.Write(“输入*:”的最大数目);
int n=Convert.ToInt32(Console.ReadLine());
}
公共空间模式()
{
Console.WriteLine(“\n模式1-左对齐:\n”);

for(i=1;i看起来像是在
getData()
中将
n
重新定义为局部变量

尝试将该行更改为:

 n = Convert.ToInt32(Console.ReadLine());

i、 e.删除
int

看起来像是在
getData()
中将
n
重新定义为局部变量

尝试将该行更改为:

 n = Convert.ToInt32(Console.ReadLine());
i、 e.移除
int

由于您没有使用此

class Myclass
{
    int i, n, j;

    public void getData()
    {
        Console.WriteLine("Program for displaying pattern of *.");
        Console.Write("Enter the maximum number of *: ");
        this.n = Convert.ToInt32(Console.ReadLine());
        // OR
        //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();
        }
    }
}
class-Myclass
{
int i,n,j;
public void getData()
{
Console.WriteLine(“用于显示*模式的程序”);
Console.Write(“输入*:”的最大数目);
this.n=Convert.ToInt32(Console.ReadLine());
//或
//n=Convert.ToInt32(Console.ReadLine());
}
公共空间模式()
{
Console.WriteLine(“\n模式1-左对齐:\n”);
对于(i=1;i以及

由于您没有使用此

class Myclass
{
    int i, n, j;

    public void getData()
    {
        Console.WriteLine("Program for displaying pattern of *.");
        Console.Write("Enter the maximum number of *: ");
        this.n = Convert.ToInt32(Console.ReadLine());
        // OR
        //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();
        }
    }
}
class-Myclass
{
int i,n,j;
public void getData()
{
Console.WriteLine(“用于显示*模式的程序”);
Console.Write(“输入*:”的最大数目);
this.n=Convert.ToInt32(Console.ReadLine());
//或
//n=Convert.ToInt32(Console.ReadLine());
}
公共空间模式()
{
Console.WriteLine(“\n模式1-左对齐:\n”);


对于(i=1;i如果您尝试在getData()中使用Console.ReadLine()分配n,则需要在前面删除“int”。目前,在不同的作用域中有2个“n”变量。这就是循环无法工作的原因。

如果您尝试在getData()中使用Console.ReadLine()分配n,则需要在前面删除“int”。目前,您有2个“n”变量在不同的范围内。这就是循环不起作用的原因。

此外,这不是一个非常干净的代码(样式),您尝试了哪些值?单词“模式1-左对齐”吗出现在控制台窗口中,如果没有,则需要输入一些值并按enter键。为什么样式中有错误…@Tuhin:嗯…将流逻辑与ui特定命令混合使用:在内部调用
console
-方法不是一个好主意-这应该是
模式
-方法的一个参数…使用实例变量意味着不是multi-threading-safe——即使这不是您想要的:除非您正确地设计了类,否则不要这样做!基本上,您所做的被称为意大利面/意大利饺/千层面-code@Andreas:看,我是OOP新手,不太清楚你在说什么,所以你能给我一些代码示例吗……除此之外,这不是一段非常干净的代码(样式),您尝试了哪些值?单词“模式1-左对齐”吗出现在控制台窗口中,如果没有,则需要输入一些值并按enter键。为什么样式中有错误…@Tuhin:嗯…将流逻辑与ui特定命令混合使用:在内部调用
console
-方法不是一个好主意-这应该是
模式
-方法的一个参数…使用实例变量意味着不是multi-threading-safe——即使这不是您想要的:除非您正确地设计了类,否则不要这样做!基本上,您所做的被称为意大利面/意大利饺/千层面-code@Andreas:看,我是OOP新手,不太清楚你在说什么,所以你能给我一些代码示例吗?+1:在我看来,它实际上应该是在嵌套作用域内重新声明变量时出现编译错误或警告。@Joel:为什么会出现编译器错误?…不…实例变量将不会被赋值,并保留其默认值0…警告…是!@Tuhin:如果这个答案是您需要的,您应该接受它!@Andreas Niedermair:我只记得VS IDE wou我不记得这是警告还是错误,因为我目前正在避免业务需求审查,我不想浪费时间来验证。我想马上记住的人会评论:)@Joel:)…是的,你是对的:它也有下划线!+1:在我看来,它实际上应该抛出一个编译错误或警告,以便在嵌套范围内重新声明变量。@Joel:为什么是编译器错误?…不…实例变量将不被赋值,并保留其默认值0…一个警告…是的!@Tuhin:如果这个答案是你所需要的,你应该接受它!@Andreas Niedermair:我只记得VS IDE会将它标记为某种东西。我不记得这是警告还是错误,而且因为我目前正在避免业务需求审查,我不想浪费时间来验证。我想有人会立即记住它nt:)@Joel:)……是的,你是对的:它也会加下划线!