2016-08-19 14 views
0

ちょうどjavaをプログラミングする方法を自分自身で教え始めました。最初の部分は、ユーザーが 'n'の値を見つけなければならない単純な数学的方程式です。答えが正しい場合は、 'if'または 'else'があります(21)。elseをバイパスして残りのプログラムを実行すると、 'else'の場合、2回の試行後にプログラムを終了するにはどうすればいいですか?Javaでのパスワードの作成

import java.util.Scanner; 
import java.util.Random; 

public class Bot { 

    public static void main(String[] args) {   
     System.out.println("(6*7)%2=n"); 
     int InQuestion; 
     System.out.println("what is the value of ' n' in the equation"); 
     Scanner ScannedNumber = new Scanner (System.in); 
     int n = ScannedNumber.nextInt(); 
     InQuestion= n; 

     if (InQuestion==21) 
      System.out.println("Access Allowed"); 
      //*If 'Access Allowed' then permit the rest of the program to run consequently 
      //*passing the 'else' branch. 
     else 
      System.out.println("try again"); 

     for(n=0; n>2; n++);{ 
      int n1=ScannedNumber.nextInt(); 
      if (n1==21); 
       System.out.println("Access Denied"); 
       System.exit(0); 
     } 

     System.out.println("Hello I Am Bot"); 
     System.out.println("What is Your Name?"); 
     Scanner VariableNamePerry=new Scanner(System.in); 
     System.out.print("Hello," +VariableNamePerry.nextLine()); 
     Scanner VariableNamecalculate=new Scanner (System.in); 
     System.out.println(" What can I do for you?"); 
     System.out.print(VariableNamecalculate.nextLine()); 
     System.out.print(" what?"); 
     Scanner VariableNameThatIsWhatIWantYouToDo=new Scanner (System.in); 
     System.out.println("I Can Calculate The Square Of Any Number, If that Is What You Want?" +VariableNameThatIsWhatIWantYouToDo.nextLine()); 
     Scanner VariableNameOkay=new Scanner (System.in); 
     System.out.println(VariableNameOkay.nextLine()+ (" then")); 
     System.out.println("Give me a number..."); 

     Scanner VariableName1=new Scanner (System.in); 
     for(int MyVar=0; MyVar<1000; MyVar++){ 
      int MyVar1 = VariableName1.nextInt(); 
      System.out.println(MyVar1*MyVar1); 
      System.out.println("give me another number please?") 
     } 
    } 
} 
+0

コードをきれいにフォーマットすることから始めます。読むのは難しいです。 – chrylis

+0

まず、命名規則と書式設定規則に従ってください。コメントを使用します。小文字で始まる名前変数は上限ではありません。名前は「VariableNameThatIsWhatIWantYouToDo」ではないものを説明するものです。また、読みたい入力行ごとに新しいスキャナは必要ありません。あなたは同じものを使うことができます。 – nhouser9

+0

@ nhouser9コードは、コメントが不要な範囲で読むことができます。適切な名前付け規則は、必要な記述の唯一のソースである必要があります。 –

答えて

0

ifとelseステートメントの周りにブロックを配置してスコープします。 2回の試行後に停止したい場合は、whileループまたはdo whileループを使用して、単純なカウンタで試行回数をカウントし、カウンタが間違っている間はカウンタを増やすことができます。

0

アクセスが許可されるか、試行が終了するまで、ループの先頭部分をループすることができます。そして、アクセスが許可されている場合、ループから脱出するときに何らかの種類のインジケータを使用してください。

int attempts = 2; 
boolean accessGranted = false; 
for (int q = 0; q < attempts; q++){ 
    System.out.println("(6*7)%2=n"); 
    int InQuestion; 
    System.out.println("what is the value of ' n' in the equation"); 
    Scanner ScannedNumber = new Scanner (System.in); 
    int n = ScannedNumber.nextInt(); 
    InQuestion= n; 

    if (InQuestion==21){ 
     System.out.println("Access Allowed"); 
     accessGranted = true; 
     break; 
    } 
    else 
     System.out.println("try again"); 
} 
if (accessGranted){/*rest of program*/} 
0

プログラムを継続するには、実際のプログラムコードをmainとは別の空白に入れておくのが最適です。 Nkdyが言ったよう

public static void main(String[] args) { 
    System.out.println("(6*7)%2=n"); 
    int InQuestion; 
    System.out.println("what is the value of ' n' in the equation"); 
    Scanner ScannedNumber = new Scanner (System.in); 
    int n = ScannedNumber.nextInt(); 
    InQuestion= n; 

    if (InQuestion==21){ 
     System.out.println("Access Allowed"); 
     program(args); 
    //*If 'Access Allowed' then permit the rest of the program to run consequently 
    //*passing the 'else' branch. 
    } else { 
     System.out.println("try again"); 
     for(n=0; n>2; n++){ 
      int n1=ScannedNumber.nextInt(); 
      if (n1==21); 
      System.out.println("Access Denied"); 
      System.exit(0);     
     } 
    } 
} 

public static void program(String[] args){ 
    System.out.println("Hello I Am Bot"); 
    System.out.println("What is Your Name?"); 
    Scanner VariableNamePerry=new Scanner(System.in); 
    System.out.print("Hello," +VariableNamePerry.nextLine()); 
    Scanner VariableNamecalculate=new Scanner (System.in); 
    System.out.println(" What can I do for you?"); 
    System.out.print(VariableNamecalculate.nextLine()); 
    System.out.print(" what?"); 
    Scanner VariableNameThatIsWhatIWantYouToDo=new Scanner (System.in); 
    System.out.println("I Can Calculate The Square Of Any Number, If that Is What You Want?" +VariableNameThatIsWhatIWantYouToDo.nextLine()); 
    Scanner VariableNameOkay=new Scanner (System.in); 
    System.out.println(VariableNameOkay.nextLine()+ (" then")); 
    System.out.println("Give me a number..."); 

    Scanner VariableName1=new Scanner (System.in); 
    for(int MyVar=0; MyVar<1000; MyVar++){ 
     int MyVar1 = VariableName1.nextInt(); 
     System.out.println(MyVar1*MyVar1); 
     System.out.println("give me another number please?"); 
    } 
} 

が、あなたはそれが指定した制限に達するまで、int型に追加するwhileループを使用することができます。ここでは

は一例です。

+0

助けてくれてありがとう、私にはwhileループの例を教えてもらえますか?私はロジックとほとんどの構文に苦労しています。私はまた、 '休憩'の作業をすることもできません – Anaphase

+0

@Anaphase 'while'ループは基本的に' for'ループを取り除き、 'while(MyVar <1000){DoStuff(); } 'したがって、' for'ループでは、新しいintを作り、パラメータ内でそれを増やすことができます。 'while'ループでは、ロジックを指定することしかできません。 – Kivitoe

関連する問題