请大佬指点,for循环有关

xuerueheng 2019-07-11 02:27:04
先上代码

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

namespace Lesson_10_homework九九乘法表
{
class Program
{
static void Main(string[] args)
{
int a1;
int b2;
for(int a = 1; a <= 9; a++)
{
a1 = a * 1;//在输出的时候好输出答案
Console.WriteLine("1*{0}={1}",a,a1);//将计算过的变量用字符串输出成算式
for (int b = 1; b <= a;b++)
{
b2 = b * a1;
Console.Write("{0}*{1}={2}\t",b,a1,b2);
}
}

Console.ReadKey();
}
}
}
还有截图
我这个怎莫回事啊,老是有个一乘几
...全文
92 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你这句导致每次都打印1*X=X;Console.WriteLine("1*{0}={1}",a,a1);
xuerueheng 2019-07-11
  • 打赏
  • 举报
回复
引用 8 楼 XBodhi. 的回复:
你的问题就出在了 Console.WriteLine("1*{0}={1}",a,a1);//将计算过的变量用字符串输出成算式 这个。你不加他正好。

我貌似懂了,谢大佬
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson_10_homework九九乘法表
{
class Program
{
static void Main(string[] args)
{
int a1;
int b2;
for(int a = 1; a <= 9; a++)
{
a1 = a * 1;//在输出的时候好输出答案
Console.WriteLine();
//Console.WriteLine("1*{0}={1}",a,a1);//将计算过的变量用字符串输出成算式
for (int b = 1; b <= a;b++)//这个b在第一次运行时也应该是被赋值了2,但在第二次运行这个for时b应该被重新复制了1
{
b2 = b * a1;
Console.Write("{0}*{1}={2}\t",b,a1,b2);
}
}
for (int a = 1; a <= 3; a++)//在这个for循环结束时a应该被赋值了4
{
Console.WriteLine(a);
}
for (int a = 1; a <= 9; a++)
{
Console.WriteLine();
for (int b = 1; b <= 9;b++ )
{
Console.Write("{0}*{1}={2}\t",b,a,b*a);
}
}
Console.WriteLine();
for (int a = 1; a <= 9; a++)
{
Console.WriteLine();
for (int b = 1; b <= a; b++)
{
Console.Write("{0}*{1}={2}\t", b, a, b * a);
}
}

Console.ReadKey();
}
}
}
XBodhi. 2019-07-11
  • 打赏
  • 举报
回复
你的问题就出在了 Console.WriteLine("1*{0}={1}",a,a1);//将计算过的变量用字符串输出成算式 这个。你不加他正好。
XBodhi. 2019-07-11
  • 打赏
  • 举报
回复
首先这其实是一个 9*9 的矩阵,只是他 取了 对角线的一册,你可以试试 弄个方向的。再不考虑执次数的复杂度时。


先分析 99 表的 特性,他的乘数和被乘数 正好时 2维的行和列。所以逐级递增的时候因为缺少对角线的另一侧,结果行的索引就成了列的最后循环次数。
xuerueheng 2019-07-11
  • 打赏
  • 举报
回复
引用 1 楼 XBodhi. 的回复:
99 表这个 是入门的 for。稍等给你写个

谢大佬,另外大佬我刚才改了一下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson_10_homework九九乘法表
{
class Program
{
static void Main(string[] args)
{
int a1;
int b2;
for(int a = 1; a <= 9; a++)
{
a1 = a * 1;//在输出的时候好输出答案
Console.WriteLine();
//Console.WriteLine("1*{0}={1}",a,a1);//将计算过的变量用字符串输出成算式
for (int b = 1; b <= a;b++)
{
b2 = b * a1;
Console.Write("{0}*{1}={2}\t",b,a1,b2);
}
}

Console.ReadKey();
}
}
}
(抱歉,不知道在回复里怎莫贴代码)然后它就就输出这样子了,我没搞懂,大佬解释一下呗
ObjectFrm 2019-07-11
  • 打赏
  • 举报
回复
把b改成=2开始
XBodhi. 2019-07-11
  • 打赏
  • 举报
回复


/*
打印 99 表
*/

for (int i = 1; i <=9; i++)
{
for (int j = 1; j <=i; j++)
{
Console.Write($"{j}*{i}={i * j} ");
}
Console.WriteLine();
}


  • 打赏
  • 举报
回复
Console.WriteLine("1*{0}={1}", a, a1);
改成
Console.WriteLine("");
csdnFUCKINGSUCKS 2019-07-11
  • 打赏
  • 举报
回复
for (int a = 1; a <= 9; a++)
{
//a1 = a * 1;//在输出的时候好输出答案
//Console.WriteLine("1*{0}={1}", a, a1);//将计算过的变量用字符串输出成算式
for (int b = 1; b <= a; b++)
{
int c = a * b;
Console.Write("{0}*{1}={2}\t", a, b, c);
}
Console.WriteLine("\r\n");
}
XBodhi. 2019-07-11
  • 打赏
  • 举报
回复
99 表这个 是入门的 for。稍等给你写个

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