用户交互Scanner
Scanner s = new Scanner(Syatem.in);
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if (scanner.hasNext()){
//使用next方式接收
String str = scanner.next();
System.out.println("输入的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close() ;
}
}
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
//判断用户有没有输入字符串
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输入的内容为:"+str);
}
scanner.close() ;
}
}
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据:");
String str = scanner.nextLine();
System.out.println("输入的内容为:"+str);
scanner.close() ;
}
}
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//从键盘接收数据
int i= 0;
float f = 0.0f;
System.out.println("请输入整数:");
//如果..那么..
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据:"+i);
}else{
System.out.println("输入的不是整数数据!");
}
System.out.println("请输入小数:");
//如果..那么..
if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据:"+f);
}else{
System.out.println("输入的不是小数数据!");
}
scanner.close() ;
}
}
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计算输入了多少个数字
int m = 0;
System.out.println("请输入数据:");
//通过循环判断是否还有输入,并在里面对每一次进行求和与统计
while (scanner.hasNextDouble()){
double v = scanner.nextDouble();
//
m = m + 1;//m++
sum = sum + v;
}
System.out.println(m+"个数的和为"+sum);
System.out.println(m+"个数的平均值是"+(sum/m));
scanner.close() ;
}
}
顺序结构
package com.kuang.struct;
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
if选择结构
package com.kuang.struct;
import java.util.Scanner;
//if单选泽结构
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equasl:判断字符串是否相等
if (s.equals("Hello") ){
System.out.println(s);
}
System.out.println("End");
scanner.close() ;
}
}
import java.util.Scanner;
//if双选择结构
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于60分及格,小于60分不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score>=60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close() ;
}
}
package com.kuang.struct;
import java.util.Scanner;
//if多选择结构
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score==100){
System.out.println("完美");
}else if (score<100 && score>=90){
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){
System.out.println("不及格");
}else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
switch选择结构
package com.kuang.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透 //switch 匹配一个具体的值
char grade = "F";
switch (grade){
case "A":
System.out.println("优秀");
break;
case "B" :
System.out.println("良好");
break;
case "C" :
System.out.println("及格");
break;
case "D" :
System.out.println("再接再厉");
break;
case "E" :
System.out.println("回家去吧");
break;
default :
System.out.println("未知等级");
}
}
}
package com.kuang.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "北极";
//JDK7新特性,表达式结果可以是字符串
//字符的本质是数字
//反编译 java--class(字节码文件)---反编译(IDEA)
switch(name){
case"南极":
System.out.println("南极");
break;
case"北极":
System.out.println("北极");
break;
default:
System.out.println("?");
}
}
}
while循环
package com.kuang.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
package com.kuang.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while (true){
//等待客户端连接
//定时检查
//。。。。。。。
}
}
}
package com.kuang.struct;public class WhileDemo03 { public static void main(String[] args) { //计算1+2+3+·····+100 int i = 0; int sum = 0; while(i<=100){ sum = sum + i; i++; } System.out.println(sum); }}
do···while循环
- do···while是先执行后判断,while先判断后执行
package com.kuang.struct;public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum + i; i++; }while(i<=100); System.out.println(sum); }}
package com.kuang.struct;public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum + i; i++; }while(i<=100); System.out.println(sum); }}
for循环
- for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构
package com.kuang.struct;public class ForDemo01 { public static void main(String[] args) { int a = 1;//初始化条件 while(a<=100){//条件判断 System.out.println(a);//循环体 a+=2;//迭代 } System.out.println("while循环结束!"); //初始化//条件判断//迭代 for (int i=1;i<=100;i++){ System.out.println(i); } System.out.println("for循环结束!"); //死循环 for (;;){ } }}
package com.kuang.struct;public class ForDemo02 { public static void main(String[] args) { //练习1:计算0到100之间的奇数和偶数的和 int oddSum = 0; int evenSum = 0; for (int i = 0; i <= 100; i++) { if (i%2!=0){ oddSum+=i; }else{ evenSum+=i; } } System.out.println("奇数的和:"+oddSum); System.out.println("偶数的和: "+evenSum); }}
package com.kuang.struct;public class ForDemo03 { public static void main(String[] args) { //练习2:用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个 for (int i = 0; i < 1000; i++) { if (i%5==0){ System.out.print(i+" "); } if (i%(5*3)==0){ System.out.println(); //System.out.print("
"); } } //println 输出完会换行 //print 输出完不会换行 }}
package com.kuang.struct;public class ForDemo04 { public static void main(String[] args) { /*练习:打印99乘法表 1.先打印第一列 2.把固定的1再用一个循环包起来 3.去掉重复项,i<=j */ for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(j+"*"+i+"="+(j+i) +" "); } System.out.println(); } }}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 »
Java流程控制