如何正确输出BishBashBosh?

huangapple go评论114阅读模式
英文:

How do I get BishBashBosh outputted correctly?

问题

I see that you're trying to build a program in C# that outputs different words based on certain conditions for numbers. It seems like you're facing some issues with your code. Here's the translated code you provided:

我正在尝试构建一个程序,对可被3整除的数字输出“Bish”,对可被5整除的数字输出“Bash”,对可同时被35整除的数字输出“BishBash”,以及对其各位数字的和为奇数的数字输出“Bosh”。

我面临的问题是我不知道如何输出“BishBashBosh”以执行上述步骤。

这是它应该执行的示例:
- 30 / 3 = 10,所以30可被3整除:Bish
- 30 / 5 = 6,所以30可被5整除:Bash
- 30包含数字303 + 0 = 3,是奇数:BOSH

总之,数字30应该在控制台输出BishBashBosh,其他情况如上所述。

这是我到目前为止编写的代码:

int Bish = 0;
int Bash = 0;
int BishBash = 0;
int BOSH = 0;
int numbers = 0;

for (int i = 1; i <= 15; i++)
{
    int sumOfDigits = 0;
    int temp = i;
    while (temp > 0)
    {
        sumOfDigits += temp % 10;
        temp != 10;
    }
    if (i % 3 == 0 && i % 5 == 0)   // 可同时被3和5整除
    {
        Console.WriteLine("BishBash");
        BOSH++;  // 增加“BOSH”的计数
        BishBash++;
    }
    else if (i % 3 == 0)    // 可被3整除
    {
        Console.WriteLine("Bish");
        Bish++;
    }
    else if (i % 5 == 0)    // 可被5整除
    {
        Console.WriteLine("Bash");
        Bash++;
    }
    else if (sumOfDigits % 2 == 1)   // 各位数字之和为奇数
    {
        Console.WriteLine("BOSH");
        BOSH++;
    }
    else   // 其他数字
    {
        Console.WriteLine(i);
    }
}

Console.WriteLine($"{Bish}, {Bash}, {BishBash}, {BOSH}");   // 打印每个输出单词的计数

你还提供了另一个实现的示例代码,但它似乎有一些问题。你希望输出数字并且只对可被3、5整除以及各位数字之和为奇数的数字进行特定输出。然而,你可能需要检查和修复代码中的错误以获得期望的结果。

英文:

I am trying to build a program that outputs Bish for any number that is divisible by 3, Bash for numbers divisible by 5 and BishBash for numbers divisible by both 3 and 5, and Bosh for each digit of the number adds up to an odd number.

The problem I'm facing is that I am struggling to figure out how to output BishBashBosh to do the above steps.

Here is an example of what it should do:

  • 30 / 3 = 10 so 30 is divisible by 3: Bish
  • 30 / 5 = 6 so 30 is divisible by 5: Bash
  • 30 contains the digits 3 and 0. 3 + 0 = 3, which is odd: BOSH

In summary, the number 30 should output BishBashBosh in the console with the others as stated above.

Here is what I coded so far:

int Bish = 0;
int Bash = 0;
int BishBash = 0;
int BOSH = 0;
int numbers = 0;
for (int i = 1; i <= 15; i++)
{
int sumOfDigits = 0;
int temp = i;
while (temp > 0)
{
sumOfDigits += temp % 10;
temp != 10;
}
if (i % 3 == 0 && i % 5 == 0)   // divisible by 3 and 5
{
Console.WriteLine("BishBash");
BOSH++;  // increase counter for "BOSH" output
BishBash++;
}
else if (i % 3 == 0)    // divisible by 3
{
Console.WriteLine("Bish");
Bish++;
}
else if (i % 5 == 0)    // divisible by 5
{
Console.WriteLine("Bash");
Bash++;
}
else if (sumOfDigits % 2 == 1)   // digits add up to an odd number
{
Console.WriteLine("BOSH");
BOSH++;
}
else   // other number
{
Console.WriteLine(i);
}
}
Console.WriteLine($"{Bish}, {Bash}, {BishBash}, {BOSH}");   // print count for each output word

I experimented with the program in another project to try to fix the problem.

Here is a sample code of this implementation:

int n = 30;
int numbers = 0;
string output = "";
for (int i = 30; i <= 300; i++)
{
if (n % 3 == 0)
{
output += "Bish";
}
Console.WriteLine(output);
if (n % 5 == 0)
{
output += "Bash";
}
int sumOfDigits = 0;
while (n > 0)
{
sumOfDigits += n % 10;
n /= 10;
}
if (sumOfDigits % 2 == 1)
{
output += "BOSH";
}
else
{
Console.WriteLine(numbers);
}
}
Console.WriteLine(output);

The output in the console came up with the lots of lines of BishBashBosh which was most definitely not the output I was expecting as it should output the numbers as well and only output it for numbers that are divisible by 3, 5 and the numbers added up for it to be an odd number.
The unexpected output is shown below:
BishBashBOSHBishBashBishBash which is repeated until it the loop is broken. this is shown in the Watch window in the debugger

答案1

得分: 1

请注意,一些 value 值(如 30)可以同时是 BishBashBosh,这就是为什么你应该测试所有三种可能性,即 3 个分开的 if,而不是 if else。让我们为此提取方法Classify):

