我如何存储价值"总计"循环外?

时间:2016-09-16 14:56:32

标签: java

我正在用java编写我的第一个游戏,并且在循环结束后存储1值时遇到困难。

int dice1, dice2, count, total = 0, total1 = 0;

Scanner scan = new Scanner(System.in);

System.out.println("Press /y again");
String response = "";
response = scan.next();

//while loop for player dice
while (response.equals("y")) { 
   System.out.println("Roll dice? y/n");
   response = scan.next();
   if (response.equals("n")) {
       ComputerPig.ComputerRoll();
   }
   Random rand = new Random();
   dice1 = rand.nextInt(6)+1;
   dice2 = rand.nextInt(6)+1;
   count = (dice1 + dice2);
   total += count; // i need to store this value

1 个答案:

答案 0 :(得分:0)

首先,一旦用户输入n

,你需要突破循环
let etaReq = session.dataTask(with: etaURLreq as URLRequest)
    {

        (data, response, error) in

        if error == nil
        {
            let httpResponse = response as? HTTPURLResponse

            if (httpResponse?.statusCode)! >= 200 && (httpResponse?.statusCode)! <= 299
            {

                print("Good status code \(httpResponse?.statusCode)")
                print(data?.description)

                do
                {

                    if let jsonDataArray = try! JSONSerialization.data(withJSONObject: data (here is the error), options: [])
                    {

                    }

                }

对于两个,我测试了这段代码,减去了if (response.equals("n")) { ComputerPig.ComputerRoll(); break; } ,它对我来说很好。当我输入ComputerPig.ComputerRoll();时,它进入上面的if语句,然后退出n循环,我在底部打印了总数。所以你的代码是正确的。