内容简介:Java使用条件语句和循环结构确定控制流
与任何程序设计语言一样,Java使用条件语句和循环结构确定控制流。本文将简单讲解条件、循环和switch。
一、块作用域
块(block),即复合语句。是指由一对大括号括起来的若干条简单的 Java 语句。块确定了变量的作用域。
比如:
public class Code {
static
{
System.out.println("1");
}
{
System.out.println("2");
}
public Code()
{
System.err.println("3");
}
public static void main(String[]args)
{
new Code();
}
}
注意:不能在嵌套的两个块中声明同名的变量。
二、条件语句
格式1:
if(condition)
{
statement1
statement2
........
}
例如:
1 if(youSales>=target)
2 {
3 performance="Satisfactory";
4 bonus=1000;
5 }
格式2:
if(condition)statement1 else statement2
例如:
if(youSales>=target)
{
performance=“Satisfactory”;
bonus=100+10*(youSales-target“);
}
else
{
performance=”Unstatisfactory“;
bonus=0;
}
三、循环
当条件为true时,while循环执行。
格式1:
while(condition)statemnet
例如:
while (balance<goal)
{
balance+=payment;
double interest=balance*interestRate/100;
balance+=interest;
years++;
}
格式2:
do statement while(condition);
do
{
balance+=payment;
double interest=balance*interestRate/100;
balance+=interest;
year++;
System.out.printf("After year %d,your balance is %,.2f%,year,balance");
System.out.print("Ready to retire?(Y/N)");
input=in.next();
}
while(input.equals("N"));
}
四、确定循环
for循环语句是支持迭代的一种通用结构,利用每次迭代之后更新的计数器或类似的变量来控制迭代的次数。
格式类似如下:
for(int i=0;i<x.length;i++)
System.out.println(i);
例子4个:
public class ShuZu1 {
public static void main(String[]args){
int [][] x={{1,2,2,2,2},{3,3,3,3,3},{4,5,-1,17,55}};
int result=qiuHe(x);
System.out.println("和是"+result);
}
public static int qiuHe(int[][]x){
int s=0;
for(int i=0;i<x.length;i++)
{
for(int j=0;j<x[i].length;j++)
{
s+=x[i][j];
}
}
return s;
}
}
public class ShuZu2 {
public static void main(String[]args){
int [][] x=new int[7][7];
//生成随机数组,注意没有返回值,另外打印一行字
suiJi(x);
System.out.println("生成的数组是:");
//显示数组内容,注意没有返回值
showArray(x);
//取值
float result=getAvg(x);
System.out.println("平均数是"+result);
}
static float getAvg(int [][] x){
float s=0;
for(int i=0;i<x.length;i++){
for(int j=0;j<x[i].length;j++){
s+=x[i][j];
}
}
return s/(x.length*x[0].length);
}
static void suiJi (int[][]x){ //这里我出错了。返回值写了int型,不应该的。思考一下。
for(int i=0;i<x.length;i++){
for(int j=0;j<x[i].length;j++){
x[i][j]=(int)(Math.random()*10);
}
}
}
static void showArray(int[][]x){ //这里我出错了。返回值写了int型,不应该的。思考一下。
for(int i=0;i<x.length;i++){
for(int j=0;j<x[i].length;j++){
System.out.print(x[i][j]+"\t");// 给数据空格
}
System.out.println();//打印换行
}
}
}
import java.util.Arrays;
public class SuZu3{
public static void main(String[] args) {
int [] x={2,-1,7,777,6,764,85896,65554,0,874785,417825,74};
sort(x,'n');
for(int i=0;i<x.length;i++){
System.out.print(x[i]+"\t");
}
}
//给数组进行选择性排序
//调用API进行升序
static void sort(int[]x,char Flag){
if('A'==Flag){
Arrays.sort(x);
}
else {
for(int i=0;i<x.length-1;i++){
for(int j=0;j<x.length-1-i;j++){
int temp=0;
if(x[j]<x[j+1]){
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
}
}
}
import java.util.Scanner;
public class Suzu4 {
public static void main(String[] args) {
System.out.println("请输入");//这个命令只能紧贴在Scanner scan = new Scanner(System.in);的上面或下面才有效。
Scanner scan = new Scanner(System.in);
//System.out.println("请输入");或者放在Scanner scan = new Scanner(System.in);的下面
String str = scan.nextLine();// nextLine才是接收一行
char[] s = str.toCharArray();// 把字符串转换称一个字符数组
scan.close();
int letterCount = 0;
int numberCount = 0;
int spaceCount = 0;
int otherCount = 0;
for (int i = 0; i < s.length; i++) {
if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z') {
letterCount++;
} else if (s[i] >= '1' && s[i] <= '9') {
numberCount++;
} else if (s[i] == ' ') {
spaceCount++;
} else {
otherCount++;
}
}
System.out.println("字母有" + letterCount + "个");
System.out.println("数字有" + numberCount + "个");
System.out.println("空格有" + spaceCount + "个");
System.out.println("其他有" + otherCount + "个");
}
}//ctrl+shift+f 是代码格式化
//ctrl+shift+o 是导包
五、多重选择:switch语句
格式类似如下:
switch(choice)
{
case 1:
........
break;
case 2:
.......
break;
.........
//可以再来几个case(用break结束一下)
default:
.......
break;
}
注意:
case标签可以是:
* 类型为char、byte、short或int的常量表达式。
* 枚举常量
* 从Java SE 7开始,case标签还可以是字符串字面量。
本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-06/144878.htm
以上所述就是小编给大家介绍的《Java使用条件语句和循环结构确定控制流》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 006.Python循环语句while循环
- 007.Python循环语句while循环嵌套
- Python 循环语句
- Python While 循环语句
- Python for 循环语句
- 顺序、条件、循环语句的底层解释
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to Computation and Programming Using Python
John V. Guttag / The MIT Press / 2013-7 / USD 25.00
This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!