private static string Classify(int value) {
  int sumOfDigits = 0;

  for (int number = value; number > 0; number /= 10)  
    sumOfDigits += number % 10;

  String result = "";

  // 请注意,每个条件都有自己的 "if"
  if (value % 3 == 0)  
    result += "Bish";

  if (value % 5 == 0)  
    result += "Bash";

  if (sumOfDigits % 2 != 0) 
    result += "Bosh";

  return result;
}

然后你可以放置:

// 在这里放置所需的限制,而不是我的 1 和 30
for (int i = 1; i <= 30; ++i)
    Console.WriteLine($"{i,2} : {Classify(i)}");  

输出:

 1 : Bosh
 2 : 
 3 : BishBosh
 4 : 
 5 : BashBosh
 6 : Bish
 7 : Bosh
 8 : 
 9 : BishBosh
10 : BashBosh
11 : 
12 : BishBosh
13 : 
14 : Bosh
15 : BishBash
16 : Bosh

...

27 : BishBosh
28 : 
29 : Bosh
30 : BishBashBosh

Fiddle

英文:

Please, note that some values (liek 30) can be both Bish, Bash and Bash, that's why you should test for all three possibilities, i.e. 3 separted if, not if else. Let's extract method (Classify) for this:

private static string Classify(int value) {
int sumOfDigits = 0;
for (int number = value; number &gt; 0; number /= 10)  
sumOfDigits += number % 10;
String result = &quot;&quot;;
// Note, that each condition has its own &quot;if&quot;
if (value % 3 == 0)  
result += &quot;Bish&quot;;
if (value % 5 == 0)  
result += &quot;Bash&quot;;
if (sumOfDigits % 2 != 0) 
result += &quot;Bosh&quot;;
return result;
}

Then you can put

// Put required limits here instead of mine 1 and 30
for (int i = 1; i &lt;= 30; ++i)
Console.WriteLine($&quot;{i,2} : {Classify(i)}&quot;);  

Output

 1 : Bosh
2 : 
3 : BishBosh
4 : 
5 : BashBosh
6 : Bish
7 : Bosh
8 : 
9 : BishBosh
10 : BashBosh
11 : 
12 : BishBosh
13 : 
14 : Bosh
15 : BishBash
16 : Bosh
...
27 : BishBosh
28 : 
29 : Bosh
30 : BishBashBosh

Fiddle

答案2

得分: 1

这里的问题在于你的 output 位于 for 循环之外,并且在每次循环迭代时都会添加新的文本。控制台输出也位于循环之外,应该在循环内部(因为你想要为每个 i 打印结果)。
修复可能如下所示:

class Program {
    static void Main() {
        for (int i = 30; i <= 300; i++) {
            string output = "";
            if (i % 3 == 0) {
                output += "Bish";
            }
            if (i % 5 == 0) {
                output += "Bash";
            }

            if (SumOfDigits(i) % 2 == 1) {
                output += "Bosh";
            }

            Console.WriteLine($"{i}: {output}");
        }
    }

    static int SumOfDigits(int num) {
        int sumOfDigits = 0;
        while (num > 0) {
            sumOfDigits += num % 10;
            num /= 10;
        }

        return sumOfDigits;
    }
}

还要注意,你只将一个固定的数字 n 传递给了你的数字和求和函数 - 应该是 i。此外,最好将数字求和的逻辑放入一个单独的函数中。

然后结果将会是:

30: BishBashBosh
31:
32: Bosh
33: Bish
34: Bosh
35: Bash
36: BishBosh
37:
38: Bosh
39: Bish
40: Bash
41: Bosh
42: Bish
43: Bosh
44:
45: BishBashBosh
46:
47: Bosh
48: Bish
49: Bosh
50: BashBosh
51: Bish
52: Bosh
53:
54: BishBosh
55: Bash
...
英文:

The issue here is that your output is outside the for loop and gets added new and new text at each loop iteration. The console output is also outside the loop, and should be inside instead (since you want to print the result for each i).
A fix might look like this:

class Program {
static void Main() {
for (int i = 30; i &lt;= 300; i++) {
string output = &quot;&quot;;
if (i % 3 == 0) {
output += &quot;Bish&quot;;
}
if (i % 5 == 0) {
output += &quot;Bash&quot;;
}
if (SumOfDigits(i) % 2 == 1) {
output += &quot;Bosh&quot;;
}
Console.WriteLine($&quot;{i}: {output}&quot;);
}
}
static int SumOfDigits(int num) {
int sumOfDigits = 0;
while (num &gt; 0) {
sumOfDigits += num % 10;
num /= 10;
}
return sumOfDigits;
}
}

Also note that you only have a fixed number n passed to your digits' sum finding—it should be i instead. Plus, it's better to put the digits' sum finding logic into a separate function.

The result then will be:

30: BishBashBosh
31:
32: Bosh
33: Bish
34: Bosh
35: Bash
36: BishBosh
37:
38: Bosh
39: Bish
40: Bash
41: Bosh
42: Bish
43: Bosh
44:
45: BishBashBosh
46:
47: Bosh
48: Bish
49: Bosh
50: BashBosh
51: Bish
52: Bosh
53:
54: BishBosh
55: Bash
...

huangapple
  • 本文由 发表于 2023年8月4日 21:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定