所以,我有一个项目,我的一个类方法应该popque前面的deque,mydeque。当我在main中调用one.play()并运行程序时,它不会弹出deque。该程序应该让用户输入他们想要的卡数,然后每次拉出一张卡时生成一个随机数,并比较卡上的值。如果随机数小于卡号,则分数增加1。但是为了使它工作,我需要弹出双端队列的前面,并且每次都将新号码与双端队列的新前线进行比较。
#include <iostream>
#include <deque>
using namespace std;
class Player {
private:
size_t cards;
deque<int> mydeque;
public:
string name;
int score;
Player(string player) {
name = player;
score = 0;
}
void recieve(size_t card){
mydeque.push_front (card);
}
int play() {
return mydeque.front();
mydeque.pop_front();
}
~Player() {
name = " ";
score = 0;
}
void tostring(ostream & out) const{
out << "player name : "<< name << endl;
out << "score : " << score << endl;
out << "cards : " << mydeque.size() << endl;
for(int i =0; i <mydeque.size();i++)
out << mydeque[i] << " " ;
}
};
ostream & operator <<(ostream & out, const Player & p){
p.tostring(out);
return out;}
int main () {
int rounds;
cout << "Give the number of rounds: "<<endl;
cin >> rounds;
Player one("some player");
for (int i = 1; i < rounds+1; i++){
one.recieve(i);
}
for( int i = 0; i < rounds; i++) {
one.play();
int randnum = rand()%(rounds-1 + 1) + 1;
cout << one << endl;
cout << " The dealer draws : " << randnum << endl;
if (randnum < one.play()){
one.score = one.score + 1;}}
return 0;
}
答案 0 :(得分:2)
执行import java.util.Scanner;
public class TwoNumbers {
public static void main(String[] args) {
int a;
int b;
Scanner in = new Scanner(System.in);
// Keep prompting the user until the input is correct
do {
System.out.println("Enter two positive integers, the first smaller than the second.");
System.out.print("First: ");
a = in.nextInt();
System.out.print("Second: ");
b = in.nextInt();
}
while(a > b);
// Only print this when the input is correct
System.out.println("You entered " + a + " and " + b);
}
}
语句后,函数结束。
变化:
return
要:
int play() {
return mydeque.front();
mydeque.pop_front();
}