抛硬币.成功,你赢了100,否则你输了50.你会继续玩,直到你的口袋里有钱a.如何a存储任何迭代的值?
a <- 100
while (a > 0) {
if (rbinom(1, 1, 0.5) == 1) {
a <- a + 100
} else {
a <- a - 50
}
}
Run Code Online (Sandbox Code Playgroud)
作为最终结果,当while循环结束时,我希望能够查看a每次迭代的值,而不仅仅是最终结果.我查阅了关于计算迭代次数的帖子,但我无法将其应用于此案例.
我正在运行一个while循环,它将使用用户输入的浮点数并将它们添加到一起,直到在输入中放入其他内容.
while(cin >> input)
{
price += input;
cout << "Next price: ";
}
Run Code Online (Sandbox Code Playgroud)
在此之后,我有用户输入他们支付了多少:
cout << "Enter the amount paid: ";
cin >> paid;
cout << "You paid: " << paid << endl;
Run Code Online (Sandbox Code Playgroud)
在运行程序时,循环执行正常,我可以cout正确地得到总价格,但它会跳过该cin >> paid;行并输出为You paid: nan.
我尝试cin.clear()在循环和下一个输入之间放置,但是在运行它时,输出是:You paid: 0
我是否需要更改while循环或使用其他版本来清除输入?我是C++的新手.谢谢!
我有以下代码,不幸的是它循环无限但我无法理解为什么:
$tot_age is 设置为604245119
while ($tot_age > 536467742) {
$tot_age - 31536000;
if ($tot_age < 536467742 ) {
// do something
break;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我在这里尝试的是以下内容.如果$tot_age大于17年,则从$ tot_age迭代循环并减去12个月.然后我试图在$ tot_age少于17年的时候突破循环.我也会在这里应用一些逻辑.
谁能在这里看到问题?谢谢
我有一个读取文件的While循环.问题是它读取1行,然后跳过1行,读取,跳过等等.我无法弄清楚是什么导致它跳过.如果有人能告诉我'那太棒了(:
StreamReader sentencesFile = new StreamReader(@"C:\Users\Jeroen\Desktop\School\C#\opwegmetcsharp\answersSen.txt");
string line;
while ((line = sentencesFile.ReadLine()) != null)
{
string SentenceFileString = sentencesFile.ReadLine();
string keyWords = line.Substring(0, line.IndexOf(' '));
string sentence = line.Substring(line.IndexOf(' ') + 1);
string testOutput= keyWords + sentence;
}
Run Code Online (Sandbox Code Playgroud) 我一直试图修复这个循环,但显然它无法识别输入是否正确.我在这里使用正确的循环吗?
var userChoice = prompt("Do you choose rock, paper or scissors?")
if (userChoice !== "rock", "paper", "scissors") {
do {
prompt("Invalid answer. Please choose rock, paper or scissors.");
} while (userChoice !== "rock", "paper", "scissors");
} else {
Run Code Online (Sandbox Code Playgroud) 我不明白这段代码.
var timesTable;
while ((timesTable = prompt("Enter the times table", -1)) != -1) {.....}
Run Code Online (Sandbox Code Playgroud)
为什么有必要在之前声明变量?我试着这样做:
while ((var timesTable = prompt("Enter the times table", -1)) != -1) {.....}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有什么问题?是关于范围的吗?
完整的计划是:
function writeTimesTable(timesTable, timesByStart, timesByEnd) {
for (; timesByStart <= timesByEnd; timesByStart++) {
document.write(timesTable + " * " + timesByStart + " = " + timesByStart * timesTable + "<br />");
}
}
var timesTable;
while ((timesTable = prompt("Enter the times table", -1)) != -1) {
while (isNaN(timesTable) == true) {
timesTable …Run Code Online (Sandbox Code Playgroud) 我会比较一个字符,但它实际上不起作用:
//...
System.out.print("PROCEED? (Y/N):\t");
line = in.nextLine();
ch = line.charAt(0);
while (ch != 'y' || ch != 'n' || ch != 'Y' || ch != 'N'){
System.out.print("NON-VALID INPUT. TYPE Y-N:\t");
line = in.nextLine();
ch = line.charAt(0);
}
//...
Run Code Online (Sandbox Code Playgroud)
在我的终端上,当我按y或时n,结果是:
PROCEED? (Y/N): y
NON-VALID INPUT. TYPE Y-N: y
NON-VALID INPUT. TYPE Y-N: Y
NON-VALID INPUT. TYPE Y-N: n
NON-VALID INPUT. TYPE Y-N: r
NON-VALID INPUT. TYPE Y-N: d
...
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
import java.util.*;
public class MyAccount {
public static double balance = 0.0;
public static double deposit(double deposit){
return balance += deposit;
}
//public void setBalance(double balance){
// this.balance = balance;
//}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String redo = "";
do{
System.out.println("What do you want to do today?");
String answer= in.nextLine();
if(answer.equals("deposit")){
System.out.println("How much do you want to deposit?");
double money = in.nextDouble();
deposit(money);
}
System.out.println("Your balance is " + balance);
System.out.println("Anything else(Y …Run Code Online (Sandbox Code Playgroud) 我为Brian Kernighan的" The C Programming Language " 练习1-19编写了一个简单的解决方案,它可以反转一个字符串.该reverse(char[])功能是如下,一切都与它的罚款;
void reverse(char src[])
{
int len = 0;
while( src[len] != '\0')
len++;
for(int i = 0; i < len/2; i++)
{
char temp = src[i];
src[i] = src[len - 1 - i];
src[len - 1 - i] = temp;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在while循环中使用postfix increment(++)运算符,则函数会失败;
void reverse(char src[])
{
int len = 0;
while( src[len++] != '\0') // this fails
;
for(int i = 0; i …Run Code Online (Sandbox Code Playgroud) 所以我仍然在学习Python,我很难处理while循环.我有一个代码示例,包括while循环if和else语句.我想要它做的是打印'小于2'和'大于4'这样做,但它继续运行.它不打印一次,这是我想要它做的.任何帮助将不胜感激!
counter = 1
while (counter < 5):
count = counter
if count < 2:
counter = counter + 1
else:
print('Less than 2')
if count > 4:
counter = counter + 1
else:
print('Greater than 4')
counter = counter + 1
Run Code Online (Sandbox Code Playgroud)