简体   繁体   English

我在使用一些JavaScript做while循环时遇到麻烦

[英]I am having trouble with a few javascript do while loops

I am writing a code guessing program to test the security of some iPhone passwords (This is not in anyway connected to anything illegal or is used to break into actual iPhones). 我正在编写一个代码猜测程序,以测试某些iPhone密码的安全性(无论如何,这都不与任何非法行为有关,或者被用于破解实际的iPhone)。 The do/while loops just end all response once they occur and the last two lines don't print to the console. do / while循环一旦发生就会结束所有响应,并且最后两行不会打印到控制台。

print("Hi!");
print("I am a password security tester.");

var tries = 0

var guess1 = 0
var guess2 = 0
var guess3 = 0
var guess4 = 0

var guess = (guess1 * 1000) + (guess2 * 100) + (guess3 * 10) + guess4;

var password1 = (Math.floor(Math.random()*9));
var password2 = (Math.floor(Math.random()*9));
var password3 = (Math.floor(Math.random()*9));
var password4 = (Math.floor(Math.random()*9));

var password = (password1 * 1000) + (password2 * 100) + (password3 * 10) + password4;

print("This is the randomly genorated password: " + password);
print("And now begins the guessing");

do{
    guess1 + 1;
    tries + 1;
}while (password1 != guess1);

do{
    guess2 + 1;
    tries + 1;
}while (password2 != guess2);

do{
    guess3 + 1;
    tries + 1;
}while (password3 != guess3);

do{
    guess4 + 1;
    tries + 1;
}while (password4 != guess4);

print("Complete in " + tries + " tries");
print("The answer is: " + guess);

Jacob Ewing's answer is correct, but another problem is that guess will still be 0 at the end, because it doesn't automatically update. Jacob Ewing的答案是正确的,但是另一个问题是guess仍将为0,因为它不会自动更新。 You'll need to do: 您需要执行以下操作:

var guess = (guess1 * 1000) + (guess2 * 100) + (guess3 * 10) + guess4;

Before: 之前:

print("Complete in " + tries + " tries");
print("The answer is: " + guess);

You need to be using a += operator instead of just + 您需要使用+=运算符,而不仅仅是+

Saying guess1 + 1 returns the value of guess1 + 1 , but does not increment that value directly, so you get an infinite loop. 话说guess1 + 1的回报的价值guess1 + 1 ,但不直接增加该值,所以你得到一个无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM