自己尝试了一下n1到n2之间的所有偶数:
int n1 = Convert.ToInt32(Console.ReadLine());
int n2 = Convert.ToInt32(Console.ReadLine());
while (n1 < n2+1) {
if (n1 % 2 == 0) { Console.WriteLine(n1); }
n1++;
}
自己尝试了一下n1到n2之间的所有偶数:
int n1 = Convert.ToInt32(Console.ReadLine());
int n2 = Convert.ToInt32(Console.ReadLine());
while (n1 < n2+1) {
if (n1 % 2 == 0) { Console.WriteLine(n1); }
n1++;
}
using System;
namespace _018_循环语句_while循环
{
class Program
{
static void Main(string[] args)
{
//int n = Convert.ToInt32(Console.ReadLine());
//int i = 1;
//int sum = 0;
//while (i < n + 1)
//{
// sum += i;
// i++;
//}
//Console.WriteLine(sum);
//int i = 1;
//while (i < 101)
//{
// // 1-100
// if (i % 2 == 0)
// {
// Console.WriteLine(i);
// }
// i++;
//}
// 10 15
//int i = 10;
//while (i < 16)
//{
// Console.WriteLine(i);
// i++;
//}
int n1 = Convert.ToInt32(Console.ReadLine());
int n2 = Convert.ToInt32(Console.ReadLine());
int i = n1;
while (i < n2 + 1)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
i++;
}
}
}
}
和老师 不一样的方法
//❓输⼊两个整数n1,n2,输出n1 - n2(包含n1和n2)之间所有的偶数。
int n1 = Convert.ToInt32(Console.ReadLine());
int n2 = Convert.ToInt32(Console.ReadLine());
if (n1 > n2)
{
int n3 = n1;
n1 = n2;
n2 = n3;
}
int a = n1;
if (a % 2 == 0)
{
Console.WriteLine(a);
while (a >= n1 && a <= n2 - 2)
{
a += 2;
Console.WriteLine(a);
}
}
if (a % 2 != 0)
{
a += 1;
Console.WriteLine(a);
while (a >= n1 && a <= n2 - 2)
{
a += 2;
Console.WriteLine(a);
}
}
int.parse
static void test11()
{
Console.WriteLine("请输入整数n1:");
int n1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入整数n2:");
int n2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("你输入的n1是:{0},你输入的n2是:{1}",n1,n2);
int i = n1;
if (n2>n1)
{
while (i<=n2)
{
if (i%2==0)
{
Console.WriteLine(i);
}
i++;
}
}
else
{
while (i >= n2 )
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
i--;
}
}
}
12345678900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
int i=1;
while (i<101)
{
if( i%2==0){
Console.WriteLine(i);
}
i++;
}
int n1;
int n2;
while (n1<n2)
{
if(n1%2==0){
Console.WriteLine(n1)
}
n1+1;
}
可以在while语句中插入if语句
第五十二课 控制while循环的开头和结尾
1. 输入两个整数n1,n2,输出n1-n2(包含n1和n2)之间所有的偶数。
int n1 = Convert.ToInt32(Console.ReadLine());
int n2 = Convert.ToInt32(Console.ReadLine());
if (n1 > n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
Console.WriteLine("{0}、{1}", n1, n2);
}
while (n1 < n2 + 1)
{
if (n1 % 2 == 0)
Console.Write(n1+" ");
n1++;
}
学这玩应掉头发!
2. 3n+1问题:
对于任意大于1的自然数n,若n为奇数,将n变成3n+1,否则变成n的一半,经过若干次这样的变化,n最终一定会变成1,。比如:7→22→11→34→17→52→26→13→40→20→10→5→16→8→4→2→1
输入n,输出变换的次数。
比如输入3,输出7;输入10,输出6。
int n =Convert.ToInt32(Console.ReadLine());
int a = 0;
while (n > 1)
{
if(n % 2 == 1)
{
n = 3 * n + 1;
a++;
}
else
{
n = n / 2;
a++;
}
}
Console.Write("最后值为{0}" + " " + "共用{1}步", n, a);
这道题还蛮好玩的,睡觉去了,睡晚了更掉头发,明天再听老师揭秘。
%是求余的意思
/是相除的意思