标签: while-loop

循环时不会退出

当我输入1,2或3时,它应该从循环中退出或不输入它.但是如果我写的不是那些3个数字,它应该循环直到我输入1,2或3.

但由于某种原因,我不能让它不进入循环或退出它.即使输入循环1,2或3是正确的数字也无关紧要.无论如何,它仍然会输入并循环.

string userValue = Console.ReadLine();
string message = "";

Console.WriteLine("Card 1, 2, or, 3?");
Console.ReadLine();


while (userValue != "1" || userValue != "2" || userValue != "3")
{
    message = "try again. Card 1, 2, or, 3?";
    Console.WriteLine(message);
    Console.ReadLine();
}

if (userValue == "1")
{
    message = "You win a Coke";
}
else if (userValue == "2")
{
    message = "You win a Diet Coke";
}
else if (userValue == "3")
{
    message = "You win a Apple Juice"; …
Run Code Online (Sandbox Code Playgroud)

c# loops while-loop

-5
推荐指数
2
解决办法
119
查看次数

为什么我在While循环中不断出现"预期表达式错误"?

所以我正在尝试将代码放在一起用于我的作业分配,并且出于某种原因,我在我的while循环条件下不断得到预期的表达式错误,我将<= end_money部分放在一起.错误显示在<=上.这是我的代码中唯一一个我收到此错误的地方.如果有人能帮助我,我会非常感激.我被困了这么久.这是片段:编辑:还有一个while循环的结束括号,我只是忘了将它粘贴在这里.

int player_total = 0;
int dealer_total = 0;
int player_bet;
int card_value = 0;
const int end_money = 1000;
const int starting_money = 100;
string card;
string response;



while (player_bet >= 0 && <= end_money)
{

    cout << "You have $100. Enter bet: ";
    cin >> player_bet;

    if (player_bet <= starting_money) {
        return true;

    }

    else if (player_bet > starting_money) {
        cout << "You only have $100 to bet. Enter bet: ";
    }
Run Code Online (Sandbox Code Playgroud)

c++ loops syntax-error while-loop visual-studio

-5
推荐指数
1
解决办法
325
查看次数

虽然循环在 Unity3D 中冻结游戏

我一直在与 while 循环作斗争,因为它们几乎不适合我。它们总是导致我的 Unity3D 应用程序冻结,但在这种情况下,我真的需要它来工作:

bool gameOver = false;
bool spawned = false;
float timer = 4f;

void Update () 
{
    while (!gameOver)
    {
        if (!spawned)
        {
            //Do something
        }
        else if (timer >= 2.0f)
        {
            //Do something else
        }
        else
        {
            timer += Time.deltaTime;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望这些 if 语句在游戏运行时运行。现在它使程序崩溃,我知道这是 while 循环的问题,因为它在我取消注释的任何时候都会冻结。

c# while-loop unity-game-engine

-5
推荐指数
1
解决办法
3503
查看次数

else 语句和 while 循环

代码跳过错误 if 语句并直接进入 else if

我需要跑几圈,如果少于 2 圈,就会出现错误并再次返回要求输入新值。反之亦然,大于 20。我是一名新程序员,发现 C# Windows 窗体很难理解

        int.TryParse(txtNumberOfLaps.Text, out laps);

while (laps < 2 && laps > 20)
        {
            if (laps < 2)
            {
                MessageBox.Show("Laps can't be less than 2", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            else if (laps > 20)
            {
                MessageBox.Show("Laps can't be more than 20", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            else if (laps > 2 && laps < 20)
            {
                break;
            }
        }
        
       
        
    
Run Code Online (Sandbox Code Playgroud)

c# if-statement boolean-expression while-loop conditional-statements

-5
推荐指数
1
解决办法
1026
查看次数

如何计算执行while循环的次数

在C中,如何计算while循环执行的次数?

在Python中,我只是在开头创建一个空列表,并在while每次执行循环时附加循环中的值.然后,我会找到该列表的长度,以了解该while循环的执行次数.C中有类似的方法吗?

c while-loop

-7
推荐指数
1
解决办法
2万
查看次数

While循环的输出

如果short int是2个字节,则在下面的prog中执行while循环多少次?

main()
{
int j = 1;
while(j <= 255);
{
    printf("%d",j); 
    j++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我认为应该是255次,但不正确.谁能告诉我为什么?

c while-loop

-7
推荐指数
1
解决办法
72
查看次数

C#While(i ++ ...)何时递增?

为什么在检查条件之后而不是在整个循环之后,While循环后递增(或后递减)变量?

如:

int x = 0;
while (x++ < 5)
Console.WriteLine(x);
Run Code Online (Sandbox Code Playgroud)

输出是:1 2 3 4 5,当我认为它应该是0 1 2 3 4.它似乎它立即检查条件 - 真实 - inrements.这是正常的行为,因为我最近一直在练习普通C,这完全不同.

我是否应该更加明确地使用此选项?

while (x < 5)
Console.WriteLine(x);
++x; 
Run Code Online (Sandbox Code Playgroud)

c# loops while-loop post-increment

-7
推荐指数
1
解决办法
2960
查看次数

-8
推荐指数
1
解决办法
1233
查看次数

如何以相反的方向重写ArrayList

我有一个ArrayList,我需要在相反的方向重写它.怎么做?

List<Posts> viewList = new ArrayList<Posts>(); 
viewList = viewLogic.getPostsList(username);
Run Code Online (Sandbox Code Playgroud)

java loops for-loop arraylist while-loop

-8
推荐指数
1
解决办法
484
查看次数

我不明白while循环

我是Java新手,我不太了解while循环.我想说'如果你不想选择Warrior类,那么回到字符选择框.

while (chooseCharacter = true){

        String[] Character= {"Kitty", "Archer","Tank","Battlemage","Warrior"};
        int characterSelect = JOptionPane.showOptionDialog (null, "Choose your class", "Class Selection", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, Character, "Warrior");

        if(characterSelect == 4){
            JOptionPane.showMessageDialog(null, "Warrior" + "\n" + "Attack 1: Slash" + "\n" + "10 Damage :: 98% Accuracy" + "\n" + "Attack 2: Spin Attack" + "\n" +  "25 Damage :: 67% Accuracy");
            String[] warriorChar= {"No", "Yes"};
            int warrior = JOptionPane.showOptionDialog(null, "Would you like to choose Warrior as your class?", "Choose this class?", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, …
Run Code Online (Sandbox Code Playgroud)

java loops while-loop

-8
推荐指数
1
解决办法
146
查看次数