标签: while-loop

Android postDelayed有效,但无法将其置于循环中?

对不起,我是一个菜鸟我读了无数关于制作一个简单的计时器的教程,并想知道为什么它不起作用,直到我注意到它是while循环导致问题oO我已经删除它然后它工作但只有一次我需要使用循环,所以运动结束:C

下面是代码:

old_x是来自ImageView的坐标和来自onTouch事件的new_x,可能是问题,因为我将它们作为int转换?我不知道我需要做什么,所以它有效请帮助O:

    while(old_x != new_x)
    {
        timedMoveIV(100);
        old_x = (int)img.getX();
    }
Run Code Online (Sandbox Code Playgroud)

它调用这个方法,如果我没有循环这样做.

public void timedMoveIV(int time_ms)
{
    //sleep for time_ms milliseconds
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
         public void run() { 
            if(new_x > img.getX())
            {
            img.setX(img.getX() + 1);
            }
            else
            {
            img.setX(img.getX() - 1);   
            }
         } 
    }, time_ms);
}
Run Code Online (Sandbox Code Playgroud)

java android casting while-loop postdelayed

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

方法必须返回int

我正在编写一个使用GCD的欧几里德算法的程序(a,b)= GCD(b,r)r = a%b.我写了一个方法,应该返回一个整数,以便主方法吐出,但是当我要求它执行此操作时,它表示它不返回一个整数.这是代码

public class Euclid {

    public static int GCD(int a, int b)
    {
        while (b != 0)
        {
            int r = a%b;
            System.out.println("(" +a+ "," +b+ ")");
            b = r;
            a = b;
            if(b == 0)
            {
                return a;
            }
        }
    }
    public static void main(String[] args)
    {
        System.out.println(GCD(36, 20));    
    }
}
Run Code Online (Sandbox Code Playgroud)

java while-loop greatest-common-divisor

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

Mingling开关和C语言

此代码由于某种原因起作用,但它根本没有意义.

#include <stdio.h>

