0
    int menuoptions;
    String userinput;
    String usercheck="";
    String username="user";
    String password;
    int intCounter=0;

    con.println("TYPING GAME\n");
    con.println("1. Sign in");
    con.println("2. Create a new account");
    menuoptions = con.readInt();
    con.clear();

    if(menuoptions==1){
        while(!username.equals(usercheck) && intCounter==0){
            con.println("Please type in your username.");
            userinput = con.readLine();
            con.clear();
            TextInputFile infile = new TextInputFile("logins.txt");
            while(infile.eof() == false && intCounter==0){
                usercheck=infile.readLine();
                infile.readLine();
                    if(username.equals(usercheck)){
                    intCounter=intCounter+1;
                    }
            }

            if(!userinput.equals(usercheck) && intCounter==0){
                con.println("No such username.");
                pause(2000);
                con.clear();
            }       
            else if(userinput.equals(usercheck)){
                intCounter = intCounter+1;
            }
        }

        con.println("What is your password?");

    }

    if(menuoptions==2){
        con.println("What will be your username?");
        username = con.readLine();
        con.clear();
        con.println("What will be your password?");
        password = con.readLine();
        con.clear();
        TextOutputFile outfile = new TextOutputFile("logins.txt", true);
        outfile.println(username);
        outfile.println(password);
    }

}
public static void pause (int intMS){
    try{Thread.sleep(intMS);
}catch(InterruptedException y){}}

logins.txt では、1 行に「voidturbulence」があり、次の行に「80」があります。「voidturbulence」と入力すると、パスワードを要求する必要があるときに「ユーザー名が見つかりません」にジャンプします。

しかし、userinput (voidturbulence) が usercheck (最初の行 [voidturbulence]) と等しい場合、ループから抜け出してパスワードを尋ねるべきではありませんか?

4

1 に答える 1

0

A.コード

usercheck=infile.readLine();
infile.readLine();

私には不審に見えます。によって読み取られるファイルには、おそらく空白行、ユーザー名の行、およびその他のテキストがありますinfile。したがって、usercheckおそらく、ターゲットとするユーザー名を受け取ることはありません。( から 2 行おきにスキップしinfileます。)

B.代わりに

infile.eof() == false

使用する

!infile.eof ()

読みやすくするために。さもないと、

(((infile.eof() == false) == true) == true)

より読みやすいと見なされますよね?

C.の代わりに

if (menuoptions == 1)
{
}
if (menuoptions == 2)
{
}

使用する

if (menuoptions == 1)
{
}
else if (menuoptions == 2)
{
}

なぜなら、menuoptionsそれが 1 に等しいことが分かった (そして最初の then ブロックで変更しなかった) ときに 2 に等しくなることはできないからです。

D. 何にintCounter適していますか?

  • 0 に初期化します。
  • ユーザー名が usercheck と等しい場合は、それを増やします。
  • username が usercheck と等しくなく、intCounter が 0 である限り、while ループはループします。

したがって、username等しい場合、両方の条件が満たされますusercheck

をなくすことができintCounterます。

これは、不適切な変数名の良い例です。" " は、それがであることも、何かのカウントが含まれているintCounterことも保証しません。int有用な名前を作成しようとすると、有用なコードを作成する傾向があることがわかります。あなたの例では、役に立たない名前と、名前の背後にある値を操作する役に立たないコードを作成しましたが、実際には何も達成しません。

E. 何を達成しようとしていますか? 質問の見出しを除いて、コードがカバーしようとする要件の仕様はありません。やりたいことを指定してから、コードを提示し、問題を特定することをお勧めします。一般的なキーワードに続けてコードを投げかけるだけではいけません。お願いします ;)

于 2012-12-04T16:01:09.473 に答える