编码练习——Java-2-流程控制-程序员宅基地

技术标签: Java  练习  

博客新址: http://blog.xuezhisd.top
邮箱:[email protected]


条件

  • 条件语句 - if
public class Test {
	public static void main(String[] args) {
		int a = 100;
		if(a==100) {
			System.out.println("a的值是 100");
		}
	}
}
public class Test {
	public static void main(String[] args) {
		int x = 45;
		int y = 12;
		if (x>y) {
			System.out.println("变量x 大于 变量y");
		}
		if (x<y) {
			System.out.println("变量x 小于 变量y");
		}
	}
}
public class Test {
	public static void main(String[] args) {
		int math = 95;
		if (math>60) {
			System.out.println("数学及格了");
		} else {
			System.out.println("数学没有及格");
		}
	}
}
public class Test {
    
	public static void main(String[] args) {
    
		int x = 20;
		if(x>30) {
    
			System.out.println("a的值大于30");
		} else if (x>10) {
    
			System.out.println("a的值大于10,但小于30");
		} else if (x>0) {
    
			System.out.println("a的值大于0,但小于10");
		} else {
    
			System.out.println("a的值小于0");
		}
	}
}
  • 条件语句 - switch
public class Test {
	public static void main(String[] args) {
		System.out.println("今天是星期几:");
		int week = 2;
		switch(week) {
		case 1:
			System.out.println("Monday");
			break;
		case 2:
			System.out.println("Tuesday");
			break;
		case 3:
			System.out.println("Wednesday");
			break;
		default:
			System.out.println("Sorry, I don't know");
				
		}
	}
}

  • 实例 - 验证登录信息的合法性
import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入登录用户名:");
		String username = scan.nextLine();
		System.out.println("请输入登录密码:");
		String password = scan.nextLine();
		
		if (!username.equals("mr")) {
			System.out.println("用户名非法");
		} else if (!password.equals("mrsoft")) {
			System.out.println("登录密码错误");
		} else {
			System.out.println("恭喜您, 登录信息通过验证");
		}
	}
}
  • 实例 - 为新员工分配部门
import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入新员工的姓名:");
		String name = scan.nextLine();
		System.out.println("请输入新员工应聘的编程语言:");
		String language = scan.nextLine();
		
		switch(language.hashCode()) {
		case 3254818:
		case 2301506:
		case 2269730:
			System.out.println("员工" + name + "被分配到Java程序开发部门。");
			break;
		case 3104:
		case 2112:
			System.out.println("员工" + name + "被分配到C#项目维护组。");
			break;
		case -709190099:
		case 955463181:
		case 9745901:
			System.out.println("员工"+name+"被分配到Asp.net程序测试部门。");
			break;
		default:
			System.out.println("本公司不需要" + language + "语言的程序开发人员。");
		}
		
	}
}

循环

  • 循环语句 - while
public class Test {
	public static void main(String[] args) {
		int x = 1;
		int sum = 0;
		while (x<=10) {
			sum = sum+x;
			x++;
		}
		System.out.println("从1到10的和是:" + sum);
		
	}
}
  • 循环语句 - do…while
    • 循环体,至少执行一次。
public class Test {
    
	public static void main(String[] args) {
    
		int a = 100;
		while (a==60) {
    	//一遍不执行
			System.out.println("Ok! a==60");
			a--;
		}
		
		int b = 100;
		do {
    	//至少执行一次
			System.out.println("Ok! b==60");
			b--;
		}
		while (b==60);
		
	}
}
  • 循环语句 - for
public class Test {
    
	public static void main(String[] args) {
    
		int sum = 0;
		for(int i=1; i<=100; i+=1) {
    
			sum = sum+i;
		}
		System.out.println("1到100之间数的和为:" + sum);
	}
}
  • 循环语句 -foreach
public class Test {
    
	public static void main(String[] args) {
    
		int arr[] = {
    7, 10, 1};
		System.out.println("一维数组中的元素分别为:");
		for (int x : arr) {
    
			System.out.print(x+"\t");
		}
	}
}
  • 实例 - 使用while循环遍历数组
public class Test {
    
	public static void main(String[] args) {
    
		String[] aves = new String[] {
    "白露", "丹顶鹤", "黄鹂", "乌鸦", "喜鹊", "布谷鸟", "灰文鸟", "百灵鸟"};
		int index = 0;
		System.out.println("我的花园里有很多鸟,种类有:");
		while (index < aves.length) {
    
			System.out.print(aves[index++] + "   ");
		}
	}
}
  • 实例 - 使用for循环输出九九乘法表
public class Test {
    
	public static void main(String[] args) {
    
		for (int i=1; i<=9; i++) {
    
			for (int j=1; j<=i; j++) {
    
				System.out.print(j + "*" + i + "=" + (i*j) + "\t");
			}
			System.out.println();
		}
		
	}
}
  • 跳转语句 - break
