提交 6735f275 编写于 作者: M Mars Liu

add try and catch and function

上级 34b2e56d
......@@ -6,7 +6,7 @@
public class App {
public Parsec<Character, String> text(String txt){
return state -> {
if(String.equals(state.read(txt.legth), txt)){
if(state.read(txt.legth).equals(txt)){
return new Success(txt);
} else {
return new NotMatch(state);
......
{
"type": "code_options",
"author": "刘鑫",
"source": "ifelse.md",
"notebook_enable": false
}
\ No newline at end of file
# 逻辑判断
`Login` 类型中,我们需要提供一个 `Optional<String> login(String key)` 函数,在函数中,调用
`boolean check(String key)` 函数,如果 key 通过了校验,调用 `String gen(String key)`
生成一个字符串 token ,否则返回 null 。那么下列实现中有错的是?
## 答案
```java
public class Login {
public Optional<String> login(String key){
Optional<String> token;
if(!check(key))
token = Optional.empty();
return token;
else
token = Optional.of(gen(token));
return token;
}
//...
}
```
## 选项
### A
```java
public class Login {
public Optional<String> login(String key){
Optional<String> token;
if(check(key)){
token = Optional.of(gen(token));
} else {
token = Optional.empty();
}
return token;
}
//...
}
```
### B
```java
public class Login {
public Optional<String> login(String key){
Optional<String> token = Optinal.empty();
if(check(key)){
token = Optional.of(gen(token));
}
return token;
}
//...
}
```
### C
```java
public class Login {
public Optional<String> login(String key){
if(check(key)){
return Optional.of(gen(token));
} else {
return Optional.empty();
}
}
//...
}
```
{
"node_id": "java-09da728d4cac4f51bcb205b7170dd83b",
"keywords": [],
"children": [],
"export": [],
"title": "true和false"
}
\ No newline at end of file
......@@ -2,6 +2,9 @@
"node_id": "java-722dc1cccbc74369b9bff5ac580bc649",
"keywords": [],
"children": [],
"export": [],
"export": [
"for.json",
"foreach.json"
],
"title": "Foreach语法"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "for.md",
"exercise_id": "97fca3618efa4ff28b0f67688874401e",
"notebook_enable": false
}
\ No newline at end of file
# 循环
我们希望写一个简单的查找程序,在一个单词列表 `List<String>` 中查找匹配的单词,找到的话
返回其索引,否则返回 -1。正确的选项是
## 答案
```java
public class App() {
public int find(List<String> words, String word) {
for(int i=0, i < words.size(); i++){
if(word.equals(words.get(i))){
return i;
}
}
return -1;
}
}
```
## 选项
### A
```java
public class App() {
public int find(List<String> words, String word) {
for(int i=1, i <= words.size(); i++){
if(word.equals(words.get(i))){
return i;
}
}
return -1;
}
}
```
### B
```java
public class App() {
public int find(List<String> words, String word) {
int point = -1;
for(point=0, point <= words.size(); point++){
if(!word.equals(words.get(point))){
point = -1;
}
}
return point;
}
}
```
### C
```java
public class App() {
public int find(List<String> words, String word) {
int i = -1;
for(var w: words){
if(!word.equals(words.get(w))){
i++;
}
}
return point;
}
}
```
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "foreach.md",
"exercise_id": "b63ff9cb13f9499889a5618913bcb14a",
"notebook_enable": false
}
\ No newline at end of file
# Foreach
现在有一个 `List<String>` 列表,我们希望遍历它,得到其中有多少个单词,列表中的
每个元素可能包含一个或多个单词,单词间用空格分割,那么下面哪个实现是对的?
## 答案
```java
public class App {
public int words(List<String> content){
int count = 0;
for(var str: content){
var tokens = str.split(" ");
count += tokens.length;
}
return count;
}
}
```
## 选项
### A
```java
public class App {
public int words(List<String> content){
int count = 0;
for(int i=0; i< content.size(); i++){
count += i;
}
return count;
}
}
```
### B
public class App {
public int words(List<String> content){
int count = 0;
for(Character w: content){
if(w == ' '){
count += 1;
}
}
return count;
}
}
### C
public class App {
public int words(List<String> content){
int count = 0;
for(String w: content){
for(Character c: w){
if(w == ' '){
count += 1;
}
}
}
return count;
}
}
\ No newline at end of file
......@@ -2,6 +2,6 @@
"node_id": "java-05144fa9daf94a64ad0667eab1d6cc9a",
"keywords": [],
"children": [],
"export": [],
"export": ["switch.json"],
"title": "switch"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "switch.md",
"exercise_id": "7dd4bea864164e3b8ff5a5644c18a9cc",
"notebook_enable": false
}
\ No newline at end of file
# switch
我们希望编写一个函数,统计一个 `List<Integer>` 列表中的数值分布,其中 1
是 small,2 是 middle, 3 是 big,3以上都是 huge 。列表中的数值全部都在
1 到 5之间,下列哪个程序会在对象的 small, middle, big, huge 四个字段
保存正确的统计结果?
## 答案
```java
public class Counter{
int small = 0;
int middle = 0;
int bug = 0;
int huge = 0;
//... getter methods
public void read(List<Integer> numbers){
for(var value: numbers){
switch(value){
case 1:
small += 1;
break;
case 2:
middle += 1;
break;
case 3:
big += 1;
break;
default:
huge +=1;
}
}
}
}
```
## 答案
### A
```java
public class Counter{
int small = 0;
int middle = 0;
int bug = 0;
int huge = 0;
//... getter methods
public void read(List<Integer> numbers){
for(var value: numbers){
switch(value){
case 1:
small += 1;
case 2:
middle += 1;
case 3:
big += 1;
default:
huge +=1;
}
}
}
}
```
### B
```java
public class Counter{
int small = 0;
int middle = 0;
int bug = 0;
int huge = 0;
//... getter methods
public void read(List<Integer> numbers){
for(var value: numbers){
switch(value){
case 1:
small += 1;
return small;
case 2:
middle += 1;
return middle;
case 3:
big += 1;
return big;
default:
huge +=1;
return huge;
}
}
}
}
```
### C
```java
public class Counter{
int small = 0;
int middle = 0;
int bug = 0;
int huge = 0;
//... getter methods
public void read(List<Integer> numbers){
for(var value: numbers){
switch(value){
case 1:
small += 1;
return value;
case 2:
middle += 1;
return value;
case 3:
big += 1;
return value;
default:
huge +=1;
return value;
}
}
}
}
```
### D
```java
public class Counter{
int small = 0;
int middle = 0;
int bug = 0;
int huge = 0;
//... getter methods
public void read(List<Integer> numbers){
for(var value: numbers){
switch(value){
case 1:
small += 1;
break;
case 2:
middle += 1;
break;
case 3:
big += 1;
break;
default:
huge +=1;
}
}
}
}
```
\ No newline at end of file
{
"node_id": "java-73fd080a97c34ae2a89bd60c9510c409",
"keywords": [],
"children": [
{
"do-while": {
"keywords": [],
"children": [],
"node_id": "java-0238e72ddb834cde8c0059fb0a213d57",
"title": "do-while"
}
},
{
"逗号操作符": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "逗号操作符"
}
}
],
"export": [],
"title": "迭代"
}
\ No newline at end of file
......@@ -215,6 +215,6 @@
}
}
],
"export": [],
"export": ["exception.json", "using.json"],
"title": "通过异常处理错误"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "exception.md",
"exercise_id": "6f70218542f84189b8ea67018eb1e3fd",
"notebook_enable": false
}
\ No newline at end of file
# 异常
已知我们有一个特殊的文件类型 PopFile,这个类型的对象方法 read() 可以返回字符串
数据,当读取到末尾的时候,会抛出 Eof 异常,我们希望先通过 PopFile.open(path)
新建一个对象,并从这样一个 PopFile 对象中读取最多一百个文本数据,然后调用 close
方法关闭它。返回字符串列表`List<String>`。但是这个对象中的数据可能不足一百个。
此时我们需要在末尾添加一项 `[stop]`
下列选项正确的是:
## 答案
```java
public class App{
public List<String> Top100(String path){
PopFile popFile = PopFile.open(path);
List<String> result = new ArrayList();
try{
for(int i=0; i<100; i++){
result.add(popFile.read());
}
} catch(EofException eof){
result.add("[stop]");
} finally {
popFile.close();
}
return reseult;
}
}
```
## 选项
### A
```java
public class App{
public List<String> Top100(String path){
PopFile popFile = PopFile.open(path);
List<String> result = new ArrayList();
try{
for(int i=0; i<100; i++){
result.add(popFile.read());
}
return result;
} catch(EofException eof){
result.add("[stop]");
return result;
}
popFile.close();
}
}
```
### B
```java
public class App{
public List<String> Top100(String path){
PopFile popFile = PopFile.open(path);
List<String> result = new ArrayList();
try{
while(true){
result.add(popFile.read());
}
} catch(EofException eof){
if(result.size() < 100){
result.add("[stop]");
}
return result;
} finally {
popFile.close();
}
}
}
```
### C
```java
public class App{
public List<String> Top100(String path){
PopFile popFile = PopFile.open(path);
List<String> result = new ArrayList();
try{
for(int i=0; i<100; i++){
result.add(popFile.read());
}
return result;
} catch(EofException eof){
result.add("[stop]");
return result;
} finally {
popFile.close();
}
}
}
```
### D
```java
public class App{
public List<String> Top100(String path){
PopFile popFile = PopFile.open(path);
List<String> result = new ArrayList();
for(int i=0; i<100; i++){
result.add(popFile.read());
}
if(result.size() < 100){
result.add("[stop]");
}
popFile.close();
return result;
}
}
```
### E
```java
public class App{
public List<String> Top100(String path){
PopFile popFile = PopFile.open(path);
List<String> result = new ArrayList();
try{
for(int i=0; i<100; i++){
result.add(popFile.read());
}
if(result.size() < 100){
result.add("[stop]");
}
return result;
} catch(Excption error){
} finally {
popFile.close();
}
return result;
}
}
```
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "using.md",
"exercise_id": "2e7b0bb8e4fd4581aeba27815060cd73",
"notebook_enable": false
}
\ No newline at end of file
# using 模式
已知我们有一个特殊的文件类型 PopFile,这个类型的对象方法 read() 可以返回字符串
数据,当读取到末尾的时候,会抛出 Eof 异常,我们希望先通过 PopFile.open(path)
新建一个对象,并从这样一个 PopFile 对象中读取最多一百个文本数据,然后调用 close
方法关闭它。返回字符串列表`List<String>`。但是这个对象中的数据可能不足一百个。
此时我们需要在末尾添加一项 `[stop]`
如果我们想要用一下 `try using` 形式可靠的关闭 PopFile,需要满足什么条件?
```java
public class App{
public List<String> Top100(String path){
List<String> result = new ArrayList();
try(PopFile popFile = PopFile.open(path)){
for(int i=0; i<100; i++){
result.add(popFile.read());
}
} catch(EofException eof){
result.add("[stop]");
}
return reseult;
}
}
```
## 答案
实现 `java.io.Closeable` 接口。
## 选项
### A
实现 `void close()` 方法。
### B
实现 `boolean close()` 方法.
### C
实现 `java.io.AutoClosable` 接口。
### D
实现 `finally()` 方法。
### E
实现 `exit()` 方法。
\ No newline at end of file
{
"node_id": "java-66019537247040fcbb6d74a29f47819f",
"keywords": [],
"children": [],
"export": [],
"title": "return"
}
\ No newline at end of file
......@@ -2,5 +2,5 @@
"node_id": "java-f2482f12e0634885bdf6701a5a802c77",
"keywords": [],
"children": [],
"export": []
"export": ["try.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "try.md",
"exercise_id": "6d8116aa4c254e808bc1c2aa33afdd4f",
"notebook_enable": false
}
\ No newline at end of file
# Try 封装
我们的项目中有一个预备好的 `Try<T>` 类型,它可以携带正常对象或异常,只需要调用对应的静态方法
`Try.success(T result)` 或者 `Try.failure(Throwable err)` 即可。
现在我们希望为类型
```java
public abstract class Parser<E, T> {
public abstract T parse(E element);
}
```
类编写一个静态的 `<T> Parser<Try<T>> pack(Parser<T> E elemenmt)` 方法,它将传
入的 parser 封装,返回一个新的,不会抛出异常的,而是返回 Try 类型的 `Parser<E, Try<T>>` 对象,下面
正确的是
## 答案
```java
public abstract class Parser<E, T> {
public abstract T parse(E element);
public static Parser<E, Try<T>> pack(Parser<E, T> parser) {
return element -> {
try {
return Try.success(parser.parse(element));
} catch(Throwable error){
return Try.failure(error);
}
};
}
}
```
## 选项
### A
```java
public abstract class Parser<E, T> {
public abstract T parse(E element);
public static Try<T> pack(Parser<E, T> parser) {
return element -> {
Try<T> result;
try {
result = Try.success(parser.parse(element));
} catch(Throwable error){
result = Try.failure(error);
} finally {
return result;
}
};
}
}
```
### B
```java
public abstract class Parser<E, T> {
public T parse(E element);
public Try<T> pack(Parser<E, T> parser) {
return element -> {
Try<T> result;
try {
result = Try.success(parser.parse(element));
} catch(Throwable error){
result = Try.failure(error);
} finally {
return result;
}
};
}
}
```
### C
```java
public abstract class Parser<E, T> {
public abstract T parse(E element);
public static Parser<E, Try<T>> pack(Parser<E, T> parser) {
return element -> {
var result = parser.parse(element);
if(result instanceof Throwable){
return Try.failure(result);
} else {
return Try.success(result);
}
};
}
}
```
### D
```java
public abstract class Parser<E, T> {
public abstract T parse(E element);
public static Parser<E, Try<T>> pack(Parser<E, T> parser) {
return element -> {
try {
var result = parser.parse(element);
if(result instanceof Throwable){
return Try.failure(result);
} else {
return Try.success(result);
}
} catch(Throwable error){
return Try.failure(error);
}
};
}
}
```
\ No newline at end of file
{
"node_id": "java-1b0927bc3a3342869b4fc981abf17ff6",
"keywords": [],
"children": [],
"export": [],
"title": "break和continue"
}
\ No newline at end of file
......@@ -672,13 +672,6 @@
"node_id": "java-05024fbfd53a4d88978d6c78d7ff06bf",
"keywords": [],
"children": [
{
"true和false": {
"node_id": "java-09da728d4cac4f51bcb205b7170dd83b",
"keywords": [],
"children": []
}
},
{
"if-else": {
"node_id": "java-85a126a6ba39488aaabd2d4015353175",
......@@ -687,50 +680,12 @@
}
},
{
"迭代": {
"node_id": "java-73fd080a97c34ae2a89bd60c9510c409",
"keywords": [],
"children": [
{
"do-while": {
"keywords": [],
"children": [],
"node_id": "java-0238e72ddb834cde8c0059fb0a213d57",
"title": "do-while"
}
},
{
"逗号操作符": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "逗号操作符"
}
}
]
}
},
{
"Foreach语法": {
"for": {
"node_id": "java-722dc1cccbc74369b9bff5ac580bc649",
"keywords": [],
"children": []
}
},
{
"return": {
"node_id": "java-66019537247040fcbb6d74a29f47819f",
"keywords": [],
"children": []
}
},
{
"break和continue": {
"node_id": "java-1b0927bc3a3342869b4fc981abf17ff6",
"keywords": [],
"children": []
}
},
{
"switch": {
"node_id": "java-05144fa9daf94a64ad0667eab1d6cc9a",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册