我设计了一个简单的程序,它有一个计数器类,在计数器类里面,我有一些方法可以增加计数,减少计数,等等.然后我向用户展示一个菜单,让他们输入他们的选择,例如.进入1方案一,等好了,我再也不想计数为负,因此处理这个我有计数器类的我SubtractCount方法抛出ArguemetOutOfRangeException当计数<0.
然后我的switch语句捕获该异常(如果发生).我的问题是:使用异常来确保计数永远不会是负数是不是很糟糕?我应该采用不同的方式吗?或者更好的是,有没有更好的方法来实现这一目标?
代码段:
static void Driver()
{
switch (userChoice)
{
case 1: // if user picks a 1, the count is deincremented
try {
myObjectOfCounterClass.SubtractCount();
}
catch (ArguemetOutOfRangeException ex)
{
console.writeLine(ex.message);
}
break;
// case 2, case 3 etc.
}
class Counter
{
private int count;
public void SubtractCount()
{
if (count < 0)
throw new ArguementOutOfRangeException("The count can never be negative!");
else
count--;
}
Run Code Online (Sandbox Code Playgroud) 我有第一个OOP课程的作业,我理解所有这些,包括以下声明:
您应该创建一个名为ComplexNumber的类.此类将包含定义为双精度的私有数据成员中复数的实部和虚部.您的类应包含一个构造函数,该构造函数允许将虚数的数据成员指定为构造函数的参数.默认(非参数化)构造函数应将数据成员初始化为0.0.
当然我知道如何在不将它们链接在一起的情况下创建这些构造函数,并且赋值不需要链接它们,但我想按照我的意愿.
没有将它们链接在一起,我的构造函数看起来像这样:
class ComplexNumber
{
private double realPart;
private double complexPart;
public ComplexNumber()
{
realPart = 0.0;
complexPart = 0.0
}
public ComplexNumber(double r, double c)
{
realPart = r;
complexPart = c;
}
// the rest of my class code...
}
Run Code Online (Sandbox Code Playgroud) 我已经参加了2个OOP C#课程,但现在我们的教授正在转向c ++.所以,为了习惯c ++,我写了这个非常简单的程序,但我一直收到这个错误:
error C2533: 'Counter::{ctor}' : constructors not allowed a return type
我很困惑,因为我相信我已经将我的默认构造函数编码为正确.
这是我的简单计数器类的代码:
class Counter
{
private:
int count;
bool isCounted;
public:
Counter();
bool IsCountable();
void IncrementCount();
void DecrementCount();
int GetCount();
}
Counter::Counter()
{
count = 0;
isCounted = false;
}
bool Counter::IsCountable()
{
if (count == 0)
return false;
else
return true;
}
void Counter::IncrementCount()
{
count++;
isCounted = true;
}
void Counter::DecrementCount()
{
count--;
isCounted = true;
}
int Counter::GetCount()
{
return count;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我没有指定返回类型.或者我是某种程度?
在c ++中是否可以计算目录中具有给定扩展名的文件数?
我正在编写一个程序,在这里做这样的事情会很好(伪代码):
if (file_extension == ".foo")
num_files++;
for (int i = 0; i < num_files; i++)
// do something
Run Code Online (Sandbox Code Playgroud)
显然,这个程序要复杂得多,但是这应该让你对我正在尝试做的事情有了一般的了解.
如果这不可能,请告诉我.
谢谢!
I was reading about regular expressions (I'm a regex newbie, but want to learn them) and came across this regex:
/^(?!http:\/\/www.google.com).*/
Run Code Online (Sandbox Code Playgroud)
and I didn't know what or when it would match...so my question is just that, what/when would this regex match?
Thanks for helping out a regex padawan!
我想从firefox扩展中触发事件,特别是单击事件.我已经尝试过jQuery .click()
以及整个:
var evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, false );
toClick[0].dispatchEvent(evt);
Run Code Online (Sandbox Code Playgroud)
这对我不起作用,我想知道这是否可行?(从firefox扩展触发事件)?
我认为这可能与我创建活动的文件有关......但我不确定.
如果是这样,那怎么做呢?
我有一个文件存储一类学生的考试成绩.我正在尝试编写一个程序,该程序可以成功打开文件,读取考试分数,找到平均分数,最高分数和最低分数,并打印出来.平均分数应在小数点后打印2位数.
这是我到目前为止:
static void Main()
{
string myData = "";
int temp = 0;
int max = 0;
int min = 0;
double average = 0;
StreamReader fileReader = new StreamReader("data.txt");
do
{
myData = fileReader.ReadLine();
if (myData != null)
{
max = int.Parse(myData);
temp = int.Parse(myData);
if (temp > max)
temp = max;
}
} while (myData != null);
fileReader.Close();
Console.ReadLine();
}//End Main()
Run Code Online (Sandbox Code Playgroud)
我不知道如何继续.如何读取新行并将其分配给temp?我不认为我做得对.
嗨,我有一个简单的问题,但是一直困扰我一段时间.
题:
在C#中使用switch语句时,使用enums
over constants
或者反之亦然?或者这是一个偏好的问题?我问这个是因为很多人似乎喜欢使用enums
,但是当你打开一个int
值时,你必须将每个包含的值转换enum
成一个int
,即使你指定了它的类型enum
.
代码片段:
class Program
{
enum UserChoices
{
MenuChoiceOne = 1,
MenuChoiceTwo,
MenuChoiceThree,
MenuChoiceFour,
MenuChoiceFive
}
static void Main()
{
Console.Write("Enter your choice: ");
int someNum = int.Parse(Console.ReadLine());
switch (someNum)
{
case (int)UserChoices.MenuChoiceOne:
Console.WriteLine("You picked the first choice!");
break;
// etc. etc.
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以创建一个实例enum
并将整个enum
转换为int?
谢谢!
我只是将构造函数链接起来的概念,但我无法弄清楚如何将这两个特定的构造函数链接在一起,所以如果有人可以帮助我,我将不胜感激.
谢谢!
构造函数
// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Line()
{
startPoint.xCoord = 0;
startPoint.yCoord = 0;
endPoint.xCoord = 0;
endPoint.yCoord = 0;
}
// parameterized constructor
// purpose: initialize data members to p1 and p2
// Parameters: Point objects p1 and p2
// returns: none
public Line(Point p1, Point p2)
{
startPoint = p1;
endPoint = p2;
}
Run Code Online (Sandbox Code Playgroud) 我是第二个OOP课程的编程学生,用C++教授.我知道在代码中使用魔术数字通常是不好的做法,所以这是我的问题:
在接下来的节目,我必须写这个类,还有就是在税表给我们120号,我们需要用它们来计算税收和其他相关信息.如此庞大的数字,我为每个数字定义一个常量吗?或者我还能做些什么吗?
您可以提供的任何帮助将不胜感激.
我有一个具有tableView,detailView,flipView和moreDetail View的应用程序.它很好地通过过渡直到第26个元素.当我点击导航到flip和moreDetailView的按钮时,应用程序崩溃了.
我收到错误消息:[NSIndexPath row] message sent to deallocated instance
.
我想我可能会多次调用indexPath,但为什么一切都运行良好直到第25个元素然后停止工作?NSIndexPath是如何解除分配的?我从来没有解除分配.
如果您知道,请帮忙.谢谢!!!
Xcode说问题在这里:
@implementation produceView aka *detailView*
- (IBAction)showInfo {
FlippedProduceView *fView = [[FlippedProduceView alloc]initWithIndexPath:index];
fView.flipDelegate = self;
fView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:fView animated:YES];
[fView release];
}
- (IBAction) buttonPushed:(id)sender
{
picView *pictureView = [[picView alloc]initWithIndexPath:index];
pictureView.picDelegate = self;
pictureView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:pictureView animated:YES];
[pictureView release];
}
-(id)initWithIndexPath:(NSIndexPath *)myIndexPath {
if (self == [super init]) {
path = myIndexPath;
}
return self;
}
@implementation picView aka *moreDetailView* …
Run Code Online (Sandbox Code Playgroud) 我需要提出的算法是计算用户输入数字的所有因子,例如用户输入"50",程序将显示50的所有因子:1,2,5, 10,25,50.
该程序需要适用于所有正整数值.
有人愿意告诉我该怎么做吗?这不是家庭作业,它只是学习材料,而且我一直在努力解决这个问题超过2小时.我应该使用数组吗?
我为最近的一个学校项目编写了一个使用指针的简单字符串标记化程序。但是,我的StringTokenizer::Next()
方法遇到了问题,该方法在调用时应该返回指向 char 数组中下一个单词的第一个字母的指针。我没有收到编译时错误,但我收到了一个运行时错误,其中指出:
Unhandled exception at 0x012c240f in Project 5.exe: 0xC0000005: Access violation reading location 0x002b0000.
Run Code Online (Sandbox Code Playgroud)
该程序当前标记字符数组,但随后停止并弹出此错误。我有一种感觉,这与NULL
我在我的Next()
方法中所做的检查有关。
那么我该如何解决这个问题?
另外,如果您发现我可以更有效地或通过更好的练习做的任何事情,请告诉我。
谢谢!!
StringTokenizer.h:
#pragma once
class StringTokenizer
{
public:
StringTokenizer(void);
StringTokenizer(char* const, char);
char* Next(void);
~StringTokenizer(void);
private:
char* pStart;
char* pNextWord;
char delim;
};
Run Code Online (Sandbox Code Playgroud)
StringTokenizer.cpp:
#include "stringtokenizer.h"
#include <iostream>
using namespace std;
StringTokenizer::StringTokenizer(void)
{
pStart = NULL;
pNextWord = NULL;
delim = 'n';
}
StringTokenizer::StringTokenizer(char* const pArray, char d)
{
pStart = pArray;
delim = d; …
Run Code Online (Sandbox Code Playgroud) c# ×6
c++ ×4
constants ×2
constructor ×2
file-io ×2
.net ×1
directory ×1
dom-events ×1
enums ×1
exception ×1
iphone ×1
javascript ×1
nsindexpath ×1
pointers ×1
regex ×1
tokenize ×1
transitions ×1