int main(void)
{
  switch(1)
    {
      case 0:
        while(1)
          {
            case 1: puts("Works"); break;
          }
    }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么它确实有效以及它有什么应用程序?

c while-loop switch-statement

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

在第二次尝试之前,Switch不接受输入

我正在为一个类的项目工作,并且可以使用一些帮助来弄清楚为什么我的代码的行为方式.赋值是提示用户输入两个整数并选择要对它们执行的算术运算,同时避免"除以零"场景.这是我的代码的一部分:

import java.util.Scanner;

public class Program3 
{

public static void main(String[] args) 
{

    double operandOne;
    double operandTwo;
    char newLine = '\n';
    Scanner input = new Scanner (System.in);

    //"input" is an object which calls for input from the keyboard.

        System.out.print("This program will request values for two operands ");
        System.out.println("and perform an arithmetic operation of your choice on them.");
        System.out.println(newLine + "Please enter a value for the first operand.");
        operandOne = input.nextDouble();

        System.out.println("Thank you.  Please enter a value for the second operand."); …
Run Code Online (Sandbox Code Playgroud)

java while-loop switch-statement do-while

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

Javascript确认&循环回声

我有一个小问题,我用PHP while循环创建了一个删除按钮,如下所示:

while($something = mysql_fetch_array($sql_something)){

    $id = $something['id']
    echo '<a href="somewhere.php?id='.$id.'"><button onclick="delconfirm()">Delete</button></a>

}
Run Code Online (Sandbox Code Playgroud)

这个echo是一些内容的删除按钮.但是我首先需要用户确认删除,这是onclick="delconfirm()"进来的地方.

我的确认看起来像这样:

function delconfirm()
{
    var r=confirm("Are you sure you want to delete this content?");

    if (r==true){

        // ...do nothing i guess? it needs to redirect using the PHP echo'd link...

    }
    else{

        window.location = "edit.php";

    }
}
Run Code Online (Sandbox Code Playgroud)

但是,无论你按取消还是确定,它都会删除它.我怎样才能解决这个问题?

javascript php confirm while-loop

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

如何在Python特定文件类型中将文件读入字典

具体来说,我有一个文本文件,其中排列了多行,以便每个条目的键是组的第一行,后跟其相关值,后跟下一个键之前的换行符.我很难用while循环用readline完成这个.

例如,第一行可以是餐馆名称,后面是几行在那里吃​​的客户,然后我需要在餐馆名称的键下写入字典的值.

我真的不熟悉文件阅读,所以我担心我所拥有的东西根本就没有帮助.

我猜这样的事情,但它甚至没有达到半功能的程度.

编辑:感谢您应该澄清的响应,相应键的每个值项都列在后面的行中,后面的行在下一个键之前的列表末尾有一个空行.不幸的是,我不得不在这里使用readline方法.

    restaurants = {}
    patrons = []

    line = file.readline()
    s = line.strip('\n')

    while s != ''
    restaurant = s
    line = file.readline
    patrons.append(s)
Run Code Online (Sandbox Code Playgroud)

python dictionary while-loop

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

打破; 线无法到达

String userAge;

    while(true){
        System.out.println("Enter user Age");
        userAge = scInput.nextLine();
            if(tryParseInt(userAge)){
             return userInfo[row][1] = userAge;
             break;
            }else{
                System.out.println("Please Enter an integer.");
                }
    }
Run Code Online (Sandbox Code Playgroud)

初学者,名字是里根.

我的问题是我的代码因为"break;"而无法编译.它说这是一个无法访问的代码.我觉得我在这里做错了,我几乎可以肯定它与变量类型有关..不知道怎么做.这是一种要求用户年龄的方法.

我的目标是通过我的tryParseInt()方法传递一个字符串来测试并确保输入数据是一个整数,但保留它作为字符串类型变量.

我正在创建一个多维String数组来存储用户的数据.就是名字; 年龄; 地点.

java while-loop unreachable-code

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

如何平均输出小数?

我在平均显示小数时遇到问题.它一直显示.00或.1.我试图把它放在一起,但我仍然得到相同的结果.另外,我想包括我输入的第一个整数,但我不知道如何.请帮助:

import java.io.*;
import java.util.*;

public class WhileSentinelSum 
{
static final int SENTINEL= -999;

public static void main(String[] args)
{
    // Keyboard Initialization
        Scanner kbin = new Scanner(System.in);

    //Variable Initialization and Declaration
        int number, counter;
        int sum =0;
        int average;

    //Input
        System.out.print("Enter an integer number: " );
        number = kbin.nextInt();
        counter=0;


    //Processing
    //Output
        while(number != SENTINEL)
        {
            counter++;
            System.out.print("Enter an  integer number: ");
            number = kbin.nextInt();
            sum += number;
        }

        if (counter !=0)
            System.out.println("\nCounter is: " + counter);
        else
            System.out.println("no …
Run Code Online (Sandbox Code Playgroud)

java average sum while-loop sentinel

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

为什么我不能在循环时终止这个?

我做了以下脚本:

print "Will accept input until EOF";  

while(defined($line = <STDIN>)){  
    print "Input was $line \n";  
    if(chomp(@line) eq "end"){  
        print "aha\n";  
        last;  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  1. 为什么当我输入end控制台我不能看到aha并break从环路(last是相等的break权)?
  2. 什么是EOF阻止while循环的关键组合?我以为它在ctrl+D,Windows但它不起作用.

windows perl while-loop

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

循环不执行多次

我应该制作一个程序,不断接受桌面订单数据,并显示超过36英寸长,至少有一个抽屉的橡木书桌的所有相关信息.

import java.util.*;

public class MangMaxB 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        char ans;
        int orderNum=0, length=0, width=0, numDrawer=0, price=1000;
        String name;

        System.out.print("Do you wish to enter Oak " +
                        "desk order data? (y/n)");
        ans = input.nextLine().charAt(0);

        while (ans != 'n')
        {  
            System.out.print("Enter customer name: ");
            name=input.nextLine();

            System.out.print("Enter order number: ");
            orderNum=input.nextInt();    

            System.out.print("Enter length and width of Oak desk" +
                        " separated by a space: ");
            length = input.nextInt();
            width = input.nextInt();

            System.out.print("Enter number …
Run Code Online (Sandbox Code Playgroud)

java iteration loops while-loop java.util.scanner

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