我正在尝试创建一个空的全局字符串arrray
struct GlobalVariables {
static var globalString = [String]()
}
我想通过将字符串值放入数组
来初始化它func percentageCalculation() -> String {
var FinalString = String()
for i in 1...100 {
let tstring = String("\(i)%\n")
GlobalVariables.globalString[i] = tstring
}
return FinalString
}
然后将全局字符串值输出到我的tableview。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let testLabel = cell?.viewWithTag(1) as! UITextView
testLabel.text = GlobalVariables.globalString[indexPath.row]
return cell!
}
关于实现这一目标的最佳方法的任何建议?这是我试图完成的伪造代码
答案 0 :(得分:1)
您不需要结构来定义简单数组。您可以在全局级别定义一个数组,如下所示:
for i in 1...100 {
globalString.append("\(i)")
}
获得阵列后,您可以像这样添加数字:
testLabel.text = globalString[indexPath.row]
然后在您的cellForRow中,您可以使用它来填充值:
public class FinalMarkCalculator
{
/**
* This method is used to caculate the students mark
*/
public static void main(String[] args)
{
//Assigning the string a variable that holds the students name
String studentName;
//Initalizing a keyboard scanner
Scanner keyboardStudentName = new Scanner(System.in);
//System printing out the question
System.out.print("Enter the students name: ");
//Assigns the string studentName to the users input
studentName = keyboardStudentName.nextLine();
//Assigning the double a variable that hold the students assignment marks as a integer
double assignmentMarks;
//Assigning the string a variable that hold the students assignment marks
String assignmentMarksInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardAssignmentMarks = new Scanner(System.in);
//Asking the user for assingment marks while printing students name
System.out.printf("Enter %s's mark for Assignments (Max. 140): ", studentName);
//Taking the user input and assigning it to our string assignmentMarksInput
assignmentMarksInput = keyboardAssignmentMarks.nextLine();
//Converting the user input from a string to a double
assignmentMarks = Double.parseDouble(assignmentMarksInput);
//Assigning the double a variable that hold the students Mid-Term exam marks as a integer
double midTermExamMark;
//Assigning the string a variable that hold the students assignment marks from user input
String midTermExamMarkInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardExamMidTermMark = new Scanner(System.in);
//Asking the user for the Mid-Term Exam mark while printing students name
System.out.printf("Enter %s's mark for Mid-Term Exam (Max. 60): ", studentName);
//Taking the user input and assigning it to our midTermExamMarkInput
midTermExamMarkInput = keyboardExamMidTermMark.nextLine();
//Converting the user input from a string to a double
midTermExamMark = Double.parseDouble(midTermExamMarkInput);
//Assigning the double a variable that hold the students Mid-Term exam marks as a integer
double finalExamMark;
//Assigning the string a variable that hold the students assignment marks from user input
String finalExamMarkInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardFinalExamMark = new Scanner(System.in);
//Asking the user for the Mid-Term Exam mark while printing students name
System.out.printf("Enter %s's mark for Final Exam (Max. 85): ", studentName);
//Taking the user input and assigning it to our midTermExamMarkInput
finalExamMarkInput = keyboardFinalExamMark.nextLine();
//Converting the user input from a string to a double
finalExamMark = Double.parseDouble(finalExamMarkInput);
//Printing grade report title
System.out.println("Grade report" + "\n------------");
//Printing the students name
System.out.println(studentName + "\n");
//Printing the assignments mark
System.out.printf("%.2f/140 worth 15%%\n", assignmentMarks);
//Printing the Mid-Term exam mark
System.out.printf("%.2f/60 worth 40%%\n", midTermExamMark);
//Printing the Final exam mark
System.out.printf("%.2f/85 worth 45%%\n", finalExamMark);
double finalMark = (assignmentMarks*.15)+(midTermExamMark*.40)+(finalExamMark*.45);
//Printing the total calculated final mark
System.out.printf("\n\nFinal Mark: " + finalMark);
}
}