如何使用输入创建for循环并显示循环所在的循环? [英] How do I make a for loop with an input and show which cycle the loop is on?

查看:100
本文介绍了如何使用输入创建for循环并显示循环所在的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是一个for循环,重复一个单词5次,在单词后面,显示单词所在的循环(1-5)



我尝试过:



使用System; 
{
class program
{
static void Main(string [] args)
{
int i =(Console.ReadLine());
for(int i = 0; i< 5; i ++);
{
Console.WriteLine(i);
}
}
}
}

解决方案

试试这个:

 static void Main(string [] args)
{
string inp = Console.ReadLine();
for(int i = 0; i< 5; i ++)
{
Console.WriteLine({0},{1},inp,i + 1);
}
}

ReadLine返回一个字符串,而不是整数,因此您需要正确存储它。存储字符串的变量不应与循环变量的名称相同,否则系统将不知道您的意思!

WriteLine可以将格式字符串作为其第一个参数它描述了要打印的内容。在这种情况下,它说打印第一个变量,一个逗号,然后是第二个变量。

因此循环的结果(假设你输入Hello)将是:

你好,1 
你好,2
你好,3
你好,4
你好,5


What I am trying to make is a for loop that repeats a word 5 times and after the word, shows which cycle the word is on (1-5)

What I have tried:

using System;
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = (Console.ReadLine());
            for (int i = 0; i < 5; i++) ;
            {
                Console.WriteLine(i);
            }
        }
    }
}

解决方案

Try this:

static void Main(string[] args)
{
    string inp = Console.ReadLine();
    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine("{0}, {1}", inp, i + 1);
    }
}

ReadLine returns a string, not an integer, so you need to store it correctly. The variable you store the string in, shouldn't be the same name as your loop variable, or the system won't know which one you mean!
WriteLine can take a format string as its first parameter which describes what to print. In this case it says "print the first variable, a comma, then the second variable".
So the result of the loop (assuming you entered "Hello") would be:

Hello, 1
Hello, 2
Hello, 3
Hello, 4
Hello, 5


这篇关于如何使用输入创建for循环并显示循环所在的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