复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49package com.WenJun; import java.util.ArrayList; import java.util.Scanner; public class ATMSystem { public static void main(String[] args) { // 1.显示页面 while (true) { System.out.println("=====欢迎来到ATM系统====="); System.out.println("1.登录账户"); System.out.println("2.注册账户"); System.out.println("请选择您所需要的服务"); // 选择相应服务 Scanner sc = new Scanner(System.in); ArrayList<Object> accounts = new ArrayList(); int firstSelectNumber = sc.nextInt(); // sc: 填写1或2 // 2.进入下一页面 switch (firstSelectNumber){ // 进入登录界面 case 1: System.out.println("=====欢迎您进入到登录界面====="); break; // 进入注册界面 case 2: System.out.println("=====欢迎您进入到开户界面====="); System.out.println("请您输入账户名称:"); String accountName = sc.next(); while (true) { System.out.println("请您输入账户密码"); int accountPassword = sc.nextInt(); System.out.println("请再次输入密码"); int comfirmPassword = sc.nextInt(); if (accountPassword == comfirmPassword){ Account theAccount = new Account(accountName, accountPassword); accounts.add(theAccount); System.out.println("恭喜您开户成功!"); accounts.add(theAccount); //break loop1; break; }else{ System.out.println("两次输入的密码不相同,请重新输入"); } } // 3.隐藏管理系统 case 3: System.out.println("若出现,说明执行的的是while"); } } } }
上述代码为ATM系统的部分代码,注册账户后会出现文字,故产生疑问,抱着好奇的想法试了试,故写了篇笔记。
非完整代码,但是可以运行;
运行结果:
结论:Switch中嵌套while时,while中的break会执行while。
最后
以上就是悦耳小松鼠最近收集整理的关于[Java基础]Switch中嵌套while时,while中的break会执行Switch还是while?的全部内容,更多相关[Java基础]Switch中嵌套while时,while中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复