Study notes: java keyboard input and output, if statement, loop structure

Two methods of keyboard input and output

package Str;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Scanner;

public class Sys_1 {
    public static void  main(String[] args){
        Scanner rea=new Scanner(System.in);
        System.out.println(rea.next());
    }
}
import java.io.*;
public class App3_4
{
  public static void main(String[] args) throws IOException
  {
    float num;
    String str;
    BufferedReader buf;
    buf=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("请输入一个实数:");
    str=buf.readLine();         //将输入的文字指定给字符串变量str存放
    num= Float.parseFloat(str);   //将str转换成float类型后赋给num
    System.out.println("您输入的数为:"+num);
  }
}

Program flow control

 

Sequence structure
The program is executed line by line from top to bottom, without any judgment or jump in between.
 
Branch structure
According to conditions, selectively execute a certain piece of code.
There are two branch statements, if...else and switch .
 
Cyclic structure
According to the loop condition, a certain piece of code is executed repeatedly.
There are three loop statements: while , do...while and for .
Note: After JDK1.5 , a foreach loop is provided to conveniently traverse collection and array elements.
//顺序结构
//Java中定义成员变量时采用合法的前向引用。如:
public class Test{
        int num1 = 12;
        int num2 = num1 + 2;
}
错误形式:
public class Test{
       int num2 = num1 + 2;
       int num1 = 12;
}
//if
//if语句三种格式:

if(true){
	执行代码块;
     }

if(条件表达式){
	执行代码块;
      }
     else{
	执行代码块;
      }
if(条件表达式){
	执行代码块;
      }
      else if (条件表达式){
	执行代码块;
      }
       ……
       else{
	执行代码块;
       }

public class class_name {
    public static void  main(String[] args){
//if-else
                int age = 75;
                if (age<0) {
                    System.out.println("不可能!");
                } else if (age>250) {
                    System.out.println("是个妖怪!");
                } else {
                    System.out.println("人家芳龄 "+ age +" ,马马乎乎啦!");
                }
            }
}

switch statement

	switch(变量){
	case 常量1:
		语句1;
		break;
	case 常量2:
		语句2;
		break;
	… …
	case 常量N:
		语句N;
		break;
	default:
		语句;
		break;
	 } 

for loop

//应用举例
	public class ForLoop {
		public static void main(String args[]){
		          int result = 0;
		          for(int i=1; i<=100; i++) {
			  result += i;
		          }
  	          System.out.println("result=" + result);
		}
	} 

while loop statement


//应用举例
		public class WhileLoop {
		        public static void main(String args[]){
        		int result = 0;
			int i=1;
			while(i<=100) {
			        result += i;
            	       	        i++;
			}
			        System.out.println("result=" + result);
		         }
		} 

do-while loop statement


//应用举例
		public class WhileLoop {
		        public static void main(String args[]){
        		  int result = 0,  i=1;
			        do{
			        	   result += i;
           		       	   i++;
				 }while(i<=100);
			 System.out.println("result=" + result);
		       }
		}  

Special flow control statement

    break 语句
break语句用于终止某个语句块的执行
 		{    ……	 
		     break;
		     ……
		}
break终止当前所在的循环


   break 语句用法举例
	 public class TestBreak{
		public static void main(String args[]){
	    for(int i = 0; i<10; i++){ 
	     	if(i==3)
		      break;	  
	    	System.out.println(" i =" + i);
	    }
	    System.out.println("Game Over!");
		}
}

continue 语句
continue语句用于跳过某个循环语句块的一次执行 
continue语句出现在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环 

continue语句用法举例
	public class ContinueTest {
		        public static void main(String args[]){
	     	   for (int i = 0; i < 100; i++) {
	      	         	  if (i%10==0)
			        		continue;
		         	                System.out.println(i);
		         	               }  }  } 

return:并非专门用于结束循环的,它的功能是结束一个方法。当一个方法执行到一个return语句时,这个方法将被结束。

与break和continue不同的是,return直接结束整个方法,不管这个return处于多少层循环之内
Break can only be used in switch statements and loop statements.
continue can only be used in loop statements.
The functions of the two are similar, but continue is to terminate the loop, and break is to terminate the loop.
There can be no other statements after break and continue, because the program will never execute the following statements.

Guess you like

Origin blog.csdn.net/qq_44909275/article/details/104745812