java流程控制
Scanner对象
实现程序和人的交互,通过Scanner类来获取用户的输入。
Scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine() 方法获取输入的字符串,在读取前我们一般需要使用 hasNext() 和hasNextLine()判断是否还有输入的数据
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
//从键盘接收数据
Scanner s = new Scanner(System.in);
System.out.println("使用next的方式接收");
//判断用户有没有输入字符串
if(s.hasNext()){
String str = s.next();//程序会等待用户输入完毕
System.out.println("输出的内容是"+str);
}
//凡是IO流的类如果不关闭,会一直占用资源,要养成好习惯用完就关掉
s.close();
}
}
/*
使用next的方式接收
hello world
输出的内容是hello
*/
Scanner s2 = new Scanner(System.in);
System.out.println("使用nextLine的方式接收");
if(s2.hasNextLine()){
String str3 = s2.nextLine();
System.out.println("输出的内容是"+str3);
}
s2.close();
/*使用nextLine的方式接收
hello coding world
输出的内容是hello coding world
*/
Scanner s3 = new Scanner(System.in);
System.out.println("使用nextLine的方式接收");
String str4 = s3.nextLine();
System.out.println("输出的内容是"+str4);
s3.close();
next()
- 一定要读到有效字符后才可以结束输入。
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
- next()不能得到带有空格的字符串
nextLine()
- 以Enter为结束符,也就是nextLine()方法返回的是输入回车之前的所有字符
- 可以获得空白
//写的时候先写开头和结尾
Scanner s = new Scanner(System.in);
s.close();
Scanner scan = new Scanner(System.in);
int i = 0;
float f = 0.0f;
//从键盘接收数据
System.out.println("请输入整数:");
if(scan.hasNextInt()){
i = scan.nextInt();
System.out.println("整数数据 : "+ i);
}else{
System.out.println("输入的不是整数数据");
}
scan.close();
//求多个数的平均值
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m = 0;
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
sum = sum + x;
m = m +1;
}
System.out.println(m+"个数的和为"+sum);
System.out.println(m+"个数的平均数为"+(sum/m));
scanner.close();
顺序结构
Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
顺序结构是最简单的算法结构。
语句和语句之间,框和框之间是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任意一个算法都离不开的一种基本算法就结构。
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
选择结构
if单选,双选,多选。if嵌套。Switch多选。
if选择
if(条件布尔){ //单
//为true则执行
}
if(条件布尔){ //双
//为true则执行的语句
}else{
//为false执行的语句
}
if(布尔表达式1){ //多
}else if(布尔表达式2){
}else if(布尔表达式3){
}else{
}
if(){
if(){
//嵌套
}
}
import java.util.Scanner;
public class Dome2 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
//判断字符串是否相等
if(s.equals("hello")){
System.out.println("输入正确");
}
scan.close();
}
}
Scanner scan = new Scanner(System.in);
int s = scan.nextInt();
if(s>=60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scan.close();
Scanner scan = new Scanner(System.in);
int score = scan.nextInt();
if(score == 100){
System.out.println("满分耶");
}else if(score>=90&& score<100){
System.out.println("优秀");
} else if (score<90 && score>=80) {
System.out.println("尚可");
}else if(score <80 && score >=60){
System.out.println("及格");
}else if(score<60 && score>=0){
System.out.println("不及格");
}else{
System.out.println("成绩不合法");
}
scan.close();
switch case语句
switch(expression){
case value:
//语句
break;//可选
case value:
//语句
break;//可选
default://可选
//语句
}
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch语句中的变量类型可以是:
- byte、short、int或者char。
- 从Java SE 7开始,switch支持字符串String类型
- 同时case标签必须为字符串常量或字面量
不加break,会有case穿透
char grade = 'C';
switch(grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("尚可");
break;
default:
System.out.println("需加努力");
}
循环结构
whlie 循环,do...while,for循环,增强for循环
whlie 循环
while(布尔表达式){
//循环内容
}
int i = 1;
int sum = 0;
while (i <= 10){
sum = sum + i;
i++;
}
System.out.println(sum);
while(true){
//等待客户端链接
//定时检查
//......
}
- 只要布尔表达式为true,循环就会一直执行下去。
- 大多数情况下,我们会让循环停止,需要一个让表达式失效的方式来结束循环。
- 少部分情况需要循环一直执行,比如服务器的请求响应监听等。
- 循环条件一直为true就会造成无限循环【死循环】,正常业务编程中应该尽量避免死循环,会影响程序性能或造成程序卡死崩溃。
do...while循环
do{
//代码语句
}while(布尔表达式);
int i = 1;
int sum = 0;
do{
sum = sum + i;
i++;
}while (i <= 10);
System.out.println(sum);
-
对于while语句而言,如果不满足条件,则不能进入循环,但有时候我们需要即使不满足条件,也至少执行一次。
-
do...while 循环和while循环相似,不同的是,do...while循环至少会执行一次。
while和do-while的区别:
-
while先判断后执行。do...while是先执行后判断。
-
do...while总是保证循环体会被至少执行一次。这是他们的主要差别。
for循环
- for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
- for循环执行的次数是在执行前就确定的。
for(初始化一个或多个;布尔表达式;更新){
//代码语句
}
//初始化可以为空
int oddsum = 0;
for(int o =1;o<=99;o+=2){
oddsum = oddsum + o;
}
System.out.println(oddsum);
//快捷键 数字.for 再按回车
增强for循环
for(声明语句:表达式){
//代码句子
}
int[] numbers = {10,20,30,40,50};//定义了一个数组
for (int x:numbers){
System.out.println(x);
}
int[] numbers = {10,20,30,40,50};
for(i=0;i<5;i++){
System.out.println(numbers[i]);
}
/*10
20
30
40
50*/
- Java5引入了一种主要用于数组或集合的增强型for循环。
- 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
- 表达式:表达式是要访问的数组名,或是返回值为数组的方法。
break,continue,goto
-
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
-
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
-
关于goto关键字
goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用;Java没有goto。然而,在break和continue这两个关键字的身上,我们仍然能看出一些goto的影子—带标签的break和continue。
“标签”是指后面跟一个冒号的标识符,例如 : label:
对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
int i = 0;
for(;i<20;){
++i;
if(i % 5 == 0){
System.out.println();
continue;
}
System.out.print(i+" ");
}
//求质数,就是那个在大于一的自然数中,除了1和它本身之外不再有其他因数的自然数。
outer:for(int i = 101;i<=150;i++){
for(int j=2;j<=Math.log(i);j++){
if(i%j==0){
continue outer;
}
}
System.out.print(i+" ");
}
//打印三角形,五行
//先打一半
for(int i = 1;i<=5;i++){
for(int j = 5-i;j>=0;j--){
System.out.print(" ");
}
for(int w = 1;w<=i;w++){
System.out.print("*");
}
System.out.println();
}
*
**
***
****
*****
//打全部的
for(int i = 1;i<=5;i++){
for(int j = 5-i;j>=0;j--){
System.out.print(" ");
}
for(int w = 1;w<=2*i-1;w++){
System.out.print("*");
}
System.out.println();
}
//想打几行打几行
Scanner scan = new Scanner(System.in);
int u = scan.nextInt();
for (int i = 1; i <= u; i++) {
for (int j = u - i; j >= 0; j--) {
System.out.print(" ");
}
for (int w = 1; w <= 2 * i - 1; w++) {
System.out.print("*");
}
System.out.println();
}
debug:
给需要的位置的行所在地,用鼠标点击,加上断点。
运行的绿色三角旁边有一只绿色的虫子,点击程序运行至断点处
在出现的debugger区点箭头,箭头显示为step in 程序会按顺序一点一点走
标签:语句,控制,Java,Scanner,流程,System,循环,println,out From: https://www.cnblogs.com/tiangong/p/16725805.html