我正在尝试fizzbuzz程序:Why Can't Programmers.. Program?
“编写一个程序,打印从1到100的数字。但对于三个打印的倍数”Fizz“而不是数字和五个打印”Buzz“的倍数。对于三和五的倍数的数字打印“FizzBuzz”。“
protected void btn1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 101; i++)
{
if (i % 3 == 0 & i % 5 == 0)
{
Response.Write("fizzbuzz" + ",");
}
else if (i % 3 == 0)
{
Response.Write("fizz" + ",");
}
else if (i % 5 == 0)
{
Response.Write("buzz" + ",");
}
else
{
i = i + 0;
}
Response.Write(i +",");
}
}
我能够产生某种结果,如:
1,2,嘶嘶声,3,4-,嗡嗡声,5,嘶嘶声,6,7,8,嘶嘶声,9,嗡嗡声,10,11,嘶嘶声,12,13,14,fizzbuzz,15,16,17 ,嘶嘶声,18,19,嗡嗡声,20%,嘶嘶声,21,22,23,嘶嘶声,24,嗡嗡声,25,26,嘶嘶声,27,28,29,fizzbuzz,30,31,32,嘶嘶声,33,34 ,嗡嗡声,35,fizz,36,37,38,fizz,39等等。
打印了 fizz 这个词,但它没有替换3并且 fizzbuzz 被打印但是它没有替换15,所以......
答案 0 :(得分:7)
无论您是否符合if条件,您仍然会在代码末尾打印i
。
专门查看你的for循环:
for (int i = 1; i < 101; i++)
{
if (i % 3 == 0 & i % 5 == 0)
{
Response.Write("fizzbuzz" + ",");
}
else if (i % 3 == 0)
{
Response.Write("fizz" + ",");
}
else if (i % 5 == 0)
{
Response.Write("buzz" + ",");
}
else
{
i = i + 0;
}
Response.Write(i +","); //look here you print i
}
所以你需要移动最后一个Response.Write(i +“,”);在最后else
条件下。找到这些错误的最简单方法是使用调试器并调试程序。然后,您将轻松查看输出结果。所以肯定使用调试器并设置断点/手表并观察会发生什么。您的代码应更改为:
for (int i = 1; i < 101; i++)
{
if (i % 3 == 0 & i % 5 == 0)
{
Response.Write("fizzbuzz" + ",");
}
else if (i % 3 == 0)
{
Response.Write("fizz" + ",");
}
else if (i % 5 == 0)
{
Response.Write("buzz" + ",");
}
else
{
Response.Write(i +","); //look here you print i
}
}
请注意,删除i=i+1
您的for
循环已经通过递增i来处理此问题。
List<int> t;
t = Enumerable.Range(1, 100).ToList();
var fizzBuzz = t.Where(num => num % 3 == 0 && num % 5 == 0);
var fizz = t.Where(num => num % 3 == 0);
var buzz = t.Where(num => num % 5 == 0);
var notFizzBuzz = t.Where(num => num % 3 != 0 && num % 5 !=0);
//print fizzBuzz elements
Console.WriteLine("Printing fizzBuzz elements...");
foreach (int i in fizzBuzz)
Console.WriteLine(i);
//print fizz elements
Console.WriteLine("Printing fizz elements...");
foreach (int i in fizz)
Console.WriteLine(i);
//print buzz elements
Console.WriteLine("Printing buzz elements...");
foreach (int i in buzz)
Console.WriteLine(i);
//print other elements
Console.WriteLine("Printing all others...");
foreach (int i in notFizzBuzz)
Console.WriteLine(i);
答案 1 :(得分:1)
尝试这些更改
protected void btn1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 101; i++)
{
if (i % 3 == 0 & i % 5 == 0)
{
Response.Write("fizzbuzz" + ",");
}
else if (i % 3 == 0)
{
Response.Write("fizz" + ",");
}
else if (i % 5 == 0)
{
Response.Write("buzz" + ",");
}
else
{
Response.Write(i +",");
}
}
}
你的i = i + 0
显然什么也没做,因为你在i的值上加0。
你打算将数字打印到响应中,无论if / else块的结果是什么(它放在它之后),所以它应该被移动到else中(意味着只有if才打印,否则如果不匹配)
答案 2 :(得分:0)
将Response.Write(i +",");
移至您的最终其他
答案 3 :(得分:0)
protected void btn1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 101; i++)
{
if (i % 3 == 0 & i % 5 == 0)
{
Response.Write("fizzbuzz" + ",");
}
else if (i % 3 == 0)
{
Response.Write("fizz" + ",");
}
else if (i % 5 == 0)
{
Response.Write("buzz" + ",");
}
else
{
i = i + 0; //this is totally useless
Response.Write(i + ",");
}
//Response.Write(i +","); //This will always write the number, even if you wrote fizz or buzz
}
}
答案 4 :(得分:0)
另一个简单的实现:
for (int i = 1; i <= 100; i++)
{
Console.WriteLine((i % 3 == 0) ? ((i % 5 == 0) ? "FizzBuzz" : "Fizz") : ((i % 5 == 0) ? "Buzz" : i.ToString()));
}
Console.ReadKey();
答案 5 :(得分:0)
public static void PrintMod3And5FromInterval(int start, int end)
{
if (end < start)
{
Console.WriteLine("End number should be higher than start.");
}
else
{
string result = "";
for (int x = start; x <= end; x++)
{
if (x % 3 == 0)
result += "fizz";
if (x % 5 == 0)
result += "buzz";
if (result == "")
Console.WriteLine(x);
else
Console.WriteLine(result);
result = "";
}
}
}
static void Main(string[] args)
{
PrintMod3And5FromInterval(1, 100);
Console.Read();
}
答案 6 :(得分:0)
这是我最初的解决方法...
setCookieFunc
但是这个要短得多...
app.get('/api/addtocart', async (req, res) => {
try {
const productId = parseInt(req.query.productId);
const quantity = parseInt(req.query.quantity);
const sessionData = req.headers['session-data'];
const headers = {};
if (sessionData) headers.Cookie = sessionData;
const response = await axios.post(`${WP_API}/wc/v2/cart/add`, {
product_id: productId,
quantity
}, { headers });
if (!sessionData) {
const cookies = response.headers['set-cookie'];
const setCookieFunc = (cookie) => {
const [cookieKeyValue, ...cookieOptionsArr] = cookie.split('; ');
const cookieKey = cookieKeyValue.split('=')[0];
const cookieValue = decodeURIComponent(cookieKeyValue.split('=')[1]);
const cookieOptions = { };
cookieOptionsArr.forEach(option => (cookieOptions[option.split('=')[0]] = option.split('=')[1]));
if (cookieOptions.expires) {
const expires = new Date(cookieOptions.expires);
cookieOptions.expires = expires;
}
res.cookie(cookieKey, cookieValue, cookieOptions);
};
cookies.map(cookie => setCookieFunc(cookie));
}
return res.json(response.data);
} catch (error) {
// Handle error
return res.json(error);
}
});