当前位置:网站首页>Character flow comprehensive exercise problem solving process
Character flow comprehensive exercise problem solving process
2022-07-29 02:16:00 【Wang Xiaoya】
stem :
( Practice questions ) Use the input stream to read the test question file , Each time a question in the question file is displayed . Read the character “*” Pause reading when , Waiting for the user to enter an answer from the keyboard . After the user has finished all the questions . The program gives the user a score (10 Divide a question ).
1) The test questions are as follows :
(1) Hero alliance S9 When did the global finals open ?
A.2018-08-08 B. 2018-08-01
C.2019-10-01 D. 2019-10-02
********************
(2) Which of the following heroes does not belong to 《 Hero alliance 》?
A. carter B. Blind monk C. Jianhao D. Hou Yi
********************
(3). Which of the following is not the line of gust swordsman Yasso ?
A. The wind of death , Always my body .B. Face the wind !C. A true master , Always with an apprentice heart .D. Hassel gave !
********************
(4). Hero League World S Sai Bei OMG base 50 The blood turning team is ?
A.SKT B.Najin C.FNC D.G2
********************
(5) In the League of Heroes , The most successful hero is ?
A Swift scout B Steam robot C The power of demacia D The strong wind sword
********************
-------》 The program runs as follows :
(1) Hero alliance S9 When did the global finals open ?
A.2018-08-08 B. 2018-08-01
C.2019-10-01 D. 2019-10-02
Enter the selected answer (A、B、C、D):D
(2) Which of the following heroes does not belong to 《 Hero alliance 》?
A. carter B. Blind monk C. Jianhao D. Hou Yi
Enter the selected answer (A、B、C、D):D
...
...
Complete the test questions , congratulations Your final score is :20 branch .
*/
Print it first , Console printing succeeded

Add judgment statement , Make it appear * Pause reading when , Enter the answer , Give it a trial run

Basically achieve the effect we want , But when I output answers in lowercase letters , No matching value found , So even if my answer is correct, I won't score , So you can write an output error statement .
java in “==” Use review of :
Because in Java in , If basic data type , be == Compare values ; If it's an object type , be == It compares the address of the object .
// Basic data type == The judgment is the value
String str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2);Basic data type ,== The judgment is the value , The result is true.
// Reference data type ,== The judge is the address they store in memory
String str1 = new String("hi");
String str2 = new String("hi");
System.out.println(str1 == str2);
Reference data type ,== The judge is the address they store in memory , The result is false.
The previous statement
/ use String To store
String line;
while ((line = br.readLine()) != null) {
br.lines();
if (!line.startsWith("*")) {
System.out.println(line);
} else {
System.out.println(" Enter the selected answer (A、B、C、D):");
String choice = sc.next();
// Splice input answers
result = sb.append(choice.charAt(0)).toString();
}
}
// Store total score
int sum = 0;
for (int i = 0; i < result.length(); i++) {
// Compare input results with answers
if (result.charAt(i) == anser.charAt(i)) {
sum += 10;
}
}
System.out.println(" Complete the test questions , congratulations Your final score is :" + sum + " branch .");

After improvement :

