1.用户交互Scanner
Scanner对象
Scanner scanner = new Scanner(System.in);
System.out.println("使用next()函数接收:");
//使用Scanner类的next()和nextLine()方法获取输入字符串
//使用hasNext()和hasNextLine()判断是否有输入.
//使用next()接收单个字符串,以空格为结束符。
//nextLine()接收整行字符串,以回车为结束符。可以接受空白。
if (scanner.hasNext()){
String str = scanner.next();
System.out.println("用户输入为:"+ str);
}
//凡是属于IO流的类,如果不关闭会一直占用资源。
scanner.close();
2.JAVA结构
-
顺序结构:从上至下顺序进行。基本算法结构。
-
选择结构:
-
if单选择结构
-
if双选择结构
-
if多选择结构
-
switch多选择结构
char grade = 'A'; switch(grade){ case 'A': System.out.println("优秀"); break; case 'B': System.out.println("良好"); break; case 'C': System.out.println("一般"); break; default: System.out.println("默认"); }
-
-
循环结构:
-
while循环
while(布尔表达式){ //循环内容 } //避免死循环
-
do..while循环
do{ //循环内容 }while(布尔表达式); //保证循环体至少执行一次
-
for循环
for(初始化;条件;迭代){ }
-
增强For循环
int[] numbers = {10,20,30,40,50}; //遍历数组 for(int x :numbers){ System.out.println(x); }
-