public class Test {
    
	public static void main(String[] args) {
    
		int sum = 0;
		String flag = "从1到100之间连续整数的和是:";
		for (int i=1; i<=100; i++) {
    
			sum +=i;
			if(sum>1000) {
    
				flag = "从1到" + i + "之间连续整数的和是:";
				//跳出该层循环
				break;
			}
		}
		System.out.println(flag + sum);
		
	}
}

  • 跳转语句 - continue
public class Test {
    
	public static void main(String[] args) {
    
		int i = 0;
		System.out.println("10以内的奇数有:");
		while (i<10) {
    
			i++;
			if (i%2 == 0) {
    
				continue;
			}
			System.out.print(i + "  ");
		}
	}
}
  • 跳转语句 - return

  • 实例 - 终止循环体(break)
public class Test {
    
	public static void main(String[] args) {
    
		System.out.println("\n----中断单层循环的例子----");
		String[] array = new String[] {
    "白露","丹顶鹤","黄鹂","鹦鹉","乌鸦","喜鹊","老鹰","布谷鸟","老鹰","灰文鸟","老鹰","百灵鸟"};
		for (String string : array) {
    
			if (string.equalsIgnoreCase("老鹰"))
				break;
			System.out.print("有:" + string + "   ");
		}
		
		System.out.println("\n\n----中单2层循环的例子----");
		int[][] myScores = new int[][] {
    {
    67,78,63,22,66}, {
    55,68,78,95,44},{
    95,97,92,93,81}};
		System.out.println("宝宝这次考试成绩:\n数学\t语文\t英语\t美术\t历史");
		No1: for (int[] is : myScores) {
    
			for (int i : is) {
    
				System.out.print(i + "\t");
				if(i<60) {
    
					System.out.println("\n等等," + i + "分的是什么?这个为什么不及格?");
					break No1;
				}
			}
			System.out.println();
		}
		
	}
}
  • 实例 - 循环体的过滤器(continue)
public class Test {
    
	public static void main(String[] args) {
    
		String[] array = new String[] {
    "白露","丹顶鹤","黄鹂","鹦鹉","乌鸦","喜鹊","老鹰","布谷鸟","老鹰","灰文鸟","老鹰","百灵鸟"};
		System.out.println("在我的花园里有很多鸟类,但是最近来了几只老鹰,请帮我把他们抓走。");
		int eagleCount = 0;
		for(String string : array) {
    
			if(string.equals("老鹰")) {
    
				System.out.println("发现一只老鹰,已经抓到笼子里。");
				eagleCount++;
				continue;
			}
			System.out.println("搜索鸟类,发现了:" + string);
		}
		System.out.println("一共捉到了:" + eagleCount + "只老鹰。");
	}
}
  • 实例 - 使用for循环输出空心的菱形
public class Test {
    
	public static void main(String[] args) {
    
		printHollowRhombus(10);
	}
	
	public static void printHollowRhombus(int size) {
    
		if (size%2==0) 
			size++;
		for (int i=0; i<size/2+1; i++) {
    
			for(int j=size/2; j>i+1; j--) {
    
				System.out.print(" ");
			}
			for (int j=0; j<2*i+1; j++) {
    
				if(j==0 || j==2*i) 
					System.out.print("*");
				else
					System.out.print(" ");
			}
			System.out.println("");
		}
		for(int i=size/2+1; i<size; i++) {
    
			for(int j=0; j<i-size/2; j++) {
    
				System.out.print(" ");
			}
			for(int j=0; j<2*size-1-2*i; j++) {
    
				if(j==0 || j==2*(size-i-1))
					System.out.print("*");
				else
					System.out.print(" ");
			}
			System.out.println("");
		}
	}
}
  • 实例 - 使用for循环输出杨辉三角
public class Test {
    
