根据谷歌C++样式指南,"当忽略返回值时,'pre'形式(++i)的效率永远不会低于'post'形式(i++),并且效率通常更高."
该指南继续解释原因,但我不完全理解.思考?也许有人可以提供这个概念的例子?
所以我正在尝试创建一个程序,用户输入其学生的年龄,直到它进入-1.在-1之后,程序必须计算学生人数和平均年龄.出于某种原因,我无法摆脱do-while循环.太头疼了!无论如何,这是代码
提前致谢.
public static void main(String[] args) {
// Input
Scanner input = new Scanner(System.in);
// Variables
int escapeNumber = 0;
int[] studentsAge = new int[50];
do {
// Input
System.out.println("Student's age (Type -1 to end): ");
// Set escapeNumber to what the user entered to break the while loop
escapeNumber = input.nextInt();
// Populate the array with the ages (Cannot be a negative number)
if (escapeNumber > 0) {
for (int arrayPos = 0; arrayPos < studentsAge.length; arrayPos++) { …Run Code Online (Sandbox Code Playgroud) 在用户尝试猜测数字 7 次后,我无法停止 do-while 循环。我不确定如何告诉循环停止并显示超时消息。
谢谢,我感谢任何帮助!
代码:
import java.util.Random;
import java.util.Scanner;
public class numberGuesses {
public static void main (String [] args){
Random rand = new Random();
int inputNumber;
int randomNumber = rand.nextInt(100) + 1;
int guessingLimit = 7;
System.out.println("Rand Number = " +randomNumber); //Temp to test program with correct number
Scanner scan = new Scanner (System.in);
do {
System.out.print("Please enter a number: ");
inputNumber = scan.nextInt();
if (inputNumber > randomNumber)
System.out.println("That's too high!");
if (inputNumber < randomNumber)
System.out.println("That's too …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) 我正在尝试编写一个简单的骰子滚动游戏,如果骰子匹配,那就是胜利,如果它们是一个数字,它是一个平局(junker),如果它们既不满足这些条件,那也是一种损失.
我正在使用do while循环,并且似乎无法获取局部变量来初始化:
import java.util.Scanner;
public class Program06
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String response = "k";
int d1 = 1;
int d2;
int win;
int lose;
int junker;
System.out.println("Welcome to Computer Dice");
System.out.println("---------------------------");
System.out.println("\nYou will first roll your dice");
System.out.println("Next the outcome of your roll will be determined:");
System.out.println("Any pair and you Win");
System.out.println("Anything else and you Lose");
System.out.println("\n----------------------------");
System.out.println();
do
{
System.out.println("Player");
System.out.println("----------");
d1 = (int)(Math.random() * 6) + 1;
d2 …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试创建一个程序,该程序仅使用 for、do while 和 ifs 来打印从 0 到 10,000 的素数。我创建了这个程序,但它没有运行
static void Main(string[] args)
{
for (int x = 2; x < 10000; x++)
{
for (int y = 1; y < x; y++)
{
if (x % y != 0)
{
Console.WriteLine(x);
}
}
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
我不知道问题出在哪里,也不知道 for inside 是否重置。
所以我正在开发一种算法来在线程之间均匀分配工作,但它有点独特.正在进行的工作是文件比较,其中我们有一个文件列表(比方说10个文件).我们不希望将文件1与文件1进行比较,因此每个文件都会跳过比较自身.文件1将自己与文件2到10进行比较,文件2将自己与文件3到10等进行比较.因此,当我们向下移动文件比较列表时,文件数比较下降.这是我遇到问题的小片段(特别是do-while循环)
// Get first pass file chunks
printf("Split approximation: ");
for (int i = 0; i < num_threads; i++)
{
file_chunks[i] = (int)floor((double)num_file_compares / (double)num_threads);
printf("%d ", file_chunks[i]);
}
printf("\n");
// Adjust file chunks to contain all files
for (int i = 0; i < num_threads; i++)
{
file_chunk_sum = file_chunk_sum + file_chunks[i];
}
append_count = num_file_compares - file_chunk_sum;
printf("Need to append last %d\n", append_count);
append_index = num_threads;
do
{
printf("im in the do while...\n");
printf("append count is: %d\n", append_count); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个小脚本,但使用几个curl 和while 循环有点搞砸了。
我想在其中一个 URL 向我提供信息时停止处理curl。注意:我有多个卷曲请求。
所以我的理念是,
我有几个 URL,我必须对其进行处理并从中获取信息。如果在特定 URL 上找到信息,它将给我一个字符串。如果找不到信息,它就不会给我任何价值。所以我大约有近 10 个 URL 需要处理。在所有情况下,任何一个 URL 都会向我提供信息,因此其余 URL 将不会产生任何价值。由于要处理大量 URL,因此延迟是一个问题。因此,假设在下面的示例代码中,如果以 value2.php 结尾的 url 给了我一个结果,那么我立即想停止处理其他 URL。因为我已经得到了结果,没有必要运行其他卷曲。最后我必须打印结果。
另外,我遇到的情况是,所有 URL 都不会产生任何结果,如果有人向我展示如何处理该问题,那就太好了。
我的示例代码。
<?php
///functions here
do {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"example[dot]com/value1.php?process=$param");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$combined = curl_exec ($ch);
curl_close ($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"example[dot]com/value2.php?process=$param");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$combined = curl_exec ($ch);
curl_close ($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"example[dot]com/value3.php?process=$param");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$combined = curl_exec ($ch);
curl_close ($ch);
$ch = curl_init();
curl_setopt($ch, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个应该在正整数列表中读取的程序(包括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) 在我的代码中,我使用了一个宏,它应该生成一个日志条目,或者它不应该根据一些编译标志:
#ifdef SOMETHING
#define LOG(a) printf ("%s", a)
#else
#define LOG(a) while (0) {}
#endif
Run Code Online (Sandbox Code Playgroud)
这段代码运行良好,但是当我查看 Internet 时,我只看到有人在使用
#define LOG(a) do {} while (0)
Run Code Online (Sandbox Code Playgroud)
代替
#define LOG(a) while (0) {}
Run Code Online (Sandbox Code Playgroud)
这两种方法等价吗?我应该更喜欢一个吗?