我正在处理一个项目,所以你可能会看到一些没有被使用的代码,无论如何我试图从clas变量中获取1,2或3的值,我希望从方法ClassSelect返回,但每次我运行我一直在接受clas仍然是0
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test4 extends JFrame implements ActionListener{
public static void main(String[] args) {
//Deklaration
int clas = 0; // | 1 = Warrior | 2 = Tank | 3 = Archer |
ClassSelect(clas);
System.out.println(clas);
}
private static int ClassSelect(int e) {
while(e == 0){
String classs = JOptionPane.showInputDialog(null, "What class would you like to be? Warrior, Tank or Archer?");
System.out.println(classs);
if(classs.equals( "Warrior" ) || classs.equals( "warrior" )){
e = e + 1;
}
else if(classs.equals( "Tank" ) || classs.equals( "tank" )){
e = e + 2;
}
else if(classs.equals( "Archer" ) || classs.equals( "archer" )){
e = e + 3;
System.out.println(e);
}
else{
JOptionPane.showMessageDialog(null, "There is no class called " + classs);
e = 0;
}
}
return e;
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
答案 0 :(得分:4)
Java始终是按值传递的。
相反:
ClassSelect(clas);
应该是:
clas=ClassSelect(clas);