本文介绍了查找数字是否可被3或5整除(FizzBu​​zz)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据输出是否被3或5整除来更改输出?如果它可以被3整除,我想显示"rock",如果可以被5整除,我想显示"star"(类似于FizzBu​​zz).如果两者都存在,他们将看到两者.

How do I change the output depending on whether or not it is divisible by 3 or 5? If it is divisible by 3, I want to show "rock" and if it's divisible by 5 I want to show "star" (similar to in FizzBuzz). If both, they'll see both.

这是我的代码:

if (var n = Math.floor((Math.random() * 1000) + 1); {
  var output = "";
  if (n % 3 == 0)
    output += "Rock";
  if (n % 5 == 0)
    output += "star";
  prompt(output || n);
}

为什么我的代码不能正常工作?

Why isn't my code working properly?

推荐答案

var n = Math.floor((Math.random() * 1000) + 1);
if (n) {
  var output = "";
  if (n % 3 == 0)
    output += "Rock";
  if (n % 5 == 0)
    output += "star";
  prompt(output || n);
}

if语句内的var是语法错误.我的浏览器显示此错误:

The var inside the if statement is a syntax error. My browser shows this error:

SyntaxError: expected expression, got keyword 'var'

所以我认为您应该在告诉if语句var n是您的比较表达式之前声明变量n.

So I think you should declare variable n before telling the if statement that var n is your comparison expression.

这篇关于查找数字是否可被3或5整除(FizzBu​​zz)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:50