我有一个有效的代码,但我的for循环不计入1 - 7.基本上我需要这个循环来运行并计算1,2,3,4,5,6,7
这是我的代码:
public class Course {
public static int a;
public static String code;
public static int CreditHours;
Course() {}
Course(int newA) {a = newA;}
public static int getCourse(int newA) {
for (int i = 0; i <= 7; i = i + 1) {
newA = i;
a=newA;
}
return a;
这是我的输出:
course Objects each has a code (e.g. IT1006) and credit hours (e.g. 6)
The number inside the [] is the display order number
The number inside the () is the credit hours
[7] IT2249 (6)
我需要[7] IT2249(6),但我需要从[1]开始并计算到[7]
这是一项学术任务。
答案 0 :(得分:1)
为了给你一个完全准确的答案,我必须看看你是如何初始化你的类并调用getCourse(int)函数的。但至少要指出你正确的方向 - 问题是你没有循环任何打印功能。你需要将你的for循环移到外面并围绕你的getCourse(int)函数。
以下是您的代码MIGHT的示例:
public class Course {
public static int a;
public static String code;
public static int CreditHours;
Course() {}
Course(int newA) {a = newA;}
public static int getCourse(int newA) {
newA = i;
a=newA;
return a;
}
}
//outside your Course class, wherever your print function is in, main?
for (int i = 1; i <= 7; i = i + 1) {
int courseNumber = Course.getCourse(i)
System.out.println("["+courseNumber+"]");
}
干杯
答案 1 :(得分:-1)
Javier Gonzalez帮我解答了答案。我完全删除了getCourse方法并将for循环移动到输出语句。
以下是方法修复:
public class Course {
public static int a;
public static String code;
public static int CreditHours;
Course() {}
Course(int newA) {a = newA;}
这是输出语句:
public static void main(String[] args) {
System.out.println("Quenten's Copy");
System.out.println("course Objects each has a code (e.g. IT1006) and
credit hours (e.g. 6)");
System.out.println("The number inside the [] is the display order number");
System.out.println("The number inside the () is the credit hours");
for (int i = 1; i <= 7; i++) {
a = i;
System.out.println("[" + a + "] " + Course.getCode(code) + " (" + Course.getCreditHours(CreditHours) + ")");
}
}
}
这是最终输出:
course Objects each has a code (e.g. IT1006) and credit hours (e.g. 6)
The number inside the [] is the display order number
The number inside the () is the credit hours
[1] IT1006 (6)
[2] IT4782 (3)
[3] IT4789 (3)
[4] IT4079 (6)
[5] IT2230 (3)
[6] IT3345 (3)
[7] IT2249 (6)
感谢您的帮助。我不知道如何让for循环正常运行,但现在我意识到它必须在输出发生时运行。