2016-11-18 4 views
-3

書き込みしようとしているプログラムでは、スイッチケースとWhileループの両方を使用する必要があります。 Switchの例では問題はありませんが、Whileループで同様のソリューションを取得する方法がわかりません。ネストされたWhileループ "すべてのコードパスが値を返すわけではありません"

入れ子のwhileループでユーザーが入力した値を評価しようとしています。私は、ユーザーが(システムなどを使用して、すべて正確であると仮定)と10を介して3などの間の任意の数を「選択」することができるようにすることで

static void Main() 
{ 
    int userInput; //user enters int with ReadLine 
    int defaultInt = 3; //user keys anything other than 3 through 10 

    Console.WriteLine("Enter an Integer"); 
    userInput = int.Parse(Console.ReadLine()); 
    InputCheck(); 
    Console.WriteLine("The number you have chosen is {0}", userInput 

} 

public int InputCheck() 
{ 
    while (userInput >= 3) 
    { 
     while (userInput <= 10) 
     { 
      return userInput; 
     } 
     while (userInput > 10) 
     { 
      return defaulInt = userInput; 
     } 
    while (userInput < 3) 
    { 
     return defaultInt = userInput; 
    } 
} 

はしばらくして行うことも可能。このようなものですループ? 私はこれをSwitchとして簡単に行うことができると知っていますが、私はすでにこのプログラムでこれを行い、Whileループを実装する必要があります。

+0

「while」を使用する理由はありません。 'if'を使用してください(再入力を要求せずにデフォルトで3になるので)。あなたの実際のエラーは、 'userInput <3'なら何も返さないからです。 – Rob

+2

私はこのコードがあなたの先生が" whileループを使う "ことを意味していることを心から疑っています。ユーザーが "QUIT"のような値を入力するまで、入力を求めて繰り返し評価するつもりはありませんでしたか? – Blorgbeard

答えて

0

if文はあなたがいないwhile、探しているものを実際にあります。およそif使用、コードの利便性と可読性を話す
は、私もそれをこのように再配置します:

public int InputCheck() 
{ 
    if (userInput > 10) 
     return defaultInt = userInput; 

    if (userInput >= 3) 
     return userInput; 

    return defaultInt = userInput; 
} 

ルックは、それが今良く見えるが、同じことを行います。
あなたのifのうちの2つは、以前の条件に従ってtrueであることが保証されているため、実際には冗長でした。

しかし、あなたのコードはまだ非稼働...

  1. であることはどのようなものです:defaultInt = userInput?本当にdefaultIntの値をオーバーライドしますか?名前のデフォルトは、そうであってはならないことを示唆しています。
  2. また、別の方法でローカル変数にアクセスしようとしましたが、これは不可能です。
  3. カッコと中カッコが2つありません。

してくださいは、完全に次の質問をする前に、この資料を読んで理解してください:

+0

はい、私はこれがはるかに良く見えると思います!あなたのコードを使用して、 "if(userHeightInput <= 10)"という上限を追加することで動作させることができました。 – user7176008

-1
public int InputCheck() 
{ 
    if (userInput >= 3) 
    { 
     if (userInput <= 10) 
     { 
      return userInput; 
     } 
     if (userInput > 10) 
     { 
      return defaulInt = userInput; 
     } 
     } 
    if (userInput < 3) 
    { 
     return defaultInt = userInput; 
    } 
} 
+0

私のIDEは2つの 'if'sが冗長であることを示唆しています:) –

+0

これはコンパイルされません - タイトルとまったく同じエラーがあります。 – Rob

0

私はトンを考えますあなたの状況で彼はwhileループのアイデアは、入力値に満足するまで、userInputを求めるプログラムを維持することです。しかし、あなたはwhileループを持っていますが、あなたの関数はuserInputの間違った値を修正する機会を与えません。

ロジックを少し並べ替えて、ユーザーがwhileループ内の入力エラーを修正する機会を持つようにしました。

static void Main() 
    { 
     // `userInput` and `defaultInt` are only visible inside this function 
     // so you won't have access to them in InputCheck() unless you pass them in as parameters ... 

     int userInput = -1; //user enters int with ReadLine 
     int defaultInt = 3; //user keys anything other than 3 through 10 
     bool ok = false;  // flag to keep you in the while loop until the userInput is acceptable 

     while (!ok) // while we are *not* ok - the while-condition is checked at the top of the while-loop 
     { 
      // Give user a chance to fix `userInput` inside the while loop 
      Console.WriteLine("Enter an Integer from 3 to 10"); // let the user know what the valid inputs are 
      userInput = int.Parse(Console.ReadLine());    // need to fix!! if user enters non-integer, eg "quit", this will throw an exception 

      ok = InputCheck(userInput); 
      // You have a choice here: 
      // You can keep looping until user specifies a number from 3 to 10 

      // Or you can simply override a bad choice with your default. 
      // If you don't want the default logic, get rid of the following block 
      if (!ok) 
      { 
       // Let the user know what you're doing 
       Console.WriteLine("Overriding your invalid choice {0} to {1}", userInput, defaultInt); 
       userInput = defaultInt; // override user's choice 
       ok = true;     // force the while loop to exit 
      } 

     } 
     Console.WriteLine("The number you have chosen is {0}", userInput); 
    } 

    // Move the while loop out of `InputCheck` function. 
    // InputCheck now has only one job - to check that userInput is acceptable 

    public bool InputCheck(int userInput) // you have to pass `userInput` in as parameter - it is only visible in Main() 
    { 
     if (3 <= userInput && userInput <= 10) 
      return true; 
     else 
      return false; 
    } 
} 
関連する問題