我在许多书籍的帮助下学习JavaScript.在其中一个中,我想出了一个显示几个标志的脚本,然后点击它们随机更改它们:
var myImages = new Array("usa.gif","canada.gif","jamaica.gif","mexico.gif");
function changeImg(that) {
var newImgNumber = Math.round(Math.random() * 3);
while (that.src.indexOf(myImages[newImgNumber]) != -1)
{
newImgNumber = Math.round(Math.random() * 3);
}
that.src = myImages[newImgNumber];
return false;
}
Run Code Online (Sandbox Code Playgroud)
它有一个while循环,以确保当你点击一个标志时,你不会得到同一个标志.HTML中的其余代码如下所示:
<body>
<img name="img0" src="usa.gif" border="0" onclick="return changeImg(this)" />
<img name="img1" src="mexico.gif" border="0" onclick="return changeImg(this)" />
</body>
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是,如果我使用IF语句而不是while循环,显然代码的工作原理相同.在这个特定的例子中,为什么使用while循环而不是IF?谢谢.
对不起那么难以捉摸的标题,但这是我将用来解释我的问题的代码片段!
while(motors[0].cycle <= maxcycle
&& motors[1].cycle <= maxcycle
&& motors[2].cycle <= maxcycle
&& motors[3].cycle <= maxcycle
&& motors[4], etc ...)
Run Code Online (Sandbox Code Playgroud)
我如何避免为while()循环键入这个非常长的条件,因为我总是检查相同的参数,只有我的结构的索引正在改变.
当用户输入-1(就像它应该)但我的代码停止但是打印的平均值是错误的.
这就是我所拥有的:
import static java.lang.System.*;
import java.util.Scanner;
import java.util.*;
class averages_1
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the scores:");
double score = 0;
double num = 0;
double sum = 0;
while (score != -1)
{
score = scan.nextDouble();
sum += score;
num++;
}
System.out.println("The average is:" + (sum/num));
}
}
Run Code Online (Sandbox Code Playgroud)
如果输入50然后输入105然后输出-1,则输出为
Enter the scores:
50
105
-1
The average is: 51.333333333333336
Run Code Online (Sandbox Code Playgroud)
我只需要纠正平均值.任何帮助表示赞赏!
我在c中创建一个逐行读取的程序,并对每行的元素进行一些操作.我正在调用我的read_row函数.
while(filePointer!=NULL)
{
int result=readRow(filePointer,headerRow,lineRow);
printf("The value of row is |%d|\n",result);
}
Run Code Online (Sandbox Code Playgroud)
在我的read_row函数中,我使用get c遍历该行.
cc=getc(fp);
while((cc!='\n') && (cc!= EOF))
{
*line=cc;
line++;
if(cc==',')
{
counterr++;
}
cc=getc(fp);
}
if(cc==EOF)
{
fp=NULL;
}
Run Code Online (Sandbox Code Playgroud)
我正试图遍历所有的路线.但是,这个循环是无限的,因为文件指针永远不会变为null.如果cc等于EOF,我试图将文件指针设置为null.但是,这根本没有帮助.因此,请解释我应该如何正确终止该循环.我相信'\n'在EOF之前出现.因此,文件指针不会变为null.我必须使用Pointer文件来完成它,因为我已经基于该逻辑创建了一个非常大的程序.而且,我只能找到使用fgets()等方法遍历的例子.
我正在创建一个计数器,我很难做到.计数器的目标是,永远第二次通过一个数字将增加170.正如你在下面看到的数字不加起来,并在一个新的线上,主要是因为我不知道如何使它加起来.有点像"经济学人"的这个时钟
<!DOCTYPE html>
<html>
<body>
<p>Click the button see how much AirHelps market increases by every second.</p>
<button onclick="counterFunction()">See it now</button>
<p id="counter"></p>
<script>
function counterFunction() {
setTimeout(function () {
var text = "";
var i = 170;
while (i < 3500) {
text += "<br>The number is " + i;
i+=170;
}, 1000) }
document.getElementById("counter").innerHTML = text;
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
关于如何制作这个以及我的代码有什么问题的任何想法?
我试图通过使用while循环每2秒拍一张照片.但是当我尝试这个时,屏幕会冻结.
这是拍摄照片的功能:
func didPressTakePhoto(){
if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){
videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
(sampleBuffer, error) in
if sampleBuffer != nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProviderCreateWithCFData(imageData)
let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, .RenderingIntentDefault)
let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
//Adds every image taken to an array each time the while loop loops which will then be used to create a timelapse.
self.images.append(image)
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
为了拍照,我有一个按钮,当一个名为count的变量等于0时,将在while循环中使用该函数,但是当按下结束按钮时,该变量等于1,因此while循环结束.
这就是startPictureButton动作的样子: …
int time=0;
int x=1;
int w=2;
int y=1;
int h=1;
int t=6;
while (x != w && y != h) {
boolean s = Abc (a,b,c,time);
if (s == true) {
time = time+t;
x++;
}
else if (s = false) {
time = time++;
}
}
System.out.println(time);
Run Code Online (Sandbox Code Playgroud)
这是我正在为学校写的程序的一部分,我的问题是,当程序进入while循环时,无论我在循环内部做什么,当它退出并打印变量"time"时,该值与值相同设置在开头.我错过了什么吗?(BTW Abc只是一种方法,当我第一次进入循环时应该返回值true,因此时间值应改为time + t)
我正在尝试用Java编写一个小程序,让用户输入四个字符串(用户名,电子邮件,密码,密码).
该程序应该检查这些字符串是否满足while循环中的某些条件,这要求用户输入上述字符串,直到它们满足这些条件.
该程序编译,前两个if案例似乎工作正常.只有两个密码(password和passcheck)之间的比较似乎是错误的.即使我两次输入相同的字符串,它们也没有通过比较,我不知道为什么会这样.
import java.util.Scanner;
public class Registry {
public static void main (String [] args){
Scanner scanner = new Scanner(System.in);
// Dialogue for the user to fill
System.out.println("Please enter your username (at least 5 characters)");
String username = scanner.nextLine();
System.out.println("Please enter a valid emailadress (an @ is necessary for it to pass.)");
String email = scanner.nextLine();
System.out.println("Please enter two matching passwords, so we can make sure that you entered them correctly."); …Run Code Online (Sandbox Code Playgroud) 我有一个简单的bash脚本:
#!/bin/bash
counter=0
while [ $counter < 100 ]; do
echo $counter
counter=$((counter+1))
done
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试执行此操作时,我收到此错误:
./test: line 4: 100: No such file or directory
Run Code Online (Sandbox Code Playgroud) 我需要一个带延迟的循环(如计时器)但是它的结尾有问题,这是我的代码:
while(true) {
if (someValue == 10) {
break;
}
//Wait two seconds. <-----
}
System.out.println("While Ended.");
Run Code Online (Sandbox Code Playgroud)
这很好,但需要每2秒重复一次.我尝试过Timer但是"虽然结束了".消息显示在计时器结束之前.我怎么解决这个问题?
while-loop ×10
java ×4
loops ×3
c ×2
if-statement ×2
javascript ×2
arduino ×1
bash ×1
file ×1
freeze ×1
pointers ×1
shell ×1
swift ×1
time ×1
timelapse ×1
timer ×1
user-input ×1