C ++我的程序是无限循环的

时间:2017-02-11 15:28:53

标签: visual-c++

感谢您花时间阅读我的问题抱歉,我只是编程的初学者。显然我在循环中不是那么好,如果是else语句,我不知道我的代码中有什么不对。当我运行它并输入“up”时它就转到(8,10)。应该是(0,1)然后控制台会询问他们想要再往哪个方向。提前致谢!!! :d

#include<iostream>;
#include<string>;

using namespace std;

 int main()
{
    int x = 0;
    int y = 0;
    string input;

     cout << "objective: go to (8,9)" << endl;
     cout << "current location (" << x << "," << y << ")" << endl;

     while ( (x = 8) && (y = 9) )
{
    cout << "which way do you want to go?: ";
    cin >> input;

    if (input == "up")
    {
        ++y;
        cout << "current location (" << x << "," << y << ")" << endl;
        break;

    }
    else if (input == "down")
    {
        --y;
        cout << "current location (" << x << "," << y << ")" << endl;
        break;
    }
    else if (input == "right")
    {
        ++x;
        cout << "current location (" << x << "," << y << ")" << endl;
        break;
    }
    else if (input == "left")
    {
        --x;
        cout << "current location (" << x << "," << y << ")" << endl;
        break;
    }

}

cout << "current location (" << x << "," << y << ")" << endl;
cout << "Congratulations! You have reach your destination! :D" << endl;


system("pause");
return(0);

}

1 个答案:

答案 0 :(得分:0)

&#34;
我尝试删除了所有的休息,但它直接进入&#34;恭喜!你到达目的地&#34;当我试图运行它并将条件更改为(x == 8)&amp;&amp; (y == 9)仍然没有改善。 &#34;

您的循环条件不正确。 while循环只运行如果 x为8且y为9.它们在程序开头为0和0,这就解释了为什么我们跳过循环。实际情况是:&#34;只要x和y 这些值,就运行循环。&#34; ### Beispielsaufruf ### Wert für Wasserstand (zwischen 0 und 43) [Millionen m³] Wstand<-12 ### Wert für Trinkwasserbedarf (zwischen 0 und 200) [m³/s] Wbedar<-40 ### Wert für Energiebedarf (zwischen 0 und 100) [%] (-> normiert auf 1 MW = 100%) Ebedar<-45 newdata<- matrix(c(Wstand, Wbedar, Ebedar), nrow= 1, byrow = TRUE) res <- predict(fuzzy.model, newdata)$predicted.val Error in MF[k, temp[1]] : subscript out of bounds ,或者交替地:while (x != 8 || y != 9)