3

while ループに if ステートメントがたくさんあります。プログラムは条件に応じてエラー メッセージを出力する必要がありますが、複数のエラーがある場合はそのうちの 1 つだけである必要があります。

4

2 に答える 2

7

あなたの質問はあまり詳細ではないので、あなたが何を望んでいるかを正確に伝えるのは少し難しいです.

エラーが発生した後に while ループを次の反復に移動する場合は、次のcontinueステートメントを使用する必要があります。

while( something )
{
    if( condition )
    {
        //do stuff
        continue;
    }
    else if( condition 2 )
    {
        //do other stuff
        continue;
    }
    <...>
}

これらの s 以外ifがループ内になく、条件が整数値である場合は、switch代わりに次の使用を検討する必要があります。

while( condition )
{
    switch( errorCode )
    {
        case 1:
        //do stuff;
        break;
        case 2:
        //do other stuff;
        break;
        <...>
    }
}

サイクルを完全に再開したい場合は...まあ、これは少し難しいです。ループがあるのでwhile、条件を開始値に設定するだけです。たとえば、次のようなループがあるとします。

int i = 0;
while( i < something )
{
    //do your stuff
    i++;
}

次に、次のように「リセット」できます。

int i = 0;
while( i < something )
{
    //do your stuff
    if( something that tells you to restart the loop )
    {
        i = 0;//setting the conditional variable to the starting value
        continue;//and going to the next iteration to "restart" the loop
    }
}

ただし、これには本当に注意する必要があります。誤って無限ループに陥りやすいです。

于 2012-10-05T05:59:05.683 に答える
-1
String errorMessage = "No Error";

while( cond){
if( cond 1) {
  errorMessage = " Error 1"
}

if( cond 1) {
  errorMessage = " Error 1"
}

if( cond 1) {
  errorMessage = " Error 1"
}

if( cond 1) {
  errorMessage = " Error 1"
}
}

エラーが発生した後にブレークしたい場合は、break

エラーが発生した後に現在の反復を無視する場合は、次を使用します。continue

エラーが発生した後に実行を終了したい場合は、exit

于 2012-10-05T05:57:43.123 に答える