我有两个函数,makeKey()和keyExists().
makeKey()只生成一个5位随机字母数字键,keyExists()接受此键作为其唯一参数,并在表中查找,根据它是否存在返回true/false.
我需要做一些非常简单的事情,但我无法找出最快捷的方法.
我只需要创建一个密钥,如果它存在于表中,则再次创建一个密钥,依此类推,直到返回一个唯一的密钥.我认为一个while循环就足够了?
谢谢,请原谅相当基本的问题,我想我昨天在阳光下煮了我的大脑.
是否建议使用while循环,它永远不会在构造函数中结束?或者我应该使用线程来获得相同的结果?构造函数永远不会终止时好吗?或者避免分段错误更安全吗?
希望你能理解我糟糕的英语..
我尝试并试图让这个代码工作,并继续提出zilch.所以我决定尝试使用"for loops"而不是它首先尝试.有人能告诉我为什么这段代码不好?
<?php
$x = $y = 10;
while ($x < 100) {
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
?>
Run Code Online (Sandbox Code Playgroud) 我一直很困惑为什么这不会工作!我的查询在所有方面都执行得非常好,但是当我在$ row值上添加if检查时,echo消息按预期进行(如果没有结果),但是当我的查询中存在匹配时,else将不会启动??? 为什么这不起作用?有人请缓解我的痛苦,让我直截了当!
$row = mysql_fetch_array($result);
if (!$row) {
echo "Sorry brah. Nothing matches your search criteria.";
} else {
$i = 1;
while($row = mysql_fetch_array($result)) {
echo "$i - " . $row['first_name'] . " " . $row['last_name'] . " - " . $row['address'] . "<br />";
$i++;
}
}
Run Code Online (Sandbox Code Playgroud) 我最近创建了一个PHP/MySQL while循环,它根据我的参数获取数组.根据我所知道的,当没有更多行可以抓取时,它应该结束.我只是继续前进,直到我停止加载页面.
while($row = mysql_fetch_array(mysql_query("SELECT username,password,title,message FROM notifications WHERE dateset = '$date'"))){
$test = new Boxcar($row['username'],$row['password']);
$test->send($row['title'],$row['message']);
}
Run Code Online (Sandbox Code Playgroud) 我正在学习bash,但我无法理解这个示例代码:
while IFS= read -r line2;
do
if [[ "$line1" == "$line2" ]]
then
(( i++ ))
fi
done <<< "$lines"
Run Code Online (Sandbox Code Playgroud)
特别是行done <<< "$lines"什么意思<<<?
我找到了其他例子,例如:
while read line
do
echo "Word count per line: $line"
done < <(cat list.txt | wc -w)
Run Code Online (Sandbox Code Playgroud)
在这种情况下因为它使用< <而不是<<<
static char characters[] = { 'a', 'b', ' ', 'c', ' ', 'e', 'f', ' ' };
public static void main(String args[]) {
for (int i = 0; i < characters.length; i++) {
int j = i + 1;
if (characters[i] == ' ') {
while (j < characters.length && characters[j] == ' ' || characters[j] == '\u0000') {
j++; // increment j till a non-space char is found
}
}
}
for (char character : characters) {
System.out.print(character + …Run Code Online (Sandbox Code Playgroud) 我必须编写一个程序,从用户那里获取一个奇数,并创建一个魔术方块.幻方是每行,列和对角线的总和相同的方格.这些是编写代码的特征:
- 询问用户奇数
- 创建一个n×n数组.
- 按照以下步骤创建一个魔术方块.
一个.在第一行的中间放置一个1.
湾 从行中减去1并将1添加到列中.
一世.如果可能,将下一个号码放在该位置.
II.如果不可能,请按照下列步骤操作.
- 如果在行-1中,则更改为最后一行
- 如果在最后一列中更改为第一列
- 如果被阻止,则下拉到下一行(从原始位置)
- 如果在右上角,则下拉到下一行.
- 打印阵列
我已经编写了代码,但是当我运行它时,程序会输入所有数字,除了第二个; 由于某种原因,我的程序跳过了它.例如,如果我输入数字3作为奇数,我的输出是:
6 1 0
3 4 5
9 7 8
Run Code Online (Sandbox Code Playgroud)
0不应该在那里,但第二个是.这是我的代码:
public static void main(String[] args) {
System.out.print("Give an odd number: ");
int n = console.nextInt();
int[][] magicSquare = new int[n][n];
int number = 1;
int row = 0;
int column = n / 2;
while (number <= n * n) {
magicSquare[row][column] = number;
number++;
row -= 1;
column += 1;
if (row == …Run Code Online (Sandbox Code Playgroud) 我很惊讶以下代码有效:
while(fred !== "stop"){
var fred = prompt("Should I stop or go?")
};
Run Code Online (Sandbox Code Playgroud)
我在do while循环中理解这一点:
do {
code to be executed at least once
?}
while (condition);
Run Code Online (Sandbox Code Playgroud)
在函数声明fred为变量之前,JavaScript如何设置条件?
其他问题我读过涉及到VAR声明中的条件.
需要做什么的示例:

"输入了几个数字(输入再次以0结尾).确定并打印第5个,第10个,第15个数字的总和等."
有人可以告诉我我的代码有什么问题吗?
class Program
{
static void Main(string[] args)
{
int counter = 0;
int sum = 0;
Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());
while (number != 0)
{
if (number > 0 && counter % 5 == 0)
{
counter++;
sum = sum + number;
}
Console.Write("Enter a number: ");
number = int.Parse(Console.ReadLine());
}
Console.WriteLine("Sum of the 5th, 10th, 15th ... number is = {0}", sum);
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud) while-loop ×10
php ×4
java ×2
mysql ×2
arrays ×1
bash ×1
c# ×1
c++ ×1
if-statement ×1
iteration ×1
javascript ×1
loops ×1
magic-square ×1
nested ×1
oop ×1
var ×1