标签: while-loop

字符串索引超出范围?(Java,子串循环)

我正在为COSC课程制作的这个程序没有正确编译,我不断收到错误:

线程"main"中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:2

在VowelCount.main的java.lang.String.substring(String.java:1765)(VowelCount.java:13)

这是我的代码:

import java.util.Scanner;

public class VowelCount {
 public static void main(String[] args) {
  int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0;
  String input, letter;
  Scanner scan = new Scanner (System.in);

  System.out.println ("Please enter a string: ");
  input = scan.nextLine();

  while (count <= input.length() ) {
   letter = input.substring(count, (count + 1));

   if (letter == "a") {
    a++; }
   if (letter == "e") {
    e++; }
   if …
Run Code Online (Sandbox Code Playgroud)

java substring while-loop

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

在PHP中有条件的while循环?

我需要做一个PHP while循环,但前提是变量为true.而且我不能把while循环放在一个"if"语句中,这似乎是显而易见的事情,因为代码块是巨大的,它将是丑陋和混乱.我是否需要将循环中的代码分解为函数,还是有更简单的方法来处理它?

这是基本的想法:

if(condition){
  while(another_condition){
    //huge block of code loops many times
  }
} else {
  // huge block of code runs once
}
Run Code Online (Sandbox Code Playgroud)

我希望无论条件变量的状态如何都要执行巨大的代码块 - 但是如果condition为false则只执行一次,并且如果condition为true,则执行for another_condition为true.

以下代码不起作用,但提供了我想要完成的内容:

if(condition){ while(another_condition){ }
  // huge block of code
if (condition){ } } // closes the while loop-- obviously throws an error though!
Run Code Online (Sandbox Code Playgroud)

提前致谢.

php loops while-loop

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

在Java中适当使用多级中断

我最近编写了一个小的java程序(作为8086汇编程序的设计)并且我处于一个有趣的位置 - 我需要退出一个内部switch语句的while循环,就像这样(伪代码,显然):

:MyLoop
While(foo)
    switch (bar)
       case '1': print '1'; break
       case '0': print '0'; break
       case ';': end while loop;
Run Code Online (Sandbox Code Playgroud)

它似乎是goto语句的理想之处,因为单个"break"只会退出switch语句,(特别是考虑到我是为汇编而设计的)但是Java没有任何东西!

我发现Java有一个叫做多级中断的东西,所以通过使用"break MyLoop",程序将突破switch case 和 while循环."

那么我的问题是 - 这是多级休息的恰当用途吗?如果出于某种原因,我想保持切换(而不是嵌套的其他ifs)语句,是否有另一种方法可以通过"break"或"continue"单独模仿多级中断?

java label while-loop switch-statement

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

而Java中的循环怪异

我注意到java(因此可能是C)对此没有任何问题:

while(condition1) {
    //do somethin'
} while(condition2);
Run Code Online (Sandbox Code Playgroud)

这是一样的:

while(condition1 && condition2) {
    //do somethin'
}
Run Code Online (Sandbox Code Playgroud)

java loops while-loop

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

是否可以将此循环转换为python中的列表解析

我有一点code我想知道它是否可以写入list comprehension.这while loop部分是我对冷凝感兴趣的部分.

>>> sum=33
>>> final_list=[]
>>> LastCoin=[0, 1, 2, 1, 2, 5, 1, 2, 1, 2, 5, 1, 2, 
           1, 2, 5, 1, 2, 1, 2, 5, 1, 2, 1, 2, 5, 1, 2, 1, 2, 5, 1, 2, 1]
>>> while sum>0:
...    final_list.append(LastCoin[sum])
...    sum-=LastCoin[sum]
... 
>>> print final_list
[1, 2, 5, 5, 5, 5, 5, 5]
>>> 
Run Code Online (Sandbox Code Playgroud)

python list-comprehension list while-loop

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

for循环和while循环中的变量范围

我是PHP新手,我不明白为什么下面代码的最终结果是'233'而不是'231',foreach中的$ a是不是临时变量?

<?php
    $a = '1';
    $c = array('2', '3');
    foreach($c as $a){
        echo $a ;
    }
    echo $a;
?>
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?THKS.

更新2014-11-28现在我知道我的问题是什么.正如已接受的答案和这个答案所指出的那样,无论foreach是while行为还是行为functions,它们都只是正常的句子$a='3';.所以现在我知道这是我的误解,而且不仅仅是php因为我在python中尝试过,它也是一样的.

a = 123
b = [1, 2, 3]
for a in b:
    print a
print a
Run Code Online (Sandbox Code Playgroud)

php python for-loop while-loop

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

尝试使用Ruby while循环查找字符串的元音

def count_vowels(string)
  vowels = ["a", "e", "i", "o", "u"]
  i = 0
  j = 0
  count = 0

  while i < string.length do
    while j < vowels.length do
      if string[i] == vowels[j]
        count += 1
        break
      end

      j += 1
    end

    i += 1
  end

  puts count 
end
Run Code Online (Sandbox Code Playgroud)

我无法发现出错的地方.如果该程序遇到辅音,它就会停止.另外,使用".each"方法如何解决同样的问题?

ruby arrays while-loop

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

打破一会儿循环,一口气切换

关于C ++中的这个问题,存在类似的问题,但是我在这里使用JavaScript。我基本上和其他帖子中的OP处于相同的情况。

var input = prompt();
while(true) {
    switch(input) {
        case 'hi':
        break;
        case 'bye':
            //I want to break out of the switch and the loop here
        break;
    }
    /*Other code*/
}
Run Code Online (Sandbox Code Playgroud)

反正有这样做吗?

我/*Other code*/在代码也应该中断的部分中也有多个开关。

javascript break while-loop switch-statement

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

用于查找素数的Python while循环

作为Python的第一个练习,我正在尝试使用循环编写程序来查找素数.一切都与for循环一起工作所以我试图使用while循环.这有效,但程序返回一些不正确的数字.

import math
# looking for all primes below this number
max_num = int(input("max number?: "))

primes = [2]  # start with 2
test_num = 3  # which means testing starts with 3

while test_num < max_num:
    i = 0
    # It's only necessary to check with the primes smaller than the square
    # root of the test_num
    while primes[i] < math.sqrt(test_num):
        # using modulo to figure out if test_num is prime or not
        if (test_num % primes[i]) == 0: …
Run Code Online (Sandbox Code Playgroud)

python primes while-loop python-3.x

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

在"A:"中对A进行了什么操作?

让我们说:

class Bar:
    pass
A = Bar()

while A:
    print("Foo!")
Run Code Online (Sandbox Code Playgroud)

然后调用什么操作A来确定while循环?

我尝试过,__eq__但没有做太多.

python class operation while-loop python-3.x

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