-1

このフォーラムを使用するのはこれが初めてであり、Java の経験全体に慣れていないため、これが本当に簡単な修正である場合はご容赦ください。

学校向けにこのプロジェクトを作成しようとしていますが、if else ステートメントを機能させることができません。ご覧のとおり、近接または遠隔に入る場合はすべて問題ありませんが、そうでない場合はリダイレクトされます。私の問題は、melee または ranged と入力しても、まず if メソッドにリダイレクトされ、その後すぐに else ステートメントにリダイレクトされることです。

これを修正する方法を知っている人はいますか?

newchamp.Type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (melee or ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
        if (Type.equalsIgnoreCase("melee")) {
           JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
        }
        if (Type.equalsIgnoreCase("ranged")) {
              JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
        }
        else {
            JOptionPane.showMessageDialog(null,"You can only choose Melee or Ranged!");
            newchamp.Type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (Melee or Ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
               JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");

        }
    }
}
4

4 に答える 4

6

あなたのコード:

if(a) ...
if(b) ... else ...

したがって、すべての場合 (a trueまたはfalse) である場合bはステートメントfalseに入りelseます (より具体的には、Typeが と等しくない場合ranged、そのelse部分が実行されます)。

あなたが望むのは

if(a) ... else if(b) ... else ...

あなたのコードを使用して:

if (Type.equalsIgnoreCase("melee")) {

} else if (Type.equalsIgnoreCase("ranged")) {

} else {

}
于 2013-02-11T08:14:01.980 に答える
5

if, else if,elseコンストラクトを使用する必要があります。

if (Type.equalsIgnoreCase("melee")) {
  // ...
}
else if (Type.equalsIgnoreCase("ranged")) {
  // ...
}
else {
  // ...
}
于 2013-02-11T08:12:20.510 に答える
1

しますか

newchamp.Type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (melee or ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
    if (Type.equalsIgnoreCase("melee")) {
       ..something..
    }
    else if (Type.equalsIgnoreCase("ranged")) {
          ..something..
    }
    else {
       ..something..
    }
}
}

あなたのために働きますか?

于 2013-02-11T08:17:21.920 に答える
1

このようにしてください

String   type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (melee or ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
            if (Type.equalsIgnoreCase("melee")) {
               JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
            }else if (type.equalsIgnoreCase("ranged")) {
                  JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
            }
            else {
                JOptionPane.showMessageDialog(null,"You can only choose Melee or Ranged!");
               type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (Melee or Ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
                   JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
            }
于 2013-02-11T08:15:51.670 に答える