这是第一次发布海报,但是整个季度我一直在使用stackoverflow来帮助我完成C#类的介绍.一般来说,如果我看起来很努力,我可以找到我正在寻找的东西,但是我找不到任何已经回答过我问题的人.
我的作业要求我在5x10数组中显示随机数.然后我需要计算数字和平均值之和,但我稍后会担心.
随机数应<0且<= 100.控制台输出应该看起来像这样
x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
Run Code Online (Sandbox Code Playgroud)
只有10行而不是5行.
但是,我当前的代码是列出彼此相邻的随机数,并在它到达行尾时包裹行,就像这样
x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x
x x x x ...etc
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能使它正确对齐?我已经非常厌倦了使用格式修饰符的能力,所以当你看到时{0, 5},这只是我最近的尝试,但远非我的唯一.我对C#非常陌生,所以任何高级技术都是不可能的,因为我不明白如何正确使用它们.有什么想法吗?!
using System;
namespace DilleyHW7
{
class …Run Code Online (Sandbox Code Playgroud) 我有一个代表不同时间的元组列表
timeList = [('4', '12', 'PM'), ('8', '23', 'PM'), ('4', '03', 'AM'), ('1', '34', 'AM'),
('12', '48', 'PM'), ('4', '13', 'AM'), ('11', '09', 'AM'), ('3', '12', 'PM'),
('4', '10', 'PM')]
Run Code Online (Sandbox Code Playgroud)
我希望从列表中返回最大值,经过一些搜索后,我意识到我可以使用最大键来首先搜索AM或PM.
print(max(timeList, key = operator.itemgetter(2)))
然而,当我运行这个时,我得到了错误的最大值,('4', '12', 'PM')
我想到了它,不仅没有意义,因为8:23应该是最大值,但我也意识到12:48可能会返回最大值,因为它是在我的搜索中,PM和技术上大于8.
话虽如此,如果无法更改列表的格式,我怎么能得到最大值才能找到最新的可能时间.
当重载postfix操作符时,我可以做一些简单的事情
Class Foo
{
private:
int someBS;
public:
//declaration of pre &postfix++
Foo operator++();
//rest of class not shown
};
Run Code Online (Sandbox Code Playgroud)
前缀不需要采用任何参数,因此当我定义它时,类似于
Foo Foo::operator()
{
someBS ++;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
这对我来说很有意义.
当我去定义postfix重载时,我必须包含一个伪int参数
Foo Foo::operator++(int)
{
Foo temp = *this;
someBS ++;
return temp;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么?我不会在方法中使用它.前缀运算符不需要一个.返回temp值的后缀不依赖于伪参数.我知道,如果我想重载一个后缀运算符,它是如何完成的,我只是想知道背后的原因.
我通过弹出堆栈并将弹出内容与查看内容进行比较来检查堆栈是否已排序。如果 pop 大于 peek,我们就知道这两个元素是有序的。只要堆栈不为空,我就会运行这个循环。
我遇到的问题是堆栈的最后一个元素。我做了最后一次弹出操作,但它试图查看空堆栈以确保它是有序的。由于那里什么都没有,所以我收到运行时错误。
public static boolean isSorted(Stack<Integer> s){
boolean result = true;
while(!s.empty()){
if(s.pop() < s.peek()){
result = false;
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我尝试专门使用 Stack 来完成此操作,因此仅使用推送、弹出和查看。ArrayList 中没有任何内容。如何解决这个问题,同时仍然检查每个元素?
我尝试将 pop 存储在临时变量中,但这没有解决任何问题。不确定我希望什么
我上课NumberArray了NumberArray.h
class NumberArray
{
private:
double *aPtr;
int arraySize;
public:
NumberArray(int size, double value);
// ~NumberArray() { if (arraySize > 0) delete [ ] aPtr;}
//commented out to avoid problems with the
//default copy constructor
void print() const;
void setValue(double value);
};
Run Code Online (Sandbox Code Playgroud)
当我去NumberArray.cpp中编写print函数时
void NumberArray::print()
{
for (int index = 0; index < arraySize; index++)
cout << aPtr[index] << " ";
}
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误
声明与"void NumberArray :: print()const不兼容"
有什么想法,我可能会出错吗?其余的构造函数和类函数工作正常.
我有一个ShoppingCart使用ItemOrder对象的类.
public class ShoppingCart extends ArrayList<ItemOrder> {
Run Code Online (Sandbox Code Playgroud)
我有一个方法,检查一个对象是否已经存在于数组列表中,如果是这样,用新对象替换索引,如果没有添加新对象.
public boolean add(ItemOrder order){
if (super.indexOf(order) != -1){ //ITEM ALREADY IN LIST, REPLACE
super.remove(super.indexOf(order));
super.set(super.indexOf(order), order);
}
else //ITEM NOT IN LIST, ADD
super.add(order);
return true; //TO SATISFY RETURN TYPE
}
Run Code Online (Sandbox Code Playgroud)
当我编写方法时,我没有返回类型.我收到一个错误,建议我将返回类型设置为void,所以我这样做了.然后我得到另一个错误,返回类型必须是布尔值.我唯一的问题是方法本身并不是(至少对我来说)一个真/假的回报.我需要做的就是添加/替换ArrayList中的对象,而不是返回任何内容.
为什么这个方法需要一个布尔返回类型,并且我在最后简单地投入是安全return true;的,还是我真的应该使用它?
当for循环由ArrayList的大小控制时
(...; i < someArrayList.size(); ...)
Run Code Online (Sandbox Code Playgroud)
但循环的每次迭代都是在列表中添加一个元素,然后循环如何识别大小正在变化?我认为一旦代码被发送到编译器,它将看到大小一次然后执行直到它达到原始大小,但显然不是这种情况.随着大小的增加,循环运行的次数也会增加.我认为它不会,所以显然我不明白计算机是如何读取它的.我只是想知道系统如何读取这样的东西.
另外,是否有某种方法可以使循环只运行原始列表大小的次数(每次迭代再次添加元素)?假设你事先不知道大小,所以你不能输入一些常数.
我找到了一个f#递归函数的基本示例,它接受一个列表并返回一个只有偶数整数的列表.我在很大程度上理解它,但有一点我很困惑.
let numbers = [1..4]
let rec even ls =
match ls with
| [] -> []
|head :: tail when head % 2 = 0 -> head :: even tail
|_::tail -> even tail
Run Code Online (Sandbox Code Playgroud)
匹配头的行让我感到困惑.这就是我读它的方式.当头部均匀时,头尾相接,然后even tail再次呼叫.因为我们一对一地追加尾巴,难道不会被一次又一次地加入头部的循环陷入困境吗?
此外,_::tail我假设的最后一行意味着"什么都不做,再次递归",但我_在f#中查找运算符并且它说它是一个通配符模式.这实际上意味着如果我的前两场比赛没有覆盖,那么这样做吗?
希望有人能澄清!f#看起来很有趣,但是来自强制性的背景,很难理解.
我今天晚上一直在使用f#中的列表(创建,添加,搜索等),并且最近卡在列表项删除上.代码很简单.
let menu = [("pizza",17);("hotdog",5);("burger", 12);("drink",3);
("milkshake",4)]
//If key is in dictionary , return new dictionary with value removed
//otherwise return dictionary unchanged
let rec remove dict key =
match dict with
//if the list is empty, return an empty list
| [] -> []
//if the list is not empty and the head meets the removing criteria
//return a list obtained by reiterating the algorithm on the tail
//of the list
| (k,v) :: tl when k = key …Run Code Online (Sandbox Code Playgroud) 我有Class NumDays如下所示:
Class NumDays
{
private:
double hours;
public:
NumDays() { hours = 0.0; } //default constructor
NumDays(double hr) { hr = hours; } //initializing constructor
//Large class, nothing of importance, rest of class omitted
//overloading << operator
friend ostream &operator<<(ostream &out, NumDays a);
}
Run Code Online (Sandbox Code Playgroud)
我有NumDay.cpp 这包括:
ostream &operator<<(ostream& out, NumDays a)
{
// takes amount of hours, computes to work days
int temp = a.hours / 8;
//gives remainder of hours after full 8 hr workday. …Run Code Online (Sandbox Code Playgroud)