// use String To store
String line;
while ((line = br.readLine()) != null) {
br.lines();
if (!line.startsWith("*")) {
System.out.println(line);
} else {
// Join a dead circle , Ensure that the wrong input can continue to run
boolean flag = true;
while (flag) {
System.out.println(" Enter the selected answer (A、B、C、D):");
String choice = sc.next();
// Select the input content and compare it with the set answer , All satisfied with “ It's not equal to A/B/C/D” Conditions
if (!choice.equals("A") && !choice.equals("B") && !choice.equals("C") && !choice.equals("D")) {
System.out.println(" The answer you entered is incorrect , Please try again ");
} else {
// Splice input answers
result = sb.append(choice.charAt(0)).toString();
flag = false; // End cycle
}
}
}
}Complete operation is available :
(1) Hero alliance S9 When did the global finals open ?
A.2018-08-08 B. 2018-08-01
C.2019-10-01 D. 2019-10-02
Enter the selected answer (A、B、C、D):
a
The answer you entered is incorrect , Please try again
Enter the selected answer (A、B、C、D):
A
(2) Which of the following heroes does not belong to 《 Hero alliance 》?
A. carter B. Blind monk C. Jianhao D. Hou Yi
Enter the selected answer (A、B、C、D):
D
(3). Which of the following is not the line of gust swordsman Yasso ?
A. The wind of death , Always my body .B. Face the wind !C. A true master , Always with an apprentice heart .D. Hassel gave !
Enter the selected answer (A、B、C、D):
C
(4). Hero League World S Sai Bei OMG base 50 The blood turning team is ?
A.SKT B.Najin C.FNC D.G2
Enter the selected answer (A、B、C、D):
B
(5) In the League of Heroes , The most successful hero is ?
A Swift scout B Steam robot C The power of demacia D The strong wind sword
Enter the selected answer (A、B、C、D):
D
Complete the test questions , congratulations Your final score is :30 branch .
Process ended , Exit code 0
The complete code is as follows :
package com.B.IOStream_14.CharDemo03.Ask;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
//( Practice questions ) Use the input stream to read the test question file , Each time a question in the question file is displayed .
// Read the character “*” Pause reading when , Waiting for the user to enter an answer from the keyboard . After the user has finished all the questions .
// The program gives the user a score (10 Divide a question ).
public class A4 {
public static void main(String[] args) throws IOException {
// Create an input stream object
BufferedReader br = new BufferedReader(new FileReader("Temp\\ Q & A questions .txt"));
// BufferedWriter bw = new BufferedWriter(new FileWriter("Temp\\itDemo\\array1.txt"));
// StringBuilder Splice input answers
StringBuilder sb = new StringBuilder();
// Keyboard entry
Scanner sc = new Scanner(System.in);
// Set the answer
String anser = "DDCCD";
// Initialization selection
String result = null;
// Read the test question file
// char[] chs = new char[1024];
// int len;
//while ((len= br.read(chs)) != -1){
// System.out.println(new String(chs,0,len));
//}
// use String To store
String line;
while ((line = br.readLine()) != null) {
br.lines();
if (!line.startsWith("*")) {
System.out.println(line);
} else {
// Join a dead circle , Ensure that the wrong input can continue to run
boolean flag = true;
while (flag) {
System.out.println(" Enter the selected answer (A、B、C、D):");
String choice = sc.next();
// Select the input content and compare it with the set answer , All satisfied with “ It's not equal to A/B/C/D” Conditions
if (!choice.equals("A") && !choice.equals("B") && !choice.equals("C") && !choice.equals("D")) {
System.out.println(" The answer you entered is incorrect , Please try again ");
} else {
// Splice input answers
result = sb.append(choice.charAt(0)).toString();
flag = false; // End cycle
}
}
}
}
// Store total score
int sum = 0;
for (int i = 0; i < result.length(); i++) {
// Compare input results with answers
if (result.charAt(i) == anser.charAt(i)) {
sum += 10;
}
}
System.out.println(" Complete the test questions , congratulations Your final score is :" + sum + " branch .");
// Release resources
br.close();
}
}
边栏推荐
- What is a proxy server? [2022 guide]
- Implementation of 10m multifunctional signal generator with FPGA
- Motionlayout -- realize animation in visual editor
- 控制输入框弹出弹窗 和不弹出窗口
- Solution of Lenovo notebook camera unable to open
- Understand the clock tree in STM32 in simple terms
- 【RT学习笔记1】RT-Thread外设例程——控制Led灯闪烁
- 2022.7.28-----leetcode.1331
- [cloud native] what is the microservice architecture
- Have you ever encountered the situation that the IP is blocked when crawling web pages?
猜你喜欢

自定义mvc原理和框架实现
![[circuit design] open collector OC output of triode](/img/48/5a111b81f0d99990fbcc5263313c07.jpg)
[circuit design] open collector OC output of triode

Comprehensive explanation of "search engine crawl"

FPGA实现10M多功能信号发生器

The solution of reducing the sharpness of pictures after inserting into word documents
![[electronic components] constant voltage, amplify the current of the load (triode knowledge summary)](/img/07/d5861404a76a0fb7d6d5f930a4a66a.png)
[electronic components] constant voltage, amplify the current of the load (triode knowledge summary)

Mathematical modeling -- the laying of water pipes

Lm13 morphological quantification momentum period analysis

数学建模——带相变材料的低温防护服御寒仿真模拟

特殊流&Properties属性集实例遇到的问题及解决方法
随机推荐
[cloud native and 5g] micro services support 5g core network
druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11
Leetcode 242. valid anagram
忽略微信设置字体
leetcode/乘积小于K 的连续子数组的个数
Semiconductor chip industry chain
Resolve the conflict with vetur when using eslint, resulting in double quotation marks and comma at the end of saving
Related function records about string processing (long-term update)
mobile-picker.js
What is the function of data parsing?
The growth path of embedded engineers
Opencv image sharpness evaluation (camera autofocus)
2022.7.27-----leetcode.592
【ONE·Data || 数组堆简单实现及其延伸】
What is browser fingerprint recognition
Ignore wechat font settings
Mathematical modeling -- red wine quality classification
Blind separation of speech signals based on ICA and DL
leetcode 242. Valid Anagram(有效的字母异位词)
What is a proxy server? [2022 guide]