当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

Complete collection of common error handling in MySQL installation
![[circuit design] convert AC AC to DC](/img/b4/67df7f4555379c63694e89055499bb.jpg)
[circuit design] convert AC AC to DC

(arxiv-2018) 重新审视基于视频的 Person ReID 的时间建模
![[UE4] replay game playback for ue4.26](/img/c3/1c7b30797f46dbd323cac4d158600f.png)
[UE4] replay game playback for ue4.26

数学建模——永冻土层上关于路基热传导问题

druid. io kill -9 index_ Realtime traceability task

How to crawl web pages with playwright?

自定义mvc原理和框架实现

What is the function of data parsing?

Read the recent trends of okaleido tiger and tap the value and potential behind it
随机推荐
TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)
awvs无法启动问题
Opencv image sharpness evaluation (camera autofocus)
JVM memory overflow online analysis dump file and online analysis open.Hprof file to get JVM operation report how jvisualvm online analysis
Mathematical modeling -- heat conduction of subgrade on Permafrost
[MySQL] SQL aliases the table
Implementation of 10m multifunctional signal generator with FPGA
IDEA 连接 数据库
webview攻击
The growth path of embedded engineers
Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number
Understand the clock tree in STM32 in simple terms
The number of consecutive subarrays whose leetcode/ product is less than k
Blind separation of speech signals based on ICA and DL
[one · data | chained binary tree]
Navigation--实现Fragment之间数据传递和数据共享
Leetcode 242. valid anagram
MySQL安装常见报错处理大全
Type analysis of demultiplexer (demultiplexer)
Probability Density Reweight