使用函数返回多个值

时间:2019-06-09 03:09:06

标签: c++ function class

我只是在尝试使用类/函数编写一些石头,纸,剪刀游戏的不同东西。除了显示用户获胜次数,计算机获胜次数和平局数量的结果之外,一切工作都很好。

我正在用vs代码进行编码。我查看了一些有关如何返回多个值的示例,并发现它很有帮助。我目前正在使用指针来执行此方法,但是我不确定基于代码的结果是否会受到限制。

void winner(int* U, int* C, int* T)
{
    *U = userWin
    *C = compWins;
    *T = numTies;

    if(compChoice == userChoice){
    std::cout << "It's a tie!\n";
    numTies++;
    }
    else if(userChoice == 'P' && compChoice == 'R'){
        std::cout << "You win! Paper covers rock.\n";
        userWins++;
    }
    else if(userChoice == 'S' && compChoice == 'R'){
        std::cout << "Computer wins! Rock beats scissors.\n";
        compWins++;
    }
    else if(userChoice == 'S' && compChoice == 'P'){
        std::cout << "You win! Scissors beats paper.\n";
        userWins++;
    }
    else if(userChoice == 'R' && compChoice == 'P'){
        std::cout << "Computer wins! Paper covers rock.\n";
        compWins++;
    }
    else if(userChoice == 'R' && compChoice == 'S'){
        std::cout << "You win! Rock beats scissors.\n";
        userWins++;
    }
    else if(userChoice == 'P' && compChoice == 'S'){
        std::cout << "Computer wins! Scissors beats paper.\n";
        compWins++;
    }
    else std::cout << "Invalid input.\n";
    std::cout << "\n";


}


int main(){

  rps obj;
  char char1('y');
  int userWins;
  int compWins;
  int numTies;

  std::cout << "THIS IS A GAME OF ROCK, PAPER, SCISSORS!\n";

  do{

    obj.player();
    obj.computer();
    obj.winner(&userWins, &compWins, &numTies);

    std::cout << "Enter y to play again or anything else to win: ";
    std::cin >> char1;
    std::cout << "\n";

  }while(char1 == 'y' || char1 == 'Y' );

  obj.results();


...
Please enter Rock, Paper, or Scissors - 'R' for Rock, 'P' for Paper,        'S' for Scissors: R

The computer chose scissors.

You win! Rock beats scissors.

Enter y to play again or anything else to win: n

Here are the results...
YOU: 1  COMPUTER: -416437631  TIES: 32769

代码的结果会打印出用户赢得多少次的期望值。但是,似乎正在打印出计算机的获胜次数和平局数的内存位置。

2 个答案:

答案 0 :(得分:1)

您忘记初始化变量。在import java.awt.*; import org.eclipse.draw2d.*; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class testdot { private void run() { Shell shell = new Shell(new Display()); shell.setSize(365, 280); shell.setText("Genealogy"); shell.setLayout(new GridLayout()); Canvas canvas = createDiagram(shell); canvas.setLayoutData(new GridData(GridData.FILL_BOTH)); Display display = shell.getDisplay(); shell.open(); while (!shell.isDisposed()) { while (!display.readAndDispatch()) { display.sleep(); } } } private IFigure createPersonFigure(String name) { RectangleFigure rectangleFigure = new RectangleFigure(); rectangleFigure.setBackgroundColor(ColorConstants.lightGray); rectangleFigure.setLayoutManager(new ToolbarLayout()); rectangleFigure.setPreferredSize(100, 100); rectangleFigure.add(new Label(name)); return rectangleFigure; } private Canvas createDiagram(Composite parent) { // Create a root figure and simple layout to contain // all other figures Figure root = new Figure(); root.setFont(parent.getFont()); XYLayout layout = new XYLayout(); root.setLayoutManager(layout); // Add the father "Andy" IFigure andy = createPersonFigure("Andy"); root.add(andy); layout.setConstraint(andy, new Rectangle(new Point(10, 10), andy.getPreferredSize())); // Add the mother "Betty" IFigure betty = createPersonFigure("Betty"); root.add(betty); layout.setConstraint(betty, new Rectangle(new Point(230, 10), betty.getPreferredSize())); // Add the son "Carl" IFigure carl = createPersonFigure("Carl"); root.add(carl); layout.setConstraint(carl, new Rectangle(new Point(120, 120), carl.getPreferredSize())); // Create a canvas to display the root figure Canvas canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED); canvas.setBackground(ColorConstants.white); LightweightSystem lws = new LightweightSystem(canvas); lws.setContents(root); return canvas; } public static void main(String[] args) { new testdot().run(); } } 中,将声明更改为:

main

然后在int userWins = 0; int compWins = 0; int numTies = 0; 中删除:

winner

并更改:

*U = userWin
*C = compWins;
*T = numTies;

收件人:

userWins++;

,其他计数器也是如此。

答案 1 :(得分:0)

在main函数中,您需要使用初始值'0'初始化int变量。 这样可以防止编译器将这些变量,垃圾值提供给您。

int main(){

    rps obj;
    char char1('y');
    int userWins=0;
    int compWins=0;
    int numTies=0;
    ...