简单判断案例— 分支结构的应用

 1 import java.util.Scanner;
 2 
 3 public class SingleQuestion {
 4     public static void main(String[] args) {
 5         boolean flag = false;
 6         //1.输出题目
 7         System.out.println("	1.软件开发最常用的模型是什么?
");
 8         System.out.println("A.瀑布模型	B.迭代模型	C.螺旋模型	D.其他
");//1.输出题目
 9         
10         //2.用户选择
11         System.out.println("请选择:");
12         Scanner sc = new Scanner(System.in);
13         String s = sc.next();
14         char answer = s.charAt(0);
15         //*int answer = sc.nextInt();//nextInt方法只能接受Int型的数据
16         
17         //3.判断答案(业务逻辑)
18         if(answer =='B'){
19             flag = true;//为了使判断答案的逻辑语句功能放大,而使用flag标记
20         }
21         //flag = (answer == 'B') ? true : false;
22         //4.输出是否正确
23         if(flag){
24             System.out.println("答案正确");
25         }
26         else{
27             System.out.println("答案错误");
28         }
29     }
30 
31 }

 

原文地址:https://www.cnblogs.com/YangGC/p/6032687.html