2016-10-06 7 views
0

if else if、elseループを出力しようとしています。配列とスキャナーを使用して、割り当てのための非常に基本的な暗号化プログラムを作成します。スキャナを使用すると整数は入力できますが、ループは実行されません。何が間違っているのですか?if else if、elseループを出力しようとしています。アレイとスキャナーを使用します。何が間違っているのですか?

import java.util.Scanner; 

public class Question1A2 { 

    public static void main (String [] args){ 

    Scanner S = new Scanner(System.in); 

    System.out.println("\t-------------------------"); 
    System.out.println ("\tIO's 4-digit Encrypter"); 
    System.out.println("\t-------------------------"); 


System.out.print("Please enter the four digit number you would like to encyrpt: "); 

int[] arr = {S.nextInt(),S.nextInt(),S.nextInt(),S.nextInt()}; 

int a = arr[0] %10 +7; 
int b = arr[1] %10 +7; 
int c = arr[2]%10 +7; 
int d = arr [3] %10 +7; 

int i = arr.length; 

for(;;){ 
    if (arr.length > 9999) { 
    System.out.println("Sorry, but that is not a four digit number. Program will terminate."); 
     System.out.println("Thank you for using Zito's 4-digit Encrypter program."); 
break; 
    } 

else if (arr.length < 1000) { 
    System.out.println("Sorry, but that is not a four digit number. Program will terminate."); 
    System.out.println("Thank you for using Zito's 4-digit Encrypter program."); 
    break; 
    } 

else { 
System.out.print("The encyrpted version of your input is "); 

System.out.print(a); 
System.out.print(b); 
System.out.print(c); 
System.out.print(d); 

break; 
} 

    } 
} 
} 
+0

なぜループが実行されるべきですか?あなたは何があってもそれを打ち破っているようです。 –

+1

ループ内のすべての場合にbreakステートメントがあります。 break文が何をしているのか知っていますか?そうでない場合は、削除するか検索してください。簡単な答えは、ループしないことは、このコードが行うべきものであることです。また、 'arr'の長さが'> 9999'であるかどうかチェックするのはなぜですか?配列の長さは常に4です。 – nhouser9

+1

'arr.length'はいつ4以外になりますか?私はあなたが間違った桁数の数字を入力したかどうかをチェックしようとしていると仮定していますが、実際にはあなたのコードでは可能ではありません。 – jonhopkins

答えて

0

達成しようとしているものに対してループは必要ありません。あなたのコードに基づいて、あなたがしようとしているように見えるのは、1000から9999までの数字を取得し、数字が有効な場合は各数字を暗号化して出力し、無効の場合はプログラムを終了します。

開始するには、単一の数字を取得するだけで、各数字を個別に取得する必要はありません。次に、1000と9999の間であるかどうかをチェックします。このチェックは、ユーザーが数字が間違っていることを示すコードを繰り返す必要がないように、1つのif文で実行できます。数字が大丈夫なら、4桁の数字に分割し、それぞれを暗号化することができます。

int theNumber = S.nextInt(); 
if (theNumber < 1000 || theNumber > 9999) { 
    System.out.println("Sorry, but that is not a four digit number. Program will terminate."); 
    System.out.println("Thank you for using Zito's 4-digit Encrypter program."); 
} 
else { 
    // I'm leaving this up to you to figure out how to get each digit. 
    // There are several ways to do it, and it's out of scope for this question anyway. 
    int digit1 = ... 
    int digit2 = ... 
    int digit3 = ... 
    int digit4 = ... 

    int a = digit1 % 10 + 7; 
    int b = digit2 % 10 + 7; 
    int c = digit3 % 10 + 7; 
    int d = digit4 % 10 + 7; 

    System.out.print("The encrypted version of your input is "); 

    System.out.print(a); 
    System.out.print(b); 
    System.out.print(c); 
    System.out.print(d); 
} 
0

ユーザーは、ここでは4桁の数字を入力した場合:

int[] arr = {S.nextInt(),S.nextInt(),S.nextInt(),S.nextInt()}; 

スキャナが、入力として整数を使用します。解決するには、スペースで区切られた4桁の数字を求めるとS.nextInt() 4回を取得したり、あなたはここで行ったように入力INTを破ることができます:

int a = arr[0] % 10 + 7; 
int b = arr[1] % 10 + 7; 
int c = arr[2] % 10 + 7; 
int d = arr[3] % 10 + 7; 

取得するには、以下を使用して、変数最初に入力を割り当てる以外各番号:

int i = S.nextInt(); 
int a = i/1000; 
int b = (i/100) % 10; 
int c = (i/10) % 10; 
int d = i % 10; 

これまでのように、各番号をアレイに割り当てます。

また、前述のように、配列の長さは4です。目的を達成する1つの方法は、4桁の数字を入力させ、if/if elseを使用して整数全体が> 1000 || < 9999を入力し、数字を1桁に分割します。

ところで、あなたは「encrypt」という言葉でタイプミスがあります。

関連する問題