这是“如何像计算机科学家一样思考”的练习。我正在学习 Python/编程,但不确定如何完成此任务。
这是书中的一个例子,向前显示字母,我不知道如何获得相反的效果。必须使用while循环。
fruit = 'banana'
index = 0
while index > len(fruit):
letter = fruit[index]
print letter
index = index + 1
Run Code Online (Sandbox Code Playgroud) while ($topic = mysql_fetch_assoc ($result)); {
echo "{$topic["overtag "]} ";
}
Run Code Online (Sandbox Code Playgroud)
我的 while 循环的结果显示为:apples orange 香蕉
我希望能够获取所有这些结果并将它们放入一个变量中并使其看起来像这样:$fruits = apples orange 香蕉
我怎么能做到这一点?
是否可以在 C++ 中进行 while 循环,直到小数达到一定精度?我想对 arctan 和 pi 进行长时间的计算,并且我希望循环运行,直到使用公式 pi=4(arctan (1.0)) 将 pi 计算到小数点后第 10 位。我正在使用泰勒级数公式手动计算反正切。我知道这些计算有内置函数,但这是一项家庭作业,所以我必须这样做。我不是在寻找问题的解决方案,只是寻找使用精度的循环是否可行。谢谢!
编辑:
我仍然坚持这个!即使在仔细阅读了每个人的提示之后,我也无法为 while 循环提出正确的论点。真的需要帮助。这是我想出的代码:
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int i;
long double result;
long double pi;
int y=3;
int loopcount=0;
long double precision;
cout<<"Start\n";
result=1-(pow(1,y)/y);
do
{
y=y+2;
result=result+(pow(1,y)/y);
y=y+2;
result=result-(pow(1,y)/y);
pi=4*(result);
precision=(pi*(pow(10,11))/10);
loopcount++;
}
while(//This is the problem!);
cout<<"Final Arctan is:"<<endl;
cout<<setprecision(20)<<result<<endl;
cout<<"Final Pi is:"<<endl;
cout<<setprecision(9)<<pi1<<endl;
cout<<"Times looped:"<<endl;
cout<<loopcount<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个从数据库中检索值的代码。但我想以相反的顺序获取这些值。见这个例子
While($row=mysql_fetch_assoc($rs)){
echo $row['id']."<br>";//gives 1 and 2 and so on
echo $row['val']."<br>";// gives abc and def and so on
}
Run Code Online (Sandbox Code Playgroud)
但我想要
2
def
1
abc
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点。我不想像使用 ORDER BY 那样使用查询。所以我可以在 PHP End 上控制它吗??
我编写了一个程序,它从一个文件中读取文本并将其复制到一个新文件中。使用 while 循环和 ReadFile/Writefile 函数,我的程序可以工作……但我的程序不会停止运行,除非我强制停止它。我猜我没有正确关闭我的句柄,或者我的 while 循环可能设置错误。一旦我强制停止我的程序,文件就会以新名称成功复制到新位置。
int n = 0;
while(n=ReadFile(hFileSource, buffer, 23, &dwBytesRead, NULL)){
WriteFile(hFileNew, buffer, dwBytesRead, &dwBytesWritten, NULL);
}
CloseHandle(hFileSource);
CloseHandle(hFileNew);
return 0;
Run Code Online (Sandbox Code Playgroud) while ($_ ne ' \n')不检测输入是否为空行并在 while 中不确定地循环。为什么?
我对 C++ 很陌生,我现在正在试验线程。我正在尝试在 while 循环下的线程内创建一个线程。但我认为它似乎不起作用。目前我的代码如下所示:
#include <>
std::vector<pthread_t> outer_thread, inner_thread;
void *inner_thread(void *ptr)
{
string data1;
data1 = *(reinterpret_cast<string*>(ptr));
cout << "inner thread started " << data1;
/* do something */
cout << "inner thread stopped " << data1;
pthread_exit(NULL);
return 0;
}
void *outer_thread(void *ptr )
{
cout << "out thread started" << endl;
//cout << ptr << endl;
//cout << *(reinterpret_cast<string*>(ptr)) << endl;
string data;
data = *(reinterpret_cast<string*>(ptr));
string str3;
while (getline(data,str3))
{
cout << "out thread started" …Run Code Online (Sandbox Code Playgroud) 我已经用 while 循环尝试了几件事,但似乎无法让它工作。我想一直请求用户输入,直到用户输入数字 0,这是我到目前为止的代码:
import java.util.*;
public class Task10 {
public static void main(String[] args) {
System.out.println("Enter a year to check if it is a leap year");
Scanner input = new Scanner(System.in);
int year = input.nextInt();
if ((year % 4 == 0) || ((year % 400 == 0) && (year % 100 != 0)))
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
}
}
Run Code Online (Sandbox Code Playgroud) 我试图在文件中输入一些数字,因此创建了一个 while 循环。输入值后,我询问用户是否希望重复该过程。但是,程序只打印第二个 printf 语句,while 循环终止。该计划是:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
fp = fopen("numbers.txt", "w");
int a;
char ch = 'y';
while(ch == 'y')
{
printf("Enter the number\n");
scanf("%d", &a);
fprintf(fp, "%d", a);
printf("Do you want to continue?");
scanf("%c", &ch); //doesn't work at all
}
fclose(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么第二个 scanf 不起作用?
我正在做Udemy课程,并试图完成一个练习,要求用户输入数字,或者如果我写“ quit”,则中断循环。循环后,我必须对所有数字求和。
我几乎可以正常工作了,但是我不得不写两次 “退出” 才能使其崩溃,但我不明白为什么。感谢任何帮助
int[] total = new int[10];
int number;
int counter = 0;
Console.WriteLine("Write up to 10 number or 'quit' to exit early");
while (counter < 10)
{
bool success = int.TryParse(Console.ReadLine(), out number);
if (success == true)
{
total[counter] = number;
counter++;
} else if (Console.ReadLine() == "quit")
{
break;
} else
{
Console.WriteLine("Wrong input.");
}
}
int sum = total.Sum();
Console.WriteLine("The sum is {0}", sum);
Run Code Online (Sandbox Code Playgroud)