虽然开始掷骰程序的循环没有正确执行。

时间:2016-02-15 01:25:26

标签: c++

我是C ++的新手,我正在尝试构建一个掷骰子程序,该程序会提示用户是否希望在获胜或松散后继续玩游戏。如果选择是,则它将再次循环。如果没有被选中,那么它将显示获胜和失败的数量。任何帮助将不胜感激。我想我已经看了一段时间,而且我可能只是我自己的敌人。

#include <iostream>
#include <string>
#include <time.h>
#include  <Windows.h>
using namespace std;


int main()
{
char answer(0);
int wins(0), loses(0);

while (answer == 'y'); {
    srand(time(NULL));  // one time at the top of the program
    int d1(0), d2(0), d3(0), d4(0), roll(0), roll2(0), point(0);

    d1 = 1 + (rand() % 6);
    d2 = 1 + (rand() % 6);
    roll = d1 + d2;
    point = d1 + d2;

    cout << "Player Rolled " << d1 << "+" << d2 << "=" << roll << endl;
    cout << "Point is:" << point << endl;

    if (roll == 7 || roll == 11)
    {
        cout << "Player Rolled " << d1 << "+" << d2 << "=" << roll << endl << "Player Wins" << endl;
        wins++;
        Sleep(2000);  // 2 second pause
        cout << "Do you want to play again? (Y or N)";
        cin >> answer;

    }
    else if (roll == 2 || roll == 3 || roll == 12)
    {

        cout << "Player Rolled " << d1 << "+" << d2 << "=" << roll << endl << "Player loses" << endl;
        loses++;
        Sleep(2000);  // 2 second pause
        cout << "Do you want to play again? (Y or N)";
        cin >> answer;

    }
    else
    {

        do
        {
            d3 = 1 + (rand() % 6);
            d4 = 1 + (rand() % 6);
            roll2 = d3 + d4;

            if (roll2 == roll)
            {
                cout << "Player Rolled " << d3 << "+" << d4 << "=" << roll2 << endl << "Player Wins" << endl;
                wins++;
                Sleep(2000);  // 2 second pause
                cout << "Do you want to play again? (Y or N)";
                cin >> answer;
            }

        } while (roll2 != 7);
        cout << "Player Rolled " << d3 << "+" << d4 << "=" << roll2 << endl << "Player loses" << endl;
        loses++;
        Sleep(2000);  // 2 second pause
        cout << "Do you want to play again? (Y or N)";
        cin >> answer;
    }
 } 

cout << wins << " Wins and " << loses << " Loses" << endl;
system("pause");
return 0;
}

1 个答案:

答案 0 :(得分:1)

这是您的代码缩减为SSCCE :(请参阅http://sscce.org

%MACRO A_ROLLING (i);   
proc reg data=dataset
outest=temp_dataset (drop=_model_) tableout;
model y = x / NOINT;
WHERE t >(1+&i) and t <(22+&i);
run;

data A_EQ1ROLLING;
set A_EQ1ROLLING temp_dataset;
run;
%mend

DATA A_EQ1ROLLING; 
RUN;
%MACRO A_ROLL;
%do j=0 %to 64;
    %A_ROLLING (&j);
%end; 
%MEND;

%A_ROLL;

http://ideone.com/rsPVP9

所有这一切都是打印import javax.swing.JOptionPane; public class Ticketpurchasingprogram { public static void main (String[] args) { string customerName = JOptionPane.showInputDialog("Enter your name"); double ticketBaseCost = 10.60; int seatSelection = Integer.parseIn( JOption.showInputDialog("Select your seat number")); //There is an additional charge if the seat selected is between seats 56 - 306 { {if (seatSelection == 0 && seatSelection >= 55) { if (seatSelection >=56 && seatSelection >= 106) { if (seatSelection >= 107 && seatSelection <=206) { if (seatSelection >= 207 && seatSelection <= 306) { double seatSelectionCost = 0.00; } double seatSelectionCost = 7.45; } double seatSelectionCost = 14.30; } double seatSelectionCost = 35.16; } } } ticketSeatCost = ticketBaseCost + seatSlectionCost; deliveryMethod = JOptionPane.showInputDialog("Would you like your ticket emailed or shipped?"); {if (deliveryMethod = "emailed") { if (deliveryMethod = "shipped") { double convenienceFee = ticketSeatCost * 0.03; } double convenienceFee = ticketSeatCost * 0.01; } } totalCost = ticketSeatCost + convenienceFee; JOptionPane.showMessageDialog(null, "**Eagle Bank Arena Ticket** \n Customer Name: " + customerName + "\n Seat Number: " + seatNumber + "\n Delivery Method: " + deliveryMethod + "\n Ticket Base Cost: " + ticketBaseCost + "\n Seat Cost: " + seatSelectionCost + "\n Convenience Fee: " + convenienceFee + String.format("\n Total Cost $%.2f", totalCost)); } } ,因为答案永远不会设置为&#39; y&#39;。

要使用#include <iostream> using namespace std; int main() { char answer(0); while (answer == 'y') { cout << "running\n"; } cout << "end\n"; return 0; } 而不是end初始化answer,要进行此操作:

'y'

或使用(0)循环。

你关于成为你自己的敌人的说法:当你把问题归结为更简单的事情时,你可以在鞋子里找到石头,就像我在这里用你的代码的SSCCE版本做的那样。