	public static void main(String[] args) {
    
		int triangle[][] = new int[8][];
		for (int i=0; i<triangle.length; i++) {
    
			triangle[i] = new int[i+1];
			for(int j=0; j<=triangle[i].length-1; j++) {
    
				if(i==0||j==0||j==triangle[i].length-1)
					triangle[i][j] = 1;
				else
					triangle[i][j] = triangle[i-1][j] + triangle[i-1][j-1];
				System.out.print(triangle[i][j] + "\t");
			}
			System.out.println();
		}
	}
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xuezhisdc/article/details/52185229

智能推荐

如何配置filezilla服务端和客户端_filezilla server for windows (32bit x86)-程序员宅基地

文章浏览阅读7.8k次,点赞3次,收藏9次。如何配置filezilla服务端和客户端百度‘filezilla server’下载最新版。注意点:下载的版本如果是32位的适用xp和win2003,百度首页的是适用于win7或更高的win系统。32和64内容无异。安装过程也是一样的。一、这里的filezilla包括服务端和客户端。我们先来用filezilla server 架设ftp服务端。看步骤。1选择标准版的就可以了。 _filezilla server for windows (32bit x86)

深度学习图像处理01:图像的本质-程序员宅基地

文章浏览阅读724次,点赞18次,收藏8次。深度学习作为一种强大的机器学习技术,已经成为图像处理领域的核心技术之一。通过模拟人脑处理信息的方式,深度学习能够从图像数据中学习到复杂的模式和特征,从而实现从简单的图像分类到复杂的场景理解等多种功能。要充分发挥深度学习在图像处理中的潜力,我们首先需要理解图像的本质。本文旨在深入探讨深度学习图像处理的基础概念,为初学者铺平通往高级理解的道路。我们将从最基础的问题开始:图像是什么?我们如何通过计算机来理解和处理图像?

数据探索阶段——对样本数据集的结构和规律进行分析_数据分析 规律集-程序员宅基地

文章浏览阅读62次。在收集到初步的样本数据之后,接下来该考虑的问题有:(1)样本数据集的数量和质量是否满足模型构建的要求。(2)是否出现从未设想过的数据状态。(3)是否有明显的规律和趋势。(4)各因素之间有什么样的关联性。解决方案:检验数据集的数据质量、绘制图表、计算某些特征量等,对样本数据集的结构和规律进行分析。从数据质量分析和数据特征分析两个角度出发。_数据分析 规律集

上传计算机桌面文件图标不见,关于桌面上图标都不见了这类问题的解决方法-程序员宅基地

文章浏览阅读8.9k次。关于桌面上图标都不见了这类问题的解决方法1、在桌面空白处右击鼠标-->排列图标-->勾选显示桌面图标。2、如果问题还没解决,那么打开任务管理器(同时按“Ctrl+Alt+Del”即可打开),点击“文件”→“新建任务”,在打开的“创建新任务”对话框中输入“explorer”,单击“确定”按钮后,稍等一下就可以见到桌面图标了。3、问题还没解决,按Windows键+R(或者点开始-->..._上传文件时候怎么找不到桌面图标

LINUX 虚拟网卡tun例子——修改_怎么设置tun的接收缓冲-程序员宅基地

文章浏览阅读1.5k次。参考:http://blog.csdn.net/zahuopuboss/article/details/9259283 #include #include #include #include #include #include #include #include #include #include #include #include _怎么设置tun的接收缓冲

UITextView 评论输入框 高度自适应-程序员宅基地

文章浏览阅读741次。创建一个inputView继承于UIView- (instancetype)initWithFrame:(CGRect)frame{ self = [superinitWithFrame:frame]; if (self) { self.backgroundColor = [UIColorcolorWithRed:0.13gre

随便推点

字符串基础面试题_java字符串相关面试题-程序员宅基地

文章浏览阅读594次。字符串面试题(2022)_java字符串相关面试题

VSCODE 实现远程GUI,显示plt.plot, 设置x11端口转发_vscode远程ssh连接服务器 python 显示plt-程序员宅基地

文章浏览阅读1.4w次,点赞12次,收藏21次。VSCODE 实现远程GUI,显示plt.plot, 设置x11端口转发问题服务器 linux ubuntu16.04本地 windows 10很多小伙伴发现VSCode不能显示figure,只有用自带的jupyter才能勉强个截图、或者转战远程桌面,这对数据分析极为不方便。在命令行键入xeyes(一个显示图像的命令)会failed,而桌面下会出现:但是Xshell能实现X11转发图像,有交互功能,但只能用Xshell输入命令plot,实在不方便。其实VScode有X11转发插件!!方法_vscode远程ssh连接服务器 python 显示plt

element-ui switch开关打开和关闭时的文字设置样式-程序员宅基地

文章浏览阅读3.3k次,点赞2次,收藏2次。element switch开关文字显示element中switch开关把on-text 和 off-text 属性改为 active-text 和 inactive-text 属性.怎么把文字描述显示在开关上?下面就是实现方法: 1 <el-table-column label="状态"> 2 <template slot-scope="scope">..._el-switch 不同状态显示不同字

HttpRequestUtil方法get、post、JsonToPost_httprequestutil.httpget-程序员宅基地

文章浏览阅读785次。java后台发起请求使用的工具类package com.cennavi.utils;import org.apache.http.Header;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apach_httprequestutil.httpget

App-V轻量级应用程序虚拟化之三客户端测试-程序员宅基地

文章浏览阅读137次。在前两节我们部署了App-V Server并且序列化了相应的软件,现在可谓是万事俱备,只欠东风。在这篇博客里面主要介绍一下如何部署客户端并实现应用程序的虚拟化。在这里先简要的说一下应用虚拟化的工作原理吧!App-V Streaming 就是利用templateServer序列化出一个软件运行的虚拟环境,然后上传到app-v Server上,最后客户..._app-v 客户端