此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
使用对象
- 对象(object)是一个会做事情的值。你用点号
.来调用它的方法(method)。 String就是一个对象。Math、Integer和Double给你现成的工具。- 在这节课里,你学习如何创建对象并使用它们的方法。
用 new 创建对象
- 有些对象用关键字
new和一个构造器(constructor)来创建。 String很特别:你可以直接用引号写它,例如"hi"。- 您也可以创建单独的
String对象并使用new String("..."),但引号是常规方式。
English
public class Main {
public static void main(String[] args) {
String greeting = "Hello";
String copy = new String("Hello");
System.out.println(greeting);
System.out.println(copy);
}
}
String 的方法
s.length()——s里有多少个字符。s.charAt(i)—— 位置i上的字符(第一个位置是 0)。s.substring(a, b)—— 从a开始到 但不包括b的部分。s.indexOf("x")——"x"第一次出现的位置,找不到就返回-1。
English
public class Main {
public static void main(String[] args) {
String word = "computer";
System.out.println(word.length()); // 8
System.out.println(word.charAt(0)); // c
System.out.println(word.substring(0, 4)); // comp
System.out.println(word.indexOf("put")); // 3
}
}
比较字符串
- 用
s.equals(other)来检查两个字符串是否有相同的字母。 - 不要用
==来比较字符串 —— 它可能给出错误的答案。我们在第 3 课学习原因。 s.compareTo(other)相等时返回0,如果s在顺序上更靠前就返回负数,更靠后就返回正数。
English
public class Main {
public static void main(String[] args) {
String a = "cat";
String b = "cat";
String c = "dog";
System.out.println(a.equals(b)); // true
System.out.println(a.equals(c)); // false
System.out.println(a.compareTo(c)); // negative: "cat" is before "dog"
}
}
Math 的方法
Math装着数学工具。你在Math本身上调用它们,例如Math.abs(-5)。Math.abs(x)——x去掉负号后的大小。Math.pow(b, e)——b的e次方(返回一个double)。Math.sqrt(x)——x的平方根(返回一个double)。Math.random()— 从0.0到1.0(不含)之间的随机double整数。
English
public class Main {
public static void main(String[] args) {
System.out.println(Math.abs(-7)); // 7
System.out.println(Math.pow(2, 3)); // 8.0
System.out.println(Math.sqrt(9)); // 3.0
// Math.random() changes each run, so we do not print it here.
}
}
包装类与自动装箱
int和double是简单类型。它们的对象版本是Integer和Double。- Java 可以替你在两者之间转换。这叫作自动装箱(autoboxing)。
Integer.parseInt("42")把文字变成int。Double.parseDouble("3.5")把文字变成double。
English
public class Main {
public static void main(String[] args) {
Integer boxed = 10; // int -> Integer (autoboxing)
int back = boxed; // Integer -> int (unboxing)
int n = Integer.parseInt("42");
double d = Double.parseDouble("3.5");
System.out.println(back + n); // 52
System.out.println(d); // 3.5
}
}
常见错误
- 比较字符串(和其他对象)用
.equals(),不是==。 Math.random()返回从0(含)到1(不含)之间的值。
现在轮到你
- 每个任务都已经填好了类的骨架 —— 把你的代码写在 main 里面,或者补全给出的方法。
- 对于完整程序,Run(运行)会编译并执行它。对于方法或类任务,Check answer(检查答案)会使用提供的测试编译您的代码并显示结果。
- 你的代码在服务器上编译并运行,所以第一次运行也很快。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The String word is(是) "programming". Print its length on the first line, then print the first 4 letters (use substring(0, 4)) on the second line.
点击“运行”查看此处输出。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Complete hypotenuse(double a, double b) so it returns the length of the longest side of a right triangle: the square root of a*a + b*b. Use Math.sqrt and Math.pow (or a*a).
点击“运行”查看此处输出。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Complete sameWord(String a, String b) so it returns true when the two Strings have the same letters, and false otherwise. Use .equals (not ==).
点击“运行”查看此处输出。