我的循环不会循环

时间:2017-02-20 01:18:13

标签: c++ loops while-loop

我有一项任务,我们必须编写代码,找到一个符合四个参数的数字。我的想法是将该数字拆分为各自的数字,然后使用if语句检查数字是否适合参数。如果它不希望它增加1并循环直到它找到数字。我的循环只运行一次,并且甚至不返回所有四位数。任何关于我应该做的事情都会受到赞赏。

#include <iostream>
using namespace std;
int digit_breakdown(int& number);
int main()
{

    int number = 1000; //This is the variable I will change to find the         secret number
    int last_dig, third_dig, sec_dig, first_dig; // These are variables   that represent each of the four digits
    int secret_number = 0; //This variable holds the secret number

    do{
        number++;
        last_dig = number%10; //This series of code breaks up the number into     digits
        number /= 10;
        third_dig = number%10;
        number /= 10;
        sec_dig = number%10;
        number /= 10;
        first_dig = number%10;
        number /=10;

        if((first_dig != sec_dig) && (first_dig != third_dig) && (first_dig    != last_dig) && (sec_dig != third_dig) && (sec_dig != last$
            number = secret_number;
    }while((secret_number = 0));

    cout << "You found the secret number! That number is " << secret_number;
    cout << last_dig << endl;
    cout << third_dig << endl;
    cout << sec_dig << endl;
    cout << first_dig << endl;

}

2 个答案:

答案 0 :(得分:1)

更改

while((secret_number = 0));

while(secret_number == 0);

=是一个赋值运算符,为了进行比较,您需要==

在您$ if-statement while loop内的一个条件中,您可能还需要var image1 = UIImage(named: "image1.png") var imageBase = yourImageView.image var size = yourImageView.frame.size UIGraphicsBeginImageContext(size) let areaSize = CGRect(x: 0, y: size.width - ( size.width / 4 ), width: (size.width / 4), height:( size.height / 4) ) image1!.drawInRect(areaSize) imageBase!.drawInRect(areaSize, blendMode: CGBlendMode.Normal, alpha: 0.8) var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() 签名。

答案 1 :(得分:0)

根据给出的问题陈述,我改变了逻辑和一些陈述。即使您按照建议更改了while循环条件,也无法获得准确的输出。

所以改变是。

A)在循环开始时分配的secret_number并保持递增的数字

B)对secret_number变量进行修改操作

C)一旦满足条件就使用“休息”。(因为你得到了你的秘密号码)

E)因为你的情况是无止境的,直到你发现所需的数字改变了循环为while(1)[这是可选的,你可以将它设置为某个上限]

F)显示数字本身。

以下是代码详情。

#include <iostream>
using namespace std;
int digit_breakdown(int& number);
int main()
{

    int number = 1000; //This is the variable I will change to find the         secret number
    int last_dig, third_dig, sec_dig, first_dig; // These are variables   that represent each of the four digits
    int secret_number = 0; //This variable holds the secret number

    do{
        secret_number = ++number;
        last_dig = secret_number%10; //This series of code breaks up the number into     digits
        secret_number /= 10;
        third_dig = secret_number%10;
        secret_number /= 10;
        sec_dig = secret_number%10;
        secret_number /= 10;
        first_dig = secret_number%10;
        secret_number /=10;

        if((first_dig != sec_dig) && (first_dig != third_dig) && (first_dig    != last_dig) && (sec_dig != third_dig) && (sec_dig != last_dig)){
            break;
        }
    }while(1);  //To avoid endless loop you can set some upper_limit

    cout << "You found the secret number! That number is " << number<<endl;
    cout << last_dig << endl;
    cout << third_dig << endl;
    cout << sec_dig << endl;
    cout << first_dig << endl;

    return 0;
}

输出如下:

/WorkDir/checks/stackover> vi loop_digit.cpp
/WorkDir/checks/stackover> g++ -Wall -Wextra -O2 -o loop_digit loop_digit.cpp 
/WorkDir/checks/stackover> ./loop_digit 
You found the secret number! That number is 1022
2
2
0
1