我有一个问题,请帮助我.我已经阅读了关于do while语句的网页上的内容,不同的是,在写入0时,不是布尔条件
do{
// do some instruction
}while(condition );
Run Code Online (Sandbox Code Playgroud)
显然是可以理解的,但这一个
do
{
//again some instruction
}while(0);
Run Code Online (Sandbox Code Playgroud)
我无法猜测它做了什么?它是否等同于:做某事(假)?或者可能是无限循环?请帮助我
使用Java中的程序员定义的方法处理简单的骰子滚动游戏.因为我知道程序需要至少运行一次,然后询问玩家是否想再次使用,我选择使用do/while循环.我以前从未使用过,但我相信我的设置是正确的.
我的代码如下,不包括循环中包含的main方法的主体,以及程序员定义的方法.
do {
// main METHOD BODY ..
String proceed = ui.nextLine();
System.out.print("Would you like to play again (yes/no)? ");
} while (proceed.charAt(0) == 'Y' || proceed.charAt(0) == 'y');
Run Code Online (Sandbox Code Playgroud)
此时我从Eclipse IDE获得的是"继续无法解决".
关于发生了什么的任何想法?
好吧基本上我很难找到为什么这不起作用,因为我认为它应该,并需要帮助获得正确的输出.我试过用这种格式搞砸几种方法,但没有任何作用,我真的不明白为什么.以下是说明,其后是我的来源:
编写一个循环,从标准输入读取字符串,其中字符串是"land","air"或"water".当读入"xxxxx"(五个x字符)时,循环终止.忽略其他字符串.在循环之后,你的代码应打印出3行:第一行包含字符串"land:",后跟读入的"land"字符串的数量,第二行包含字符串"air:",后跟数字" air"字符串读入,第三个字符串由"water:"组成,后跟读入的"water"字符串数.每个字符串应打印在单独的行中.
ASSUME变量stdin的可用性,该变量引用与标准输入关联的Scanner对象.
int land = 0;
int air = 0;
int water = 0;
do
{
String stdInput = stdin.next();
if (stdInput.equalsIgnoreCase("land"))
{
land++;
}else if (stdInput.equalsIgnoreCase("air"))
{
air++;
}else if (stdInput.equalsIgnoreCase("water"))
{
water++;
}
}while (stdin.equalsIgnoreCase("xxxxx") == false); // I think the issue is here, I just dont't know why it doesn't work this way
System.out.println("land: " + land);
System.out.println("air: " + air);
System.out.println("water: " + water);
Run Code Online (Sandbox Code Playgroud) 它只是保持循环.数字继续减少,直到该计划结束.我滥用了什么吗?
playerHealth和orcHealth ints是100.
randomNumber = ("%10d", 1 + (rand() % 100));
Run Code Online (Sandbox Code Playgroud)
这是我在srand()解释页面上看到的随机数的方式.如果这是错的,应该怎么做?
这里还有其他问题吗?
switch(charDecision)
{
case 1:
cout << "FIGHT" << endl;
do{
randomNumber = ("%10d", 1 + (rand() % 100));
if(randomNumber >= 50){
orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
}
else
{
playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
cout << "The orc hit you! You …Run Code Online (Sandbox Code Playgroud) 是否有必须使用do while循环的情况?这是一种公认的做法吗?看起来它等同于一个普通的while循环,除了它的第一次迭代发生在检查条件之前,如果这是偶然的话.
int i = 3;
while ( i > 0 ) { // runs 3 times
i--;
}
Run Code Online (Sandbox Code Playgroud)
VS
int j = 3;
do {
j --;
} while ( j > 0 ); // runs 3 times
Run Code Online (Sandbox Code Playgroud)
相同?
编辑:我已经看过java文档,但是java文档中的示例看起来并不像要求do while循环中的特定例程必须在do while循环中运行而不是在常规while循环中运行!
所以这应该继续循环,直到用户点击"ENTER"或数组已填满.但在输入数组中的第一个元素后,它退出循环.
do
{
System.out.print("Enter name (or <ENTER> if done): ");
names[index] = kb.nextLine();
if(! (names[index].equals("")))
{
System.out.print("Enter phone number: ");
phone[index] = kb.nextLine();
System.out.print("Enter email address: ");
email[index] = kb.nextLine();
index++;
break;
}
} while ( ! (names[index - 1].equals("")) && index < SIZE);
Run Code Online (Sandbox Code Playgroud)
更正
do
{
System.out.print("Enter name (or <ENTER> if done): ");
names[index] = kb.nextLine();
if(! (names[index].equals("")))
{
System.out.print("Enter phone number: ");
phone[index] = kb.nextLine();
System.out.print("Enter email address: ");
email[index] = kb.nextLine();
}
index++;
} while ( ! …Run Code Online (Sandbox Code Playgroud) 我通常做Java编程,但想用C#测试我的运气,看看它是什么样的.我更像是一个新手程序员,所以也许这很愚蠢,但是在我的Do-While循环中,为什么它只有在我使用&&而不是||时才有效 当试图终止我的循环?对我来说,在Java中,|| 将被使用,因为你想要一个大写或小写"q"来终止.我正在读取终止为"Q"和"q".显然情况并非如此,因为循环终止就好了.我只是不明白这是如何工作的.
namespace Testing_Grounds
{
class Program
{
static void Main(string[] args)
{
string entry;
do
{
Console.WriteLine("This is an inventory screen");
Console.WriteLine("1. Health Potions");
Console.WriteLine("2. Mana Potions");
Console.WriteLine("3. Revive Potions");
Console.Write("Please enter the number of the item " +
"you would like to inspect, or enter \"Q\" to quit: ");
entry = Console.ReadLine();
if (entry.Equals("1"))
{
Console.WriteLine("Health Potions");
}
else if (entry.Equals("2"))
{
Console.WriteLine("Mana Potions");
}
} while (entry != "Q" && entry != "q");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我很确定这应该有用......但即使我输入0或1,它仍然要求我选择一种颜色.如果我输入0或1,我是愚蠢还是退出循环?
public static int setColour() {
EasyReader keyboard = new EasyReader();
int colour;
do{
colour = keyboard.readInt("Pick a colour (black = 0, white = 1): ");
}while (colour != 0 || colour != 1);
return colour;
}
Run Code Online (Sandbox Code Playgroud) 我已经编写了用于读取开关状态的代码,如果按下#是3次则退出.
void allkeypadTest(void)
{
static uint8_t modeKeyCount=0;
do
{
uint8_t key=getKeyStatus();
if(key)
{
if(key=='#')
{
modeKeyCount++;
//pulseIODevice(LED1,1,500,200);
}
else
{
pulseIODevice(LED1,key-0x30,500,200);
}
}
}while(modeKeyCount<3);
}
Run Code Online (Sandbox Code Playgroud)
但是一旦我输入#key一次,循环就会退出.如果按其他键,行为就可以了.但是,如果我pulseIODevice在if(key=='#')部分下取消注释,行为是正常的.pulseIODevice将在特定时间段内将LED切换一定时间并将PWM传递给它.我很困惑我的代码出了什么问题.请注意,如果未检测到任何键,getKeyStatus则返回'\0'(null)并返回1x4键盘键的ASCII值(ASCII值为3,6,9和#)
我正在编写一个应该在正整数列表中读取的程序(包括0).如果用户输入的数字超过100或输入负数,则该循环应该终止.
我尝试做一个简单的do-while循环来解决这个问题但是在运行时,无论我提供什么数字,循环都永远不会终止.我试过几次改变循环,到目前为止都没有成功.我一定是犯了一个非常微妙的菜鸟错误,但我似乎无法抓住它.
int[] list = new int[100];
int size=0;
do{
list[size] = kbd.nextInt();
size++;
}while((size < 100) && (list[size] >= 0));
}
Run Code Online (Sandbox Code Playgroud)