对不起,我是一个菜鸟我读了无数关于制作一个简单的计时器的教程,并想知道为什么它不起作用,直到我注意到它是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) 我正在编写一个使用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) 此代码由于某种原因起作用,但它根本没有意义.
#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)
有人可以解释为什么它确实有效以及它有什么应用程序?
我正在为一个类的项目工作,并且可以使用一些帮助来弄清楚为什么我的代码的行为方式.赋值是提示用户输入两个整数并选择要对它们执行的算术运算,同时避免"除以零"场景.这是我的代码的一部分:
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) 我有一个小问题,我用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)
但是,无论你按取消还是确定,它都会删除它.我怎样才能解决这个问题?
具体来说,我有一个文本文件,其中排列了多行,以便每个条目的键是组的第一行,后跟其相关值,后跟下一个键之前的换行符.我很难用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) 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数组来存储用户的数据.就是名字; 年龄; 地点.
我在平均显示小数时遇到问题.它一直显示.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) 我做了以下脚本:
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)
我有两个问题:
end控制台我不能看到aha并break从环路(last是相等的break权)? EOF阻止while循环的关键组合?我以为它在ctrl+D,Windows但它不起作用.我应该制作一个程序,不断接受桌面订单数据,并显示超过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) while-loop ×10
java ×6
android ×1
average ×1
c ×1
casting ×1
confirm ×1
dictionary ×1
do-while ×1
iteration ×1
javascript ×1
loops ×1
perl ×1
php ×1
postdelayed ×1
python ×1
sentinel ×1
sum ×1
windows ×1