【发布时间】:2016-08-25 05:38:37
【问题描述】:
我想做的是做一些我可以用来计划我一年中工作量的东西。我正在编写一个程序,在其中手动输入班级名称、页数和班级难度。
我正在使用一个类,其中我放入了我之前命名的所有数据,另一个类负责计算工作。
包含大学课程信息的Java类:
public classInfo(String className, int classAmount, int classDifficulty, int classTime)
{
name = className; // name of the class
amount = classAmount; // amount of pages
difficulty = classDifficulty; // difficulty (1 = easy >> 3 = hard)
time = classTime; // time you have (1 or 2 semesters)
}
/**
* Prints all the data you have put in about this class
*/
public void printInfo()
{
System.out.println("Vak: " + name);
System.out.println("Hoeveelheid leerstof: " + amount + " pagina's");
System.out.println("Moeilijkheidsgraad: " + difficulty);
System.out.println("Dit vak duurt: " + time + " semester(s)");
}
public String toString()
{
return naam;
}
}
我测试了这些方法,它们都按我的预期工作。最后一个方法 (toString() ) 是我创建的一个方法,以便我可以打印我保存在第二个类的 ArrayList 中的所有类的列表。应该进行计算的那个。
这是我的第二堂课的概述:(应该进行计算的那个)
import java.util.ArrayList;
public class Planner
{
private ArrayList<ClassInfo> course;
private int index;
/**
* constructor
*/
public Planner()
{
course = new ArrayList<ClassInfo>();
}
/**
* This method is used to add classes to the ArrayList
*/
public void addClass(ClassInfo aCourse)
{
course.add(aCourse);
}
/**
* With this method you can print all the names of the classes that are currently in the ArrayList
*/
public void showClass()
{
for(index = 0; index < course.size(); index++)
{
System.out.println(course.get(index));
}
}
/**
* This method is used to calculate the workload of the classes.
*/
public void berekenKlas()
{
/////// HERE IS MY PROBLEM. /////
}
}
我想知道如何告诉 Java 我保存在 ArrayList 中的不同整数之间的区别。
我想要做的是循环遍历所有类(就像在循环遍历类名称的方法中一样)并将“页数”和“时间”的数据保存到 2 个不同的变量中。
我目前的困惑是我不知道该怎么做。谁有想法?
提前谢谢你。
【问题讨论】:
-
既然你已经有了一个循环,你显然知道如何提取值。你想从那里做什么? “保存数据”是什么意思?
-
你可以有两种方法来生成你需要的两条数据
public int totalTime() { int result=0; for(index = 0; index < course.size(); index++) { result+=course.get(index).time; } return result; } public int totalDifficulty() { int result=0; for(index = 0; index < course.size(); index++) { result+=course.get(index).difficulty; } return result; } -
我真的只是想弄清楚如何告诉 Java 我想要使用的 2 个整数之间的差异。两个整数都保存在同一个 ArrayList 中。