标签: while-loop

在Java中学习嵌套的while循环

在这个程序中,内循环生成100个随机数,然后在随机数为7时停止生成它们.外循环重复内循环100次.
为什么我的外循环不能重做内循环?
似乎它只做过一次.

package test;


public class Loops {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 0;
        int sum = 0;
        int counter = 0;
        String randomNumberList = " ";
        int c = 0;
        while (c != 100){

            while (i != 7) {
            i = (int) (101 * Math.random());
            sum += i;
            ++counter;
            randomNumberList += " " + i;
            }
        System.out.print("\n loop repeated" + counter+ " times and …
Run Code Online (Sandbox Code Playgroud)

java loops while-loop

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

如何在While循环中初始化关联数组?

我现在做一个简单的事情,我会从一个CSV文件中读取,即A列中包含的代码,如"EN",B列中包含特定名称"英" ..等等,我想将它们读入关联数组.

我目前的做法是这样的:

  $handle = fopen("Languages.csv","r") or die("EPIC FAIL!");

  $languageArray = array(
  while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
  {
              $row[0] => $row[1],
  }
  )
Run Code Online (Sandbox Code Playgroud)

但它实际上抱怨我的语法,所以我只是想知道是否有办法通过获取我的csv文件的所有行来初始化我的关联数组,并将第一个字符串(从列A)作为键,第二个字符串(来自B列)作为值?

谢谢.

php arrays associative-array while-loop

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

从Java中的数字列表中查找平均值

这是我第一次尝试java问题,作为编程任务的一部分.

我必须创建一个程序来计算用户输入的数字列表的平均值.输入0时应终止数据.我的问题是这个"所有的负数都应该被忽视"

由于某种原因,下面的代码不起作用,当我输入一个负数时,它应该被忽略,但由于某种原因它终止了数据

import java.util.*;

class Task_8
  {
public static void main()
  {

    Scanner inputLine = new Scanner(System.in);

    float row, numberentered, numbersum = 0, negativenumber = 0;
    double result, count = 0;


    System.out.println ("Welcome to Task 8 of 10 of my Programming Assignment... Nearly There!");
    System.out.println ("_____________________________________________________________________");
    System.out.println ();
    System.out.println ("Enter as many numbers as you like and this program will tell you the arithmatic mean");
    System.out.println ("Terminate data entry by entering 0");


   do{

       System.out.print ("Please enter a number: "); …
Run Code Online (Sandbox Code Playgroud)

java loops average while-loop

3
推荐指数
2
解决办法
3899
查看次数

聪明的while循环错误

官方文档说聪明有"同时"的支持.但是我在模板中做了以下事情:

{while $foo > 0}
  {$foo--}
{/while}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

语法错误:无法识别的标签'while'(Smarty_Compiler.class.php)

你能帮忙解决这个问题吗?非常感谢.

smarty while-loop

3
推荐指数
1
解决办法
1775
查看次数

查找未知连续循环的最小值和最大值

我试图找到下面给出的连续while循环的最小值和最大值,但不知怎的,我无法得到正确的逻辑.请告诉我哪里出错了.

while (true)
   {
         Function(&RawX, &RawY, &RawZ);// Keeps generating new RawX,Y and Z values

         if(MaxRawX < RawX)
            MaxRawX = RawX;
         if(MinRawX > RawX)
            MinRawX = RawX;

         Output("MaxRawX:%0.2f",MaxRawX);
   }
Run Code Online (Sandbox Code Playgroud)

我在上述算法中遇到的问题是RawX,RawY和RawZ的值不断变化.例如:在某一点上,我的值范围为-46到-35.我希望我的程序将MinRawX显示为-46,将MaxRawX显示为-35.在某些其他方面,我可能在201到215之间有值,我希望它将MaxRawX显示为215,将MinRawX显示为201.它基本上是我从硬件接收的一些传感器角度数据.我确信我在这里做错了,因为这是非常基本的,但无法弄清楚.有什么建议?

c++ algorithm while-loop

3
推荐指数
1
解决办法
2029
查看次数

C++尝试Catch内部循环

我有这个C++程序,具有以下一般结构

1st while (condition A == true)
  //some code for 1st loop

  2nd while (condition B == true)
    //some code for 2nd loop
    try
      //some code for try
    catch
      //condition B == false (supposed to leave 2nd loop and go back to first loop)
Run Code Online (Sandbox Code Playgroud)

我希望它在出现异常时离开第二个循环并返回到第一个循环,直到条件B再次出现.如上所述,它不像我期望的那样工作.似乎正在发生的是代码陷入困境catch并且永远不会离开它.

如何安排它以使其按需要工作?

注意:条件A永远不会为假.

c++ try-catch while-loop flow-control

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

我的while循环重复一遍又一遍

它正在引用一个具有9个插槽的游戏板,一旦插槽被填满## while循环在没有任何空位且我不知道如何修复它时继续搜索新的位置,请帮助!:(

        computer = random.randint(0, 8)
        if board[computer] != 'X' and board[computer] != 'O':
            print computer
            board[computer] = 'O'
        else:
            while board[computer] == 'O' or 'X':
                counter = 0
                if counter > 15:
                    break
                computer = random.randint(0, 8)
                print computer
                if board[computer] != 'X' and board[computer] != 'O':
                    board[computer] = 'O'
                counter += 1
Run Code Online (Sandbox Code Playgroud)

python while-loop

3
推荐指数
1
解决办法
166
查看次数

虽然声明不起作用C.

我正在学习C,而我正在尝试制作一个基本的计算器,但是在使用while语句时遇到了一些麻烦.我尝试过多种方式,但它从不重复,只需完成脚本.

有任何想法吗?

//
//  main.c
//  Calculator
//
//  Created by Austen Patterson on 2013-06-27.
//  Copyright (c) 2013 Austen Patterson. All rights reserved.
//

#include <stdio.h>
#include <stdbool.h>

int main()
{
    int number[100];
    int operator = '0';
    int doAgainAnswer = '0';
    bool doAgain;
    do{ 
    printf("Please enter your first number:");
    scanf("%d", &number[1]);
    printf("\nYou entered %d as your first number. Please enter your second: ", number[1]);
    scanf("%d", &number[2]);
    printf("\nYou entered %d as your second number.", number[2]);
    printf("\nYour numbers are now %d …
Run Code Online (Sandbox Code Playgroud)

c while-loop do-while

3
推荐指数
1
解决办法
346
查看次数

在java中使用while循环

我正在尝试读入并添加(仅)正整数,直到输入负整数.只有输入负整数时,程序才会停止.我能想出的最好的是下面的代码,但问题是即使读取负整数也不会停止.PS:我还在学习,请耐心等待.我已经尝试添加&& input2!<0到while循环条件但它出现错误.关于我如何使它按照我想要的方式行事的任何建议?谢谢.

public class Total{
    public static void main(String[] args){

            System.out.println("Enter an integer: ");

            Scanner entry = new Scanner(System.in);
            int input1 = entry.nextInt();

            System.out.println("Enter another integer: ");
            int input2 = entry.nextInt();

            int total = input1 + input2;

            while (input2 > 0){            
                System.out.println(total + "\nEnter another interger:  ");
                total += entry.nextInt();
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

java loops while-loop

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

创建无限循环而不破坏程序

我有一个游戏网格,在"步骤"中更新(康威的生命游戏,虽然这与我的问题无关).我正在尝试创建一个自动运行模拟的"播放"按钮,直到再次按下该按钮以暂停模拟.起初我有这样的事情:

public partial class MainWindow : Window
{
    bool running = true;
    public MainWindow()
    {   
        InitializeComponent();
        bool isProgramRunning = true;
        while(isProgramRunning)
        {
            while(running)
            ProcessGeneration();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我播放/暂停模拟的按钮有以下点击处理程序:

private void PlayClick_Handler(object sender, RoutedEventArgs e)
{           
     PlayButton.Content = ((string)PlayButton.Content == "Play") ? "Pause" : "Play";

     running = (running) ? false : true;
}
Run Code Online (Sandbox Code Playgroud)

我认为这会让我的程序简单地保持循环(isProgramRunning永不结束),反复检查"running"是真还是假,按下按钮切换"running"将允许我循环/中断循环.但只是while(isProgramRunning)部分杀死程序(它甚至不会加载).实际上每次我尝试使用while()时,程序都会停止响应.有办法解决这个问题吗?

c# wpf loops while-loop

3
推荐指数
2
解决办法
3774
查看次数