[ 本帖最后由 Muvac 于 2024-11-24 23:40 编辑 ]
上个帖因为新开主题太多被自己玩没了^ ^,现在重新开一个,小游戏推荐LearingGit
配置环境
现在只有21spring的课程邀请码是公开的,所以直接21spring启动,代码测试平台就是 Gradescope,Gradescope 课程的激活码是 P5WVGW,根据指示注册好账号就行。
Set up your computer
这里有shell的基本知识 ,学习一下git的配置,下载宇宙第一IDE IntelliJ IDEA
todo
HW0:Basic Java Programs
这里就是介绍一下基本的Java语法,做好几个Exercise
以下是参考答案,因为帖主生物工程转行先学的C++(藉口+1),最近家里搬厂自己还得去帮手(藉口+2),每天只有<=一坤时碰电脑(藉口+3),所以自己写的答案就不拿出来丢人现眼了,
Creative Exercise 1a: Drawing a Triangle
public class Ex1a {
public static void main(String[] args) {
Draw(5);
}
public static void Draw(int x) {
int y = 1;
while (y <= x) {
int i = 0;
while (i < y) {
System.out.print('*');
i++;
}
System.out.println();
y++;
}
}
}
Creative Exercise 1b: DrawTriangle
public class Ex1a {
public static void main(String[] args) {
Draw(5);
}
public static void Draw(int x) {
int y = 1;
while (y <= x) {
int i = 0;
while (i < y) {
System.out.print('*');
i++;
}
System.out.println();
y++;
}
}
}
Exercise 2
public class Ex2 {
/** Returns the maximum value from m. */
public static int max(int[] m) {
int maxNumber = 0;
int i = 0;
while (i < m.length) {
if (maxNumber < m[i]) {
maxNumber = m[i];
}
i++;
}
return maxNumber;
}
public static void main(String[] args) {
int[] numbers = new int[]{9, 2, 15, 2, 22, 10, 6};
System.out.println(max(numbers));
}
}
Exercise 3
public class Ex3 {
/** Returns the maximum value from m using a for loop. */
public static int max(int[] m) {
int maxNumber = 0;
for (int i = 0; i < m.length; i++) {
if (maxNumber < m[i]) {
maxNumber = m[i];
}
}
return maxNumber;
}
public static void main(String[] args) {
int[] numbers = new int[]{9, 2, 15, 2, 22, 10, 6};
System.out.println(max(numbers));
}
}
Exercise 4
public class Ex4 {
public static void windowPosSum(int[] a, int n) {
/** your code here */
for (int i = 0; i < a.length; i++) {
if (a[i] < 0) {
continue;
}
if (i == a.length - 1) {
break;
}
for (int j = 1; j <= n; j++) {
if (i + j >= a.length){
break;
}
a[i] = a[i] + a[i + j];
}
}
}
public static void main(String[] args) {
int[] a = {1, 2, -3, 4, 5, 4};
int n = 3;
windowPosSum(a, n);
// Should print 4, 8, -3, 13, 9, 4
System.out.println(java.util.Arrays.toString(a));
}
}
|