所以我有一个 do-while 循环,它应该读取用户输入。如果用户输入 J 那么它应该要求用户输入 2 个数字来计算它们,但是如果用户输入 N 那么它应该停止循环。知道为什么它不起作用吗?
if (input.equalsIgnoreCase("J")) {
do
{
status=true;
}
while (status);{
if (input.equalsIgnoreCase("N"))
status = false;
}
}
Run Code Online (Sandbox Code Playgroud)
完整代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean status=true;
while (status){
Scanner minScanner1 = new Scanner(System.in); // mina scanners som tar in userinput
Scanner scanner2 = new Scanner(System.in);
System.out.println("Skriv in ditt nummer en och en :\n"); //obivous
double nr1 = minScanner1.nextDouble(); // int nr 1 lagrar det userinput skriver …Run Code Online (Sandbox Code Playgroud) 我一直在寻找类似于我现在遇到的问题的几个小时,但是我找不到任何东西,也没有找到任何东西,也没有找到其他网站.
#include <iostream>
using namespace std;
int main()
{
double Numero=0, ParteDecimale=0, Controllo=0, Controllo2=0;
int ParteIntera=0;
string Var1="1", Var0="0", NumeroFinale;
cout<<"\n\n\tBenvenuto, questo programma ti consente di convertire la parte\n"
"\tdecimale di un numero in sistema binario. Per iniziare, inserisci un numero: ";
cin>>Numero;
cout<<Numero<<" Numero\n\n"<<endl; //
ParteIntera=Numero; //casting implicito, in modo da poter isolare esclusivamente la parte decimale del numero inserito.
cout<<ParteIntera<<" ParteIntera\n\n"<<endl; //
ParteDecimale=Numero-ParteIntera; //Isolamento della parte decimale
cout<<ParteDecimale<<" ParteDecimale\n\n"<<endl; //
Controllo2=ParteDecimale;
do{
ParteDecimale=ParteDecimale*2; //Moltiplicazione della parte decimale per …Run Code Online (Sandbox Code Playgroud) 我的代码基本上将数字排列成相反的顺序,例如 415 程序会将它排列成 514 我的代码是正确的,但我有一个问题,输出应该是垂直的。
Run Code Online (Sandbox Code Playgroud)expected output 5 1 4
import java.util.Scanner;
public class Main{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int rev=0;
while( num != 0 )
{
rev = rev * 10;
rev = rev + num%10;
num = num/10;
}
System.out.println(rev);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用while循环,它应该在它应该时终止.如果它正常工作,那么当randno它是== highbound或时它会终止== lowbound.
循环代码:
do {
do {
randno = (int) (Math.round((Math.random()*(4)) + 0.5)-1);
direction = getDirection(randno,heading);
} while (robot.look(direction)==IRobot.WALL);
System.out.println(randno);
System.out.println(highbound);
System.out.println(lowbound);
System.out.println("---------------");
} while (randno!=lowbound | randno!=highbound);
Run Code Online (Sandbox Code Playgroud)
输出是3 3 2 ------,或者2 3 2 ------,因此循环应该结束.第一个循环正确结束(我嵌入它们试图让它工作......).出了什么问题?
嗨,我有一个关于做循环的问题.我是一个初学者,所以我猜这是一个非常简单的问题.
我有这个程序:
public class MyClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int value = 0;
do {
System.out.println("Enter a number");
value = input.nextInt();
}
while (value != 5);
{
System.out.println("Got 5!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
哪个转弯产生得到5!当输入为5时,它可以正常工作.我的问题是,为什么不"得到5!" 当值输入不是5时执行,因为条件表示"值!= 5"并且Sysout在该条件内.执行条件不一定是真的吗?
我从教程中得到了这个例子,并没有自己写出来,这就是为什么我有点困惑.
有人可以解释有两种不同类型的while循环的目的吗?我是编程新手.如果可能的话,还提供带有正确while循环的示例情况.
我理解如何使用while循环.这就是我所做的:
bool myBool = true;
int i = 0;
while (myBool) {
if (i > 10) {
myBool = false;
}
i = i + 1;
}
Run Code Online (Sandbox Code Playgroud) 目前我遇到了这个问题... while循环.
do {
// program code here
cout << "Would you like to run the program again?(yes/no)";
bool exit = false;
string strexit;
do {
getline(cin, strexit);
if (strexit == "no") {
exit = false;
break;
}
else if (strexit == "yes") {
exit = true;
}
else {
cout << "Enter yes to rerun the program, and no to exit.\n";
};
} while (!exit);
system("cls");
} while (exit);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在网上研究过,如何摆脱do ... while循环,当条件为真时,它会再循环回来,但如果它是假的,它会退出.因此,如果您查看代码,如果用户键入no,则设置exit = false,这将从更大的do while循环中取出,其中break将其从当前do while循环中取出.如果用户输入yes,则将exit更改为true,这会将其从当前的do …
确实很简单.
int main()
{
int n,i,j;
n = 20;
i = 0;
char ch[8];
do
{
ch[i] = (n%2) + '0';
n /= 2;
// SIMPLE WAY
if(n != 0)
i++;
}
while(n != 0);
for(j=0; j<=i; j++)
{
printf("%c",ch[i-j]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我不喜欢这种方式
我尝试了下面的方法,但代码很糟糕
int main()
{
int n,i,j;
n = 20;
i = 0;
char ch[8];
do
{
ch[i] = (n%2) + '0';
n /= 2;
}
while(n != 0 && i++); // THIS
for(j=0; j<=i; j++) …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写将指针变量分配给数组索引的代码。这是代码:
#include <stdio.h>
int main()
{
int x;
int iChoice;
int *iArray[4];
int iNumber;
*iArray = iNumber;
for (x = 0; x < 5; x++) {
iArray[x] = '\0';
}
do {
system("clear");
printf("\n\t\tENTER NUMBERS INTO AN ARRAY\n");
printf("\nArray element #1: %d", iArray[0]);
printf("\nArray element #2: %d", iArray[1]);
printf("\nArray element #3: %d", iArray[2]);
printf("\nArray element #4: %d", iArray[3]);
printf("\nArray element #5: %d", iArray[4]);
printf("\n\nEnter an array number to access: ");
scanf("%d", &iChoice);
} while (iChoice = NULL);
do {
printf("\n\t\tEnter …Run Code Online (Sandbox Code Playgroud) 这是代码
$get_favory = mysql_query("SELECT * FROM favory WHERE user1='$username'");
$values = "";
while($fav = mysql_fetch_assoc($get_favory)) {
$user2 = $fav['user2'];
$values = " OR added_by='".$user2."'";
}
echo $values;
Run Code Online (Sandbox Code Playgroud)
值变量必须是这样的:OR added_by='zac' OR linked_by='john' OR linked_by='emily'
但我得到这个结果:OR linked_by='zac'
我必须做什么?
我正在尝试创建一个插入一串字符的程序,验证它,然后对其进行排序并将其打印出来.
我确定这里有一个明显的逻辑错误,有人可以帮忙指出来吗?我花了好几个小时盯着我的屏幕.在我对C++的有限知识中,我尝试了所有我知道的东西,但是我仍然无法使用它.
你能提供的任何东西都会以某种方式帮助我,即使它是居高临下的.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void mySort(string &s);
int main()
{
string str;
char c;
bool invalid = true;
cout<<"Please enter some alphabetical characters:"<<endl;
cout<<"(* to end input): ";
do
{
getline(cin, str, '*');
for(int i = 0; i < str.length(); i++)
{
c = str.at(i);
}
if(! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) )
{
cout<<"Error!"<<endl;
} …Run Code Online (Sandbox Code Playgroud) 我知道它做了什么,以及它如何与while循环合作,但是不会有一个while循环代码是相同的,无论是否存在?