我如何在C#中执行此程序 [英] How do I do this program in C#

查看:68
本文介绍了我如何在C#中执行此程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序

输出仅由1到N的数字组成。但是每3的倍数被Waka取代, 5由Maka和7的每个倍数由Laka。

如果任何数字是数字3,5,7中的两个或三个的倍数,则该数字将被字典顺序中的相应代码(Laka,Maka,Waka)替换为空间。例如21被LakaWaka取代(中间没有空格)。



输出听起来像1 2 Laka 4 Maka Laka Waka 8 ....等等。



编写一个程序,在输出中显示n个单词。



输入格式:

输入是一个对应于N的整数值。

输出格式:

输出应打印输出。



请参阅样本输入和输出以了解格式规范。



样本输入

10

样本输出

1 2 Waka 4 Maka Waka Laka 8 Waka Maka


我尝试过:



 < span class =code-leadattribute>使用系统; 
使用 系统集合 .Generic;
使用 System .Linq;
使用 System .Text;
使用 系统线程 。任务;

命名空间 A
{
class 程序
{
static void Main(string [] args)
{
int n = int Parse(Console.ReadLine() );
for (int i = 1; i < = n; i ++)
{
if (i% 3 == 0)
{
if (n< 14)
Console .Write(Waka);
else
控制台 .Write(Laka);
}
if (i 5 == 0)
{
控制台 写(Maka);
}
if (i 7 == 0)
{
if (n< 14)
Console .Write(Laka);
else
控制台 .Write(Waka);
}
if (i 3 != 0 && i 5 != 0 && i 7 != 0)
{
控制台 写入(ⅰ)。
}

控制台 .Write();
}
控制台 .ReadKey();
}
}
}







我正在获得输出但未通过其他各种测试用例输入

解决方案

开始简单:只执行 3 - > Maka 替换:如果一个数字是(完全可被整除) 3 的倍数,那么打印'Maka',否则,打印数字本身。

提示:如果数字 n 完全可被 3 然后(n%3 == 0)持有。

然后添加简单'Waka''Laka'替换。

最终处理多个号码 3,5,7 因素。


您的实施中存在两个逻辑错误(相关并因此一起解决):



  1. 您使用单个 if 语句但应使用 else如果而不是为一个数字创建多个输出(例如15是3和5的倍数)

  2. 你没有处理这个案子如果任何数字是数字3,5,7中的两个或三个的倍数(即3 * 5 = 15,3 * 7 = 21,5 * 7 = 35,并且3 * 5 * 7 = 105)



所以你有四个额外的数字(和它们的倍数)来检查和替换并观察你执行检查的顺序[/编辑]。


blockquote>地狱,你在声明中看到14岁以后,Waka被替换为Laka的3倍数?

  if (n <   14 
Console.Write( Waka);
else
Console.Write( 拉卡);



建议:使用调试器查看程序执行情况,注意输出的构建方式。

显示不复杂了解输出错误的原因。
-----

有一个工具可以让你看到你的代码在做什么,它的名字是< b>调试的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

在Visual中调试C#代码工作室 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不是'找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Program
The output consisted only of numbers from 1 to N. But every multiple of 3 was replaced by Waka, every multiple of 5 by Maka and every multiple of 7 by Laka.
If any number was a multiple of two or three of the numbers 3,5,7, then the number would be replaced by the corresponding codes (Laka, Maka, Waka) in lexicographical order separated by a space. For example 21 was replaced by "LakaWaka"(no space inbetween).

The output sounded like 1 2 Laka 4 Maka Laka Waka 8....etc.

Write a program that would display n words in the output.

Input Format:
The input is an integer value that corresponds to N.
Output Format:
Output should print the output.

Refer sample input and output for formatting specifications.

Sample Input
10
Sample Output
1 2 Waka 4 Maka Waka Laka 8 Waka Maka

What I have tried:

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

namespace A
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            for (int i = 1; i <= n; i++)
            {
                if (i % 3 == 0)
                {
                    if (n < 14)
                        Console.Write("Waka");
                    else
                        Console.Write("Laka");
                }
                if (i % 5 == 0)
                {
                    Console.Write("Maka");
                }
                if (i % 7 == 0)
                {
                    if (n < 14)
                        Console.Write("Laka");
                    else
                        Console.Write("Waka");
                }
                if (i % 3 != 0 && i % 5 != 0 && i % 7 != 0)
                {
                    Console.Write(i);
                }

                Console.Write(" ");
            }
            Console.ReadKey();
        }
    }
}




I am getting the output but fails the testcase for various other inputs

解决方案

Start simple: perform just the 3 -> Maka substitution: if a number is multiple of (exactly divisible by) 3 then print 'Maka', otherwise, print the number itself.
hint: if a number n is exactly divisible by 3 then (n % 3 == 0) holds.
Then add the simple 'Waka' and 'Laka' substitutions.
Eventually take care of the numbers having multiple 3,5,7 factors.


There are two logic errors in your implementation (which are related and therefore solved together):


  1. You are using single if statements but should use else if instead to avoid creating multiple outputs for one number (e.g. 15 is a multiple of 3 and 5)
  2. You did not handle the case "If any number was a multiple of two or three of the numbers 3,5,7" (that is 3*5=15, 3*7=21, 5*7=35, and 3*5*7=105)


So you have four additional numbers (and multiples of them) to check and replace [EDIT] and to observe in which order you perform the checks[/EDIT].


Hell, where have you seen in statement that after 14, Waka is replaced bu Laka for multiples of 3 ?

if (n < 14)
    Console.Write("Waka");
else
    Console.Write("Laka");


Advice: Use debugger to see your program performing, pay attention to how the output is built.
Show not be complicated to understand the reason of output in wrong order.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我如何在C#中执行此程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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