我目前正在编写C++ Monty Hall问题模拟,并遇到了一些麻烦.我一直得到的错误是:
source.cpp(23): error C4700: uninitialized local variable 'doorReveal' used
source.cpp(25): error C4700: uninitialized local variable 'doorSwitch' used
source.cpp(52): error C4700: uninitialized local variable 'stayWin' used
source.cpp(56): error C4700: uninitialized local variable 'switchWin' used
Run Code Online (Sandbox Code Playgroud)
我似乎无法弄清楚出了什么问题.该项目应该通过首先在前100次尝试中保持原始门选择然后在接下来的100次尝试中显示门时切换来模拟胜利.谢谢大家的帮助.
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
int doorChoice;
int prizeDoor;
int doorReveal;
int doorSwitch;
int count;
int switchWin;
int stayWin;
srand((unsigned int) time(0));
for (count = 0; count <= 200; count++)
{
prizeDoor = (rand() % 3) + 1;
doorChoice …Run Code Online (Sandbox Code Playgroud) 当我试图编译它时,我在c ++中得到了这个错误"期望unqualified-id in int".
void yearlyWithdrawal(int startingAge, int numOfYears), int yearlyAmount, double interestRate)
{
int age = startingAge;
int lastAge = startingAge + numOfYears;
double cash = yearlyAmount;
cout << "Age | Yearly Plan" << endl;
cout << "----+----------------" << endl;
while (age <= lastAge)
{
cout.width(3);
cout << age << " | ";
cout.width(15);
cout.precision(2);
cout.setf(ios::fixed);
cout << cash << endl;
if (age != lastAge)
cash = cash + cash*interestRate / 100.0;
age++;
}
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
我试图找出问题所在但却无法找到.
我写了一个c ++程序如下,
#include <iostream>
using namespace std;
main()
{
int a,b,c;
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
c=a+b;
cout<<"Sum of two numbers are"<<c;
}
Run Code Online (Sandbox Code Playgroud)
并试图用以下程序编译程序,
home$ g++ add.cpp
Run Code Online (Sandbox Code Playgroud)
我收到这个错误,
程序g ++可以在以下包中找到,g ++,pentium-builder
有人能帮助我吗?
我想通过指向main函数的指针返回在本地函数中创建的数组.我的代码如下.返回数组,我只能按元素访问每个元素.下次它给出垃圾值.怎么了?
void DoWork(int** ppOut , int& nSize)
{
int m[5];
for(int i = 0 ; i < 5 ; i++)
{
m[i] = i;
}
nSize = 5;
cout << m[0] << endl;
cout << m[1] << endl;
cout << m[2] << endl;
cout << m[3] << endl;
cout << m[4] << endl;
*ppOut = &m[0];
//delete [] m;
}
void main()
{
int nSize = -1;
int i;
int* f = NULL;
DoWork(&f , nSize);
cout << f[3] << …Run Code Online (Sandbox Code Playgroud) 在这个文档http://www.cplusplus.com/reference/string/string/npos/ 中已经说过std :: string :: npos是-1.但是当我打印出价值时,它不是.这个价值架构是否依赖?
我的测试非常简单
std::cout << std::string::npos << std::endl;
Run Code Online (Sandbox Code Playgroud)
哪个输出
4294967295
Run Code Online (Sandbox Code Playgroud) 假设一个元组有一个整数:
tup = (19201,)
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种获得此输出的方法:
tup = "(19201)"
Run Code Online (Sandbox Code Playgroud)
到目前为止,我试过这个:
str_tup = "(" + tup[0] + ")"
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:
TypeError:无法隐式地将'int'对象转换为str
表达式if (mask1 | mask2) is None:返回此错误
TypeError: unsupported operand type(s) for |: 'int' and 'NoneType'
Run Code Online (Sandbox Code Playgroud)
如何检查两个变量之一是否为None?
下面是我的 C 代码,一个问题是当我尝试运行它时,它不会在切换后和输入输入后继续打印下一条语句。这是一个演讲厅/房间预订系统。如果你看到了,在 if else switch 语句之后有第 3 步。它不会打印出第 3 步。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char applicant_name[100], applicant_faculty[100], applicant_date[40], applicant_id[50], applicant_mobile_number[20];
int number_of_students, lecture_hall_book, lecture_room;
char lecture_hall[20], booking_date[20];
printf("\n=======================================================================================\n");
printf(" Author : Fathul Amin\n");
printf(" Matric ID : CB19070\n");
printf("=======================================================================================\n\n");
printf(" Universiti Malaysia Pahang\n\n");
printf(" Welcome to the Faculty of Computing (FCOM) Lecture Hall/Room Booking System\n\n");
printf(" For your information, this system was created to support\n");
printf(" government initiative to reduce paper usage by replacing …Run Code Online (Sandbox Code Playgroud) 返回第 N 个偶数(想象一个偶数列表,返回输入数字在人眼位置的数字)
示例(输入 --> 输出)
1 --> 0 (the first even number is 0)
3 --> 4 (the 3rd even number is 4 (0, 2, 4))
Run Code Online (Sandbox Code Playgroud)
(nth_even(3), --> 4)
(nth_even(1), --> 0)
(nth_even(2), --> 2)
(nth_even(100), --> 198)
(nth_even(1298734), --> 2597466)
Run Code Online (Sandbox Code Playgroud)
我的代码在处理更大的数字时超时,所以我需要一个更快的方法。
def nth_even(n):
data = [num for num in range(0, n*2) if num % 2 == 0]
return data[n-1]
Run Code Online (Sandbox Code Playgroud) 我遇到了解析字符串的问题; 我似乎无法找出为什么我的String Array的值不等于它们应该是什么!这可能看起来很奇怪,所以这里是代码的简化版本(应用程序要大得多,所以我只做了一个小程序来解释我的观点)
public class Test {
public static void main(String[] args) {
String src = "beep zap derp flop";
String delims = "[ ]+";
String[] tokens = src.split(delims);
if (tokens[0] == "beep") {
System.out.println("you said beep!");
} else {
for (int i = 0; i < tokens.length; i++) {
System.err.println("'" + tokens[i] + "'");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的输出是
'beep'
'zap'
'derp'
'flop'
Run Code Online (Sandbox Code Playgroud)
我知道令牌[0]发出嘟嘟声,但Java似乎没有注意到.有什么我做错了吗?