栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

常见Java基础题(for,while,dowhile,switch,continue,scanner)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

常见Java基础题(for,while,dowhile,switch,continue,scanner)

1,Continue
public class Continue {
    public static void main(String[] args) {
        for (int a = 1; a <= 100; a++) {
            if (a == 4) {
                continue;
            }
            if (a == 7) {
                continue;
            }
            if (a % 10 == 4) {
                continue;
            }
            if (a % 10 == 7) {
                continue;
            }
            if (a % 4 == 10) {
                continue;
            }
            if (a % 7 == 10) {
                continue;
            }
            System.out.println(a + "楼到了!");
        }
    }
}
2,DoWhile

打印0-100

public class DoWhile {
    public static void main(String[] args) {
        int sum = 0;
        do {             //执行语句
            System.out.println(sum);
            sum += 1;        //不加则是死循环  一直打印0
        }
        while (sum <= 100);//条件表达式
    }
}
3,For
public class For {
    public static void main(String[] args) {
        int sum = 0;
        for (int a = 0; a <= 100; a++) {
            if (a % 2 == 0) {
                sum += a;
            }
        }
        System.out.println("1-100偶数和为:" + sum);
        System.out.println("======================");
        for (int i = 0; i < 5; i++) {          //外层控制行
            for (int j = 0; j < 6; j++) {      //内层控制列
                System.out.print(i + "t");
            }
            System.out.println();
        }
        System.out.println("======================");
        for (int b = 1; b <= 9; b++) {
            for (int c = 1; c <= b; c++) {
                System.out.print(c + "x" + b + "=" + (c * b) + "t");
            }
            System.out.println();
        }
        System.out.println("======================");
        for (int d = 9; d >= 1; d--) {
            for (int e = 1; e <= d; e++) {
                System.out.print(e + "x" + d + "=" + (d * e) + "t");
            }
            System.out.println();
        }
        System.out.println("======================");
        for (int f = 1; f <= 5; f++) {
            for (int g = 1; g <= 5; g++) {
                System.out.print("*t");
            }
            System.out.println();
        }
        System.out.println("======================");

        for (int h = 1; h <= 5; h++) {
            for (int j = 1; j <= 5; j++) {
                if ((h + j) % 2 == 0) {
                    System.out.print("*t");
                } else if ((h + j) % 2 == 1) {
                    System.out.print("#t");
                }
            }
            System.out.println();
        }
        System.out.println("======================");

        int k = 0;
        for (int l = 100; l <= 150; l++) {
            if (l % 3 != 0) {
                k++;
                System.out.print(l + "、t");
            }
            if (k % 5 == 0) {
                System.out.println();
            }
        }
        System.out.print("n共打印:" + k + "次!!!");

        System.out.println("======================");
//打印100-150之间的素数质数(带标签的continue)
        outer:
        for (int m = 101; m < 150; m++) {
            for (int n = 2; n < m / 2; n++) {
                if (m % n == 0) {
                    continue outer;//从内部直接跳转到外部
                }
            }
            System.out.println(m);
        }
    }
}

打印结果:

1-100偶数和为:2550
======================
0	0	0	0	0	0	
1	1	1	1	1	1	
2	2	2	2	2	2	
3	3	3	3	3	3	
4	4	4	4	4	4	
======================
1x1=1	
1x2=2	2x2=4	
1x3=3	2x3=6	3x3=9	
1x4=4	2x4=8	3x4=12	4x4=16	
1x5=5	2x5=10	3x5=15	4x5=20	5x5=25	
1x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=36	
1x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49	
1x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64	
1x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81	
======================
1x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81	
1x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64	
1x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49	
1x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=36	
1x5=5	2x5=10	3x5=15	4x5=20	5x5=25	
1x4=4	2x4=8	3x4=12	4x4=16	
1x3=3	2x3=6	3x3=9	
1x2=2	2x2=4	
1x1=1	
======================
*	*	*	*	*	
*	*	*	*	*	
*	*	*	*	*	
*	*	*	*	*	
*	*	*	*	*	
======================
*	#	*	#	*	
#	*	#	*	#	
*	#	*	#	*	
#	*	#	*	#	
*	#	*	#	*	
======================
100、	101、	103、	104、	106、	
107、	109、	110、	112、	113、	

115、	116、	118、	119、	121、	
122、	124、	125、	127、	128、	

130、	131、	133、	134、	136、	
137、	139、	140、	142、	143、	

145、	146、	148、	149、	
共打印:34次!!!======================
101
103
107
109
113
127
131
137
139
149
4,Scanner
import java.util.Scanner;

public class KeyScanner {

    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        // next方式接收字符串
        System.out.println("Next方式接收:");
        // 判断用户还有没有输入字符
        if (scanner.hasNext()) {
            String str = scanner.next();
            System.out.println("输入内容:" + str);
        }
        System.out.println("nextLine方式接收:");
        if (scanner.hasNextLine()) {
            String str2 = scanner.nextLine();
            System.out.println("输入内容:" + str2);
        }
        // 凡是属于IO流的类如果不关闭会一直占用资源.要养成好习惯用完就关掉.就好像你接水完了要关 水龙头一样.很多下载软件或者视频软件如果你不彻底关,都会自己上传下载从而占用资源,你就会觉得 卡,这一个道理.
        scanner.close();
    }
}
5,Switch
public class Switch {
    public static void main(String []args){
    System.out.println("请输入一个数:");
    Scanner sc=new Scanner(System.in);
    int num=sc.nextInt();
    switch (num){
        case 1://"1"为输入语句
            System.out .println("一");
            break;
        case 2:
            System.out .println("二");
            break;
        case 3:
            System.out .println("三");
            break;
        case 4:
            System.out .println("四");
            break;
        case 5:
            System.out .println("五");
            break;
            case 6:
            System.out .println("六");
            break;
        default:
            System.out.println("数字不合法!");
    }
    }
}
6,While
public class While {
    public static void main(String[] args) {
        //打印0-100的总和以及奇数和and偶数和
        int sum = 0;
        int i = 1;
        int jishu = 0;
        int oushu = 0;
        while (i <= 100) {
            sum += i;
            if (i % 2 == 1) {
                jishu += i;
            } else {
                oushu += i;
            }
            i++;
        }
        System.out.println("1-100的总和:" + sum);
        System.out.println("1-100的奇数和:" + jishu);
        System.out.println("1-100的偶数和:" + oushu);
        System.out.println("======================");

        //打印一百以内的数,每行5个数
        int a = 0;
        while (a < 100) {
            a++;
            System.out.print(a + "t");
            if (a % 5 == 0) {
                System.out.println();
            }
        }
            }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/362267.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号