2016-09-08 1 views
-2

このプログラム全体をループに入れて、「終了」とタイプするまで数字を入力し続けることができる最も簡単な方法は何ですか?コードは以下の通りです、あなたの助言に感謝します。C#ランダムダイスの生成 - ユーザーが終了するまでどのようにループするのですか?

私は初心者ですが、以前はC#でループ計算機を作っていましたが、それほどエレガントではありませんでした。私はここでこのランダムなサイコロプログラムで休憩といくつかのループのアイデアを試してきましたが、うまく動作する何かを得ていない。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace RandomDice 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Creates our object randomRoll as a member to the Random class. 
      // By default Random objects use a parameter-less constructor, 
      // hence the emply parentheses. 
      // Classes are reference types, and stored on the heap. 

      Random randomRoll = new Random(); 

      // Simulate the rolls for the requested dice. 

      Console.WriteLine ("Enter number of dice to roll: "); 
      int diceCount = Convert.ToInt32 (Console.ReadLine()); 

      // We now call the Next method, which is part of the Random class. 
      // This method Next can be called on objects that are a member of the Random class, 
      // which our object randomRoll is. 
      // The +1 is because Next(6) returns 0 to 5. 

      int total = 0; 
      for (int index = 1; index <= diceCount; index++) { 

       int roll = randomRoll.Next (6) + 1; 
       total += roll; 

       // If the loop is not on the last dice, print out a plus symbol after the roll value. 
       // As an aside, if you wanted to start the loop at index = 0 as is conventional, 
       // index would have to count up until it is simply < diceCount (not less than or equals), 
       // and the printing plus symbols conditional below would have to be coded as 
       // (index != diceCount -1) to adjust for the indexing since the loop 
       // would be starting at zero. But who rolls zero dice? So start the loop at 1. 

       if (index != diceCount) { 
        Console.Write (roll + "+"); 
       } else { 

        // Write the last die roll. 
        Console.Write (roll); 
       } 
      } 

      // Print the total sum of all the dice rolls. 

      Console.WriteLine ("=" + total); 
      Console.ReadKey(); 
     } 
    } 
} 
+1

はちょうど別の方法でコードを入れて終了するか継続するようにユーザーに依頼し、各ループで、ループ内でそれを呼び出します。それは完全に自分の意見でどのようにアプリを設計することです。 –

+1

1)ダイスローラーを別の方法で入れ、ユーザーが終了するまでそれを呼び出します。 https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c-sharp%20how%20to%20add%20and%20call%20a%20method2)入力とDO/WHILEループ内でexitをテストするhttps://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c-sharp+do+while、(最悪の場合)、既存のメソッドhttps://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c-sharp+gotoにlabel:とGOTOステートメントを追加します。 "私は本当に頑張ったが、私のために宿題をしてください"それをカットしません。 –

+1

あなたのProfを感心させたい場合は、Console.ReadKey(true)を使用し、ESC https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c- sharp + console.readkey –

答えて

2

使用このようなループ中:

static void Main(string[] args) 
    { 
     // Creates our object randomRoll as a member to the Random class. 
     // By default Random objects use a parameter-less constructor, 
     // hence the emply parentheses. 
     // Classes are reference types, and stored on the heap. 
     Console.WriteLine("Enter a number to roll or enter Exit"); 
     string input = Console.ReadLine(); 
     while(!input.equalsIgnoreCase("exit")) 
     { 
     Random randomRoll = new Random(); 

     // Simulate the rolls for the requested dice. 

     int diceCount = Convert.ToInt32 (input); 

     // We now call the Next method, which is part of the Random class. 
     // This method Next can be called on objects that are a member of the Random class, 
     // which our object randomRoll is. 
     // The +1 is because Next(6) returns 0 to 5. 

     int total = 0; 
     for (int index = 1; index <= diceCount; index++) { 

      int roll = randomRoll.Next (6) + 1; 
      total += roll; 

      // If the loop is not on the last dice, print out a plus symbol after the roll value. 
      // As an aside, if you wanted to start the loop at index = 0 as is conventional, 
      // index would have to count up until it is simply < diceCount (not less than or equals), 
      // and the printing plus symbols conditional below would have to be coded as 
      // (index != diceCount -1) to adjust for the indexing since the loop 
      // would be starting at zero. But who rolls zero dice? So start the loop at 1. 

      if (index != diceCount) { 
       Console.Write (roll + "+"); 
      } else { 

       // Write the last die roll. 
       Console.Write (roll); 
      } 
     } 

     // Print the total sum of all the dice rolls. 

     Console.WriteLine ("=" + total); 
     //Get the input again 
     Console.WriteLine("Enter a number to roll or enter Exit"); 
     input = Console.ReadLine(); 
    } 
    } 
+0

ありがとうRicha Garg。 – jarombra

関連する問題