Java 从项目中删除静态
在没有超长的方法签名的情况下,它们是在这些方法之间传递变量和数组的简单方法吗?目前我正在使用这些静力学,但我知道这不是“正确”的方法,但如果我在签名中传递它们,就会开始让一切看起来很丑陋。那么,传递诸如lastName、firstName和role等变量的“正确”方式是什么呢Java 从项目中删除静态,java,oop,static,Java,Oop,Static,在没有超长的方法签名的情况下,它们是在这些方法之间传递变量和数组的简单方法吗?目前我正在使用这些静力学,但我知道这不是“正确”的方法,但如果我在签名中传递它们,就会开始让一切看起来很丑陋。那么,传递诸如lastName、firstName和role等变量的“正确”方式是什么呢 // Program wide variables //static String firstName; // First name from the file static String lastName; // Las
// Program wide variables
//static String firstName; // First name from the file
static String lastName; // Last name from the file
static String username; // Username
static String password;
static String role;
static String email;
static String answersFile; // Answers file in use with path and ext
static String[] answeredQ = new String[questAsks]; // Recording the question asked
static Boolean[] answeredA = new Boolean[questAsks]; //Recording users answer
static Boolean[] answeredC = new Boolean[questAsks]; //Recording the correct answer
public static void main(String Args[]) throws FileNotFoundException, IOException {
// Main method that contains major control functions; a quick summary of the program; magic
}
public static void quiz(String testType) throws HeadlessException, IOException {
String testBankFile = path + testType + ".txt";
Random rand = new Random();
int questionCount = 0, right = 0, wrong = 0;
long startTime = System.currentTimeMillis(); // Setting the start time in milliseconds
while (questionCount < questAsks) { // Loop that will ask all the questions
int r = rand.nextInt(getLines(testBankFile));
boolean ans = promptQuestion(read(r, testBankFile), questionCount + 1); // For some reason this makes it work
answeredQ[questionCount] = read(r, testBankFile);
answeredA[questionCount] = ans;
answeredC[questionCount] = parseA(read(r, answersFile));
if (ans != parseA(read(r, answersFile))) {
wrong++;
} else if (ans == parseA(read(r, answersFile))) {
right++;
}
questionCount++;
}
JOptionPane.showMessageDialog(null, "You got " + wrong + " wrong and " + right + " correct.");
long endDiff = (System.currentTimeMillis() - startTime);
makeReport(firstName, lastName, username, printTime(endDiff), testType, right);
}
// Generates a report report(first, last, score, time, array of answers)
public static void makeReport(String first, String last, String user, String time, String testType, int score) throws IOException {
DateFormat dateF = new SimpleDateFormat(dateFormat);
Date date = new Date();
String fileName = user + "_COSC236_Quiz_" + dateF.format(date) + ".txt";
File file = new File(fileName);
file.createNewFile();
FileWriter out = new FileWriter(fileName);
double percent = (((double) score) / ((double) questAsks) * 100);
out.write("Name: " + first + " " + last + "\n");
out.write("Score: " + percent + "%\n");
out.write("Elapsed time: " + time + "\n");
out.write("Test type: " + testType + "\n");
out.write("---------------------------------------------------------------------\n");
out.write(" Users\tCorrect\tQuestion\n");
for (int i = 0; i < answeredQ.length; i++) {
out.write(i + 1 + ".) ");
out.write(answeredA[i].toString() + "\t");
out.write(answeredC[i].toString() + "\t");
out.write(answeredQ[i] + "\n");
}
out.close();
}
// Boolean login method | login(tries allowed, source file)
public static void login(int tries, String source) throws FileNotFoundException, IOException {
String[] loginInfo;
boolean invalid = false;
for (int x = 0; x < tries; x++) {
invalid = false;
loginInfo = promptLogin();
if (loginInfo[0].toLowerCase().equals("done")) {
System.exit(0);
}
for (int i = 0; i < getLines(source); i++) {
StringTokenizer st = null;
st = new StringTokenizer(read(i, source));
String user = st.nextToken();
String pass = st.nextToken();
if (user.equals(loginInfo[0])) {
if (pass.equals(loginInfo[1])) {
username = loginInfo[0];
password = loginInfo[1];
firstName = st.nextToken();
lastName = st.nextToken();
email = st.nextToken();
role = st.nextToken();
if (role.toLowerCase().equals("instructor")) {
promptInstructor();
JOptionPane.showMessageDialog(null, exitedInstructorMode);
break;
} else {
run();
}
} else {
invalid = true;
}
} else {
invalid = true;
}
}
if(invalid) {
JOptionPane.showMessageDialog(null, invalidLogin);
}
}
JOptionPane.showMessageDialog(null, tooManyAttempts);
}
//程序范围的变量
//静态字符串firstName;//文件中的名字
静态字符串lastName;//文件中的姓氏
静态字符串用户名;//用户名
静态字符串密码;
静态字符串角色;
静态字符串电子邮件;
静态字符串应答文件;//与path和ext一起使用的应答文件
静态字符串[]answeredQ=新字符串[questAsks];//记录所问问题
静态布尔值[]answeredA=新布尔值[questAsks]//录音用户回答
静态布尔值[]answeredC=新布尔值[questAsks]//记录正确答案
公共静态void main(字符串Args[])抛出FileNotFoundException、IOException{
//包含主要控制功能的主要方法;程序的快速摘要;魔术
}
公共静态无效测试(String testType)抛出HeadlesException、IOException{
字符串testBankFile=path+testType+“.txt”;
Random rand=新的Random();
int questionCount=0,right=0,error=0;
long startTime=System.currentTimeMillis();//以毫秒为单位设置开始时间
while(questionCount
}为什么不使用OOP创建一个类来保存需要传递的值呢。为您的对象创建CLS 例如:
class User{
String lastName;
String username;
String password;
String role;
String email;
...
public static User login(int tries, String source) throws FileNotFoundException, IOException {
//this you read User param and add new User
return user;
}
}
现在,如果您需要姓氏、用户名、密码、角色或电子邮件,您可以
pass User instance一个选项是构建一个包含所有成员字段的类。您的登录方法将创建该类型的新对象,填写并返回它。如果您使用的是字段,无论是否为静态字段,您根本不需要将它们作为参数传递。