标签: while-loop

如果while循环中的语句不起作用

public static void main(String[] args) {
    int i = 0;
    while(i==0){
        System.out.println("Possibilities: R, T, O and X.\nYour choice: ");
        Scanner scan = new Scanner(System.in);
        String bla = scan.nextLine();
        System.out.println(bla);
        if(bla=="R"){
            System.out.println("S or B?\n Your choice: ");
            String derp = scan.nextLine();
            if(derp=="S"){
                String rekeningNummer = spaarRekening.getRekeningNummer();
                }
        }
        if(bla=="T"){
            System.out.println("dit is T");
        }
        if(bla=="O"){

        }
        if(bla=="X"){

        }
        else{
            System.out.println("Impossible. Try again.");
        }
    i+=1;
    }

}
Run Code Online (Sandbox Code Playgroud)

我试图运行这个程序,但当遇到if语句时它会忽略它.我不知道为什么,我的想法已经不多了.

java if-statement nested while-loop

-4
推荐指数
2
解决办法
789
查看次数

如何在两个无限循环之间交替?

我想在两个无限循环之间交替; 我该怎么做到这一点.我的代码是这样的:

#include <stdio.h>

int main() {
    // Master loop
    while (1)

    // loop # 1
    while (1) {
        printf("I am in loop #1");
    }

    // loop # 2
    while (1) {
        printf("I am in loop #2");
    }
} // end of master loop
Run Code Online (Sandbox Code Playgroud)

如何在循环#1和循环#2之间切换?我只是测试一个想法来检查/比较两个循环时间(时间)之间的一些结果,以便在时间线中找到一些偏差/错误.

c c++ infinite-loop while-loop

-4
推荐指数
1
解决办法
1376
查看次数

循环运行永远

我正在创建一个基本的while循环,由于某种原因它无限运行.我想知道它在var"days"的长度上没有停止.

var day1 = 1
var days = 365

while (day1 <= days) {
    day1++;
 if (day1 = 358) {
    console.log('today is christmas');
 } else {
    console.log("today is day " + day1);
 }
}
Run Code Online (Sandbox Code Playgroud)

javascript while-loop

-4
推荐指数
2
解决办法
172
查看次数

如何在Ruby中创建模式程序?

我使用的是Ruby 1.9.3.我做了如下的模式程序:

n = 1
while n <= 5
  n.downto 1 do |i|
    print "* "
  end
  puts
  n += 1
end
Run Code Online (Sandbox Code Playgroud)

以上程序的输出如下:

*
* *
* * * 
* * * *
* * * * *
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试模式程序,如下所示:

        *
      * *
    * * *
  * * * *
* * * * *    
Run Code Online (Sandbox Code Playgroud)

我不知道我该怎么做?

有人可以帮我吗?

谢谢.

ruby while-loop

-4
推荐指数
1
解决办法
2363
查看次数

Python:如何在while循环中的break之前插入一个语句

对于我在Python中使用的以下while循环:

while True: 
    <do something>
    if <condition>: break 
Run Code Online (Sandbox Code Playgroud)

但我希望它在退出之前做一些事情,像这样:

while True: 
    <do something>
    if <condition>: 
    <do something> 
    then break 
Run Code Online (Sandbox Code Playgroud)

可能吗?什么是语法?谢谢!

python while-loop python-2.7

-4
推荐指数
1
解决办法
679
查看次数

用Python生成彩票号码生成器

我是一个业余爱好者,我在制作彩票号码发生器时需要一些帮助.我已经进行了一个循环,我将变量设置为变量为0然后我告诉python,当该变量小于6时,它需要在1,49的范围内创建一个随机数,然后我告诉它添加1到我的变量然后在1,49的范围内创建6个数字,虽然我需要的是这些数字按升序排列并且没有双精度数,所以没有重复的数字.

python random loops generator while-loop

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

为什么我被困在一个循环中?

我根据自己的要求编辑问题,并清楚地表明我的错误.这个问题已得到解答.

为什么我陷入无休止的循环?我尝试了一些不同的技术,比如在if语句中引入break语句,甚至只是在那里抛出break语句.但是我仍然陷入困境.

    while (recalculate = 1){

        cout << "\nEnter in the radius of the sphere: ";
        cin >> radius;
        cout << "\nEnter in the weight of the sphere: ";
        cin >> weight;
        cout << "\n";

        if (bForce > weight)
        {
            cout << "\nEgads, it floats!\n";
        }
        else {
            cout << "\nIt sunk...\n";
        }

        cout << "\nRecalculate? (1 = yes, 0 = exit)\n";
        cin >> recalculate;
        // See what recalculate really is. 
        cout << "\n" << recalculate;

    }
Run Code Online (Sandbox Code Playgroud)

c++ loops if-statement while-loop

-4
推荐指数
1
解决办法
71
查看次数

我的while语句是无限循环?

#include "stdafx.h"
#include <iostream>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>



using namespace std;

//this program will let the user input their assignment score and see their letter grade
int main() {

    int score;

    cout << "Input your score: ";

    //to make the while loop
    int x = 1;

    while (x == 1) {

        cin >> score;

        if (score >= 90){
            cout << "\nA";
            break;
        }

        else if (score >= 80) {
            cout << "\nB";
            break;
        }

        else if …
Run Code Online (Sandbox Code Playgroud)

c++ while-loop

-4
推荐指数
1
解决办法
798
查看次数

c#error:是一个'field',但在尝试使用while循环时用作'type'

我正在通过C#进行euler项目来学习语言并在暑假期间保持我的编码技能(我是大学生).所以我对这门语言很新.我试图寻找这个错误的答案,但我找到的所有其他类似问题都没有处理while循环.有问题的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EulerProblems
{
    class Problem2
    {
        List<int> fib = new List<int>();
        bool notAtLimit = true;

        while(notAtLimit)
        {
             //code to populate list of fibonacci series
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# while-loop

-4
推荐指数
1
解决办法
66
查看次数

如何使while循环花费一定的时间

我正在使用python为Minecraft pi版制作一个数字时钟.我正在使用包含大量代码的单个while循环 - 执行一次运行需要很短的时间,但它比我想要的还要多几毫秒,这对我的时钟造成了严重破坏.有没有办法让while循环完全准确?我正在使用两个计数time.sleep(1),但执行时间超过两秒.

例:

while True:
    start = time.time()
    for _ in range(5):
        1000**3
    time.sleep(1)
    print (time.time() - start)
Run Code Online (Sandbox Code Playgroud)

每个循环需要超过1秒.

1.00102806091
1.00028204918
1.00103116035
1.00051879883
1.0010240078
1.00102782249
Run Code Online (Sandbox Code Playgroud)

此错误随着时间累积.我怎样才能防止漂移?

python while-loop

-4
推荐指数
1
解决办法
917
查看次数