学校Java Week3
Week3
W3L1
public static void main(string[] args) {
System.out.println("hello world");
// ";"很重要
System.out.println(10+5);
}
Variables
Stored in RAM
public static void main(string[] args) {
int myLastOne;
int myTotal = 9;
int myLastOne = 1000 - myTotal;
System.out.println(myLastOne);
//PPT中的less:减去
}
Primitive Types Challenge
public static void main(string[] args) {
int i = 50;
long j = 50000 + 10*(10+20+50);
System.out.println(j);
}
Float and double
public static void main(string[] args) {
double pound = 8/0.45359237;
System.out.println(pound);
}
Char and boolean
public static void main(string[] args) {
}
String
W3L2 ex1
import java.util.Scanner;
public class W3L2_ex1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int y;
int m;
int d;
y = scanner.nextInt();
m = scanner.nextInt();
d = scanner.nextInt();
// double a = (double)y - (14-m)/12;
// double b = a + a/4 - a/100 + a/400;
// double c = (double)m + 12 * ((14-m) / 12) -2;
int a = (int)(y - (14-m)/12);
int b = (int)(a + a/4 - a/100 + a/400);
int c = (int)(m + 12 * ((14-m) / 12) -2);
int day = (int)(( d + b + (31*c)/12) % 7);
System.out.println("It's day " + day + " !");
}
}
W3L2 ex2
import java.util.Scanner;
public class W3L2_ex2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x = scanner.nextDouble();
double y = scanner.nextDouble();
double r = Math.sqrt(x*x + y*y);
double th = Math.atan2(y, x);
System.out.println("r = " + r);
System.out.println("theta = " + th);
}
}
W3 CW1 3.1
import java.util.Scanner;
public class W3_CW1_31 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double cyan = scanner.nextDouble();
double magenta = scanner.nextDouble();
double yellow = scanner.nextDouble();
double black = scanner.nextDouble();
double white = 1 - black;
long red = Math.round(255 * white * (1-cyan));
long green = Math.round(255 * white * (1-magenta));
long blue = Math.round(255 * white * (1-yellow));
System.out.println("red = " + red);
System.out.println("green = " + green);
System.out.println("blue = " + blue);
}
}
W3 CW1 3.2
import java.util.Scanner;
public class W3_CW1_32 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x1 = Math.toRadians(scanner.nextDouble());
double y1 = Math.toRadians(scanner.nextDouble());
double x2 = Math.toRadians(scanner.nextDouble());
double y2 = Math.toRadians(scanner.nextDouble());
double dx = Math.sin((x2-x1)/2);
double dy = Math.sin((y2-y1)/2);
double dist = 2 * 6371.0 * Math.asin(Math.sqrt(dx*dx + Math.cos(x1) * Math.cos(x2) * dy*dy));
System.out.println(dist + " kilometres");
}
}