【Java基础】· Java基本语法:程序流程控制习题总结

写在前面


        Hello大家好, 我是【麟-小白】,一位软件工程专业的学生,喜好计算机知识。希望大家能够一起学习进步呀!本人是一名在读大学生,专业水平有限,如发现错误不足之处,请多多指正!谢谢大家!!!

        如果小哥哥小姐姐们对我的文章感兴趣,请不要吝啬你们的小手,多多点赞加关注呀!❤❤❤ 爱你们!!!


【流程控制知识详解】

【Java基础】· 异常处理习题详解【Java基础】· Java基本语法(下):程序流程控制


目录

写在前面

分支结构:

1. 写出结果:

2. 实现对三个整数进行排序,输出时按照从小到大的顺序输出。

3.编写程序,从键盘接收整数参数。如果该数为1-7,打印对应的星期值,否则打印“非法参数”。

4.从键盘分别输入年、月、日,判断这一天是当年的第几天

5.switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?

6.编写程序,判断给定的某个年份是否是闰年

7.要求用户输入两个数a和b,如果a能被b整除或者a加b大于1000,则输出a;否则输出b。

8.编写程序:从键盘上读入一个学生成绩,存放在变量score中,根据score的值输出其对应的成绩等级:

9.根据指定月份,打印该月份所属的季节。

10.彩票游戏

11.赌数游戏

12.生肖问题

13.下面程序片段的输出结果是?

循环结构

1.下面程序片段的输出结果是?

2.打印1-100之间13的倍数,使用for循环

3.使用双重循环打印20 * 8的矩形,使用for循环实现

4.用for循环计算1000以内偶数的和

5.执行如下代码后,c的值是多少?

6.以下代码的运行结果?

7.修正如下代码

8.打印九九乘法表

9.3000米长的绳子,每天减一半。问多少天这个绳子会小于5米?不考虑小数。

10.用循环控制语句打印输出:1+3+5+...+99=?的结果

混合结构练习

1.写出结果

2.猜数字游戏

3.打印1-100之间数,其中3、5、7的倍数不打印(continue)

4.一个数如果恰好等于它的因子之和,这个数就称为"完数"。(因子:除去这个数本身的约数)

5.输入两个正整数m和n,求其最大公约数和最小公倍数

6.输出所有的水仙花数

结语


分支结构:

1. 写出结果:

class Demo{
    public static void main(String[] args){
        int m=0,n=3;
        if(m>0)
            if(n>2)
                System.out.println("A");	
            else
                System.out.println("B");        			
    }
}

答:没有结果。


2. 实现对三个整数进行排序,输出时按照从小到大的顺序输出。

public class sort {
	public static void main(String[] args) {
		int a = 95, b = 79, c = 66;
		if (a > b) {
			int temp = b;
			b = a;
			a = temp;
		}
		if (a > c) {
			int temp = c;
			c = a;
			a = temp;
		}
		if (b > c) {
			int temp = c;
			c = b;
			b = temp;
		}
		System.out.println("a = " + a + " b = " + b + " c = " + c);
	}
}


3.编写程序,从键盘接收整数参数。如果该数为1-7,打印对应的星期值,否则打印“非法参数”。

import java.util.Scanner;
class TestSwitch {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入星期值:");
		int week = input.nextInt();
		switch (week) {
		case 1:
			System.out.println("星期一:Monday");
			break;
		case 2:
			System.out.println("星期二:Tuesday");
			break;
		case 3:
			System.out.println("星期三:Wednesday");
			break;
		case 4:
			System.out.println("星期四:Thursday");
			break;
		case 5:
			System.out.println("星期五:Friday");
			break;
		case 6:
			System.out.println("星期六:Saturday");
			break;
		case 7:
			System.out.println("星期天:Sunday");
			break;
		default:
			System.out.println("非法星期值");
			break;
		}
	}
}


4.从键盘分别输入年、月、日,判断这一天是当年的第几天

import java.util.Scanner;

class TestDaysOfYear {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		System.out.print("年:");
		int year = input.nextInt();

		System.out.print("月:");
		int month = input.nextInt();

		System.out.print("日:");
		int day = input.nextInt();

		int days = day;

		// 加前面几个月的满月天数
		switch (month) {
		case 12:
			// 前面11个月的总天数
			// days += 第11月的天数;
			days += 30;
		case 11:
			// 前面10个月的总天数
			// days += 第10月的天数;
			days += 31;
		case 10:
			days += 30;// 九月
		case 9:
			days += 31;// 八月
		case 8:
			days += 31;// 七月
		case 7:
			days += 30;// 六月
		case 6:
			days += 31;// 五月
		case 5:
			days += 30;// 四月
		case 4:
			days += 31;// 三月
		case 3:
			days += 28;// 二月
			/*
			 * if(闰年){ days++; }
			 */
			if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
				days++;
			}
		case 2:
			days += 31;// 一月
		}

		System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + days + "天");

		/*
		int days = 0;
		switch(month){
			case 1:
				days = day;
				break;
			case 2:
				days = 31 + day;
				break;
			case 3:
				//days = 31 + 二月的天数 + day;
				days = 31 + 28 + day;
				break;
			case 4:
				//days = 31 + 二月的天数 + 31 + day;
				days = 31 + 28 + 31 + day;
				break;
			....
		}
		
		if(闰年 && month >2){
			days++;
		}
		*/
		
	}
}


5.switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?

答案一:switch可以作用在byte上,不能作用在long上,JDK1.7之后可以作用在String上。

答案二:switch支持的类型byte,short,int,char,JDK1.5之后支持枚举,JDK1.7之后支持String类型。


6.编写程序,判断给定的某个年份是否是闰年

闰年的判断规则如下:

      (1)若某个年份能被4整除但不能被100整除,则是闰年。

      (2)若某个年份能被400整除,则也是闰年。

提示:

if((year % 4 ==0 && year % 100 != 0) || year % 400 == 0){}

import java.util.Scanner;

public class Year {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = scan.nextInt();
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
			System.out.println(year + "年是闰年");
		} else {
			System.out.println(year + "年不是闰年");
		}
	}
}


7.要求用户输入两个数a和b,如果a能被b整除或者a加b大于1000,则输出a;否则输出b。

import java.util.Scanner;

public class Test1 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入两个数:");
		int a = input.nextInt();
		int b = input.nextInt();
		if (a % b == 0 || a + b > 1000)
			System.out.println(a);
		else
			System.out.println(b);
	}
}


8.编写程序:从键盘上读入一个学生成绩,存放在变量score中,根据score的值输出其对应的成绩等级:

score>=90           等级:A

70=<score<90        等级:B

60=<score<70        等级:C

score<60            等级:D

import java.util.Scanner;

class Exam1{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("请输入学生的成绩:");
		int score = input.nextInt();

		if(score>=90){
			System.out.println("等级:A");
		}else if(score>=70){
			System.out.println("等级:B");
		}else if(score>=60){
			System.out.println("等级:C");
		}else{
			System.out.println("等级:D");
		}
		
		/*
		if(score>=90){
			System.out.println("等级:A");
		}else if(score<90 && score>=70){
			System.out.println("等级:B");
		}else if(score<70 && score>=60){
			System.out.println("等级:C");
		}else{
			System.out.println("等级:D");
		}
		*/
	}
}


9.根据指定月份,打印该月份所属的季节。

3,4,5 春季 6,7,8 夏季  9,10,11 秋季 12, 1, 2 冬季

import java.util.Scanner;

class TestSeason {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入当前月份:");
		int month = input.nextInt();

		switch (month) {
		case 3:
		case 4:
		case 5:
			System.out.println("春季");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("夏季");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("秋季");
			break;
		case 12:
		case 1:
		case 2:
			System.out.println("冬季");
			break;
		default:
			System.out.println("非法月份");
		}
	}
}


10.彩票游戏

假设你想开发一个玩彩票的游戏,程序随机地产生一个两位数的彩票,提示用户输入一个两位数,然后按照下面的规则判定用户是否能赢。

1)如果用户输入的数匹配彩票的实际顺序,奖金10 000美元。

2)如果用户输入的所有数字匹配彩票的所有数字,但顺序不一致,奖金 3 000美元。

3)如果用户输入的一个数字仅满足顺序情况下匹配彩票的一个数字,奖金1 000美元。

4)如果用户输入的一个数字仅满足非顺序情况下匹配彩票的一个数字,奖金500美元。

5)如果用户输入的数字没有匹配任何一个数字,则彩票作废。

提示:使用Math.random() 产生随机数

Math.random() 产生[0,1)范围的随机值

Math.random() * 90:[0,90)

Math.random() * 90 + 10:[10,100) 即得到  [10,99]

使用(int)(Math.random() * 90  + 10)产生一个两位数的随机数。

import java.util.Scanner;

class TestCaiPiao {
	public static void main(String[] args) {
		// 1、随机产生一个两位数
		// System.out.println(Math.random());//产生[0,1)
		int number = (int) (Math.random() * 90 + 10);// 得到[10,99],即[10,100)
		// System.out.println(number);

		int numberShi = number / 10;
		int numberGe = number % 10;

		// 2、用户输入一个两位数
		Scanner input = new Scanner(System.in);
		System.out.print("请输入一个两位数:");
		int guess = input.nextInt();

		int guessShi = guess / 10;
		int guessGe = guess % 10;

		if (number == guess) {
			System.out.println("奖金10 000美元");
		} else if (numberShi == guessGe && numberGe == guessShi) {
			System.out.println("奖金3 000美元");
		} else if (numberShi == guessShi || numberGe == guessGe) {
			System.out.println("奖金1 000美元");
		} else if (numberShi == guessGe || numberGe == guessShi) {
			System.out.println("奖金500美元");
		} else {
			System.out.println("没中奖");
		}

		System.out.println("中奖号码是:" + number);
	}
}


11.赌数游戏

提供三个1-6的随机数,作为掷骰子得到的点数。

如果各个点数相同,则为豹子。

如果三个骰子点数和,小于或等于9,则为“小”。

如果三个骰子点数和,大于9,则为“大”。

用户随机选择:押大、押小或者押豹子。通过判断,输出客户是否押正确。

import java.util.Scanner;

class GuessNumber {
	public static void main(String[] args) {
		// 1、产生一个[1-6]的数
		int a = (int) (Math.random() * 6 + 1);
		// System.out.println(a);
		int b = (int) (Math.random() * 6 + 1);
		// System.out.println(b);
		int c = (int) (Math.random() * 6 + 1);
		// System.out.println(c);

		/*
		 * a = 1; b = 1; c = 1;
		 */

		String result = "";
		if (a == b && b == c) {
			result = "豹子";
		} else if (a + b + c <= 9) {
			result = "小";
		} else {
			result = "大";
		}

		// 2、用户输入猜的结果
		Scanner input = new Scanner(System.in);
		System.out.print("请押宝,买定离手:(选择:大、小、豹子)");
		String guess = input.next();

		// 3、判断结果
		if (result.equals(guess)) {
			System.out.println("猜对了");
		} else {
			System.out.println("猜错了");
		}
	}
}


12.生肖问题

编写一个程序,为一个给定的年份找出其对应的中国生肖。

中国的生肖基于12年一个周期,每年用一个动物代表:

rat(鼠)、ox(牛)、tiger(虎)、rabbit(兔)、dragon(龙)、snake(蛇)、horse(马)、sheep(羊)、monkey(候)、rooster(鸡)、dog(狗)、pig(猪)。

提示:2019年:猪   2019 % 12 == 3

import java.util.Scanner;

class ShengXiaoTest {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入年份:");
		int year = input.nextInt();

		switch (year % 12) {
		case 0:
			System.out.println("猴年");
			break;
		case 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;
		case 7:
			System.out.println("兔年");
			break;
		case 8:
			System.out.println("龙年");
			break;
		case 9:
			System.out.println("蛇年");
			break;
		case 10:
			System.out.println("马年");
			break;
		case 11:
			System.out.println("羊年");
			break;

		}
	}
}


13.下面程序片段的输出结果是?

public static void main(String[] args) {
	int a = 3;
	int b = 1;
	if(a = b){
		System.out.println("Equal");
	}else{
		System.out.println("Not Equal");
	}
}

答案:编译不通过(注意是赋值,不是判断相等)


循环结构

1.下面程序片段的输出结果是?

public class Test {
	public void method() {
		for (int i = 0; i < 3; i++) {
			System.out.print(i);
		}
		System.out.print(i);
	}
}

compile error  (i的作用域在for循环之内)


2.打印1-100之间13的倍数,使用for循环

public class Test1 {
	public static void main(String[] args) {
		for (int i = 1; i <= 100; i++) {
			if (i % 13 == 0)
				System.out.println(i);
		}
	}
}


3.使用双重循环打印20 * 8的矩形,使用for循环实现

public class Test1 {
	public static void main(String[] args) {
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 20; j++) {
				System.out.print(".");
			}
			System.out.println();
		}
	}
}


4.用for循环计算1000以内偶数的和

public class Test1 {
	public static void main(String[] args) {
		int sum = 0;
		for (int i = 1; i <= 1000; i++) {
			if (i % 2 == 0)
				sum += i;
		}
		System.out.println(sum);
	}
}


5.执行如下代码后,c的值是多少?

public class Test1 {
	public static void main(String[] args) {
		int a = 0;
		int c = 0;
		do {
			--c;
			a = a - 1;
		} while (a >= 0);
		System.out.println("c = " + c);
	}
}

答案:c = -1


6.以下代码的运行结果?

public class Test1 {
	public static void main(String[] args) {
		int i = 10;
		while (i > 0) {
			i = i + 1;
			if (i == 10) {
				break;
			}
		}
		System.out.println("i=" + i);
	}
}

答案一:是一个负数,因为i一直累加会超过int的存储范围

答案二:死循环


7.修正如下代码

下面是一段程序,目的是输出10个=,但是不小心代码写错了,现在需要修改代码,使得程序完成功能,但是只能“增加”或“修改”其中“一个”字符,很明显,将i--改为i++,可以完成功能,但是需要修改“两个”字符,所以并不是一个正确的答案?

public class Test1 {
	public static void main(String[] args) {
		int n = 10;
		for (int i = 0; i < n; i--) {
			System.out.println("=");
		}
	}
}

答:i<n修改为-i<n


8.打印九九乘法表

public class Test1 {
	public static void main(String[] args) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
	}
}


9.3000米长的绳子,每天减一半。问多少天这个绳子会小于5米?不考虑小数。

public static void main(String[] args) {
		int day = 0;
		for (int x = 3000; x >= 5; x /= 2) {
			day++;
		}
		System.out.println("day=" + day);
		/*
		 * 方法二: 
		 * day = 0; 
		 * for(int x=3000; x>=5; day++) { 
		 * 		x = x/2; 
		 * }
		 * System.out.println(day);
		 */
}


10.用循环控制语句打印输出:1+3+5+...+99=?的结果

public class Test1 {
	public static void main(String[] args) {
		int sum = 0;
		for (int i = 1; i <= 99; i += 2) {
			sum += i;
		}
		System.out.println("sum = " + sum);
	}
}
public class Test1 {
	public static void main(String[] args) {
		int sum = 0;
		for (int i = 1; i < 100; i++) {
			if (i % 2 != 0) {
				sum += i;
			}
		}
		System.out.println("sum = " + sum);
	}
}


混合结构练习

1.写出结果

public class Test1 {
	public static void main(String[] args) {
		int i = 0, j = 5;
		tp: for (;;) {
			i++;
			for (;;) {
				if (i > j--)
					break tp;
			}
		}
		System.out.println("i = " + i + ", j = " + j); // i=1,j=-1;
	}
}


2.猜数字游戏

随机生成一个100以内的数,猜数字游戏:

从键盘输入数,如果大了提示,大了,如果小了,提示小了,如果对了,就不再猜了,并统计一共猜了多少次?

提示:随机数

import java.util.Random;

Random rand = new Random();

int num= rand.nextInt(100);

import java.util.Random;
import java.util.Scanner;

public class Test1 {
	public static void main(String[] args) {
		// 1、随机产生一个100以内的整数
		Random rand = new Random();
		// int num = rand.nextInt();//产生的是任意大小的整数
		int num = rand.nextInt(100);// 产生[0,100)的整数
		System.out.println(num);

		// 2、键盘输入
		Scanner input = new Scanner(System.in);

		// 声明变量
		int guess;
		int count = 0;
		do {
			// 循环体至少执行一次
			System.out.print("请输入一个整数:");
			guess = input.nextInt();// 为变量赋值

			count++;// 输入一次,计数一次

			if (guess > num) {
				System.out.println("大了");
			} else if (guess < num) {
				System.out.println("小了");
			} else {
				System.out.println("猜对了");
			}
		} while (guess != num);

		System.out.println("一共猜了:" + count + "次");
	}
}
import java.util.Random;
import java.util.Scanner;

public class Test1 {
	public static void main(String[] args) {
		Random rand = new Random();
		int num = rand.nextInt(100);
		Scanner input = new Scanner(System.in);
		int count = 0;
		do {
			count++;
			System.out.println("请猜:");
			int temp = input.nextInt();
			if (temp < num) {
				System.out.println("小了");
				continue;
			}
			if (temp > num) {
				System.out.println("大了");
				continue;
			}
			if (temp == num) {
				break;
			}
		} while (true);
		System.out.println("总共猜了" + count + "次");
	}
}


3.打印1-100之间数,其中3、5、7的倍数不打印(continue)

public class Test1 {
	public static void main(String[] args) {
		// 打印1-100之间数,其中3、5、7的倍数不打印
		for (int i = 1; i <= 100; i++) {
			if (i % 3 == 0 | i % 5 == 0 | i % 7 == 0) {
				// 跳过下面的打印语句,提前进入下一次循环,即i++语句
				// break;//结束循环
				continue;
			}
			// 下面这部分循环体语句,有些情况下需要跳过
			System.out.println(i);
		}
		System.out.println("over");
	}
}


4.一个数如果恰好等于它的因子之和,这个数就称为"完数"。(因子:除去这个数本身的约数)

例如6=1+2+3.编程 找出1000以内的所有完数

public class Test1 {
	public static void main(String[] args) {
		System.out.println("1-1000之间的完数有:");
		for (int i = 1; i <= 1000; i++) {
			int sum = 0;
			for (int j = 1; j < i; j++) {
				if (i % j == 0) {
					sum += j;
				}
			}
			if (sum == i) {
				System.out.println(i);
			}
		}
	}
}


5.输入两个正整数m和n,求其最大公约数和最小公倍数

public class Test1 {
	public static void main(String[] args) {
		int m = 12, n = 28;
		// 获取m和n的较大值
		int max = (m > n) ? m : n;
		// 获取m和n的较小值
		int min = (m < n) ? m : n;

		// 求m和n的最大公约数
		for (int i = min; i >= 1; i--) {
			if (m % i == 0 && n % i == 0) {
				System.out.println("m和n的最大公约数是:" + i);
				break;
			}
		}
		// 求m和n的最小公倍数
		for (int i = max; i <= m * n; i++) {
			if (i % m == 0 && i % n == 0) {
				System.out.println("m和n的最小公倍数是:" + i);
				break;
			}
		}
	}
}


6.输出所有的水仙花数

所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身。

例如: 153 = 1*1*1 + 3*3*3 + 5*5*5

public class Test1 {
	public static void main(String[] args) {
		for (int i = 100; i < 1000; i++) {// 实现所有的三位数的一个遍历
			int j1 = 0;
			int j2 = 0;
			int j3 = 0;
			j1 = i / 100;// 百位
			j2 = (i - 100 * j1) / 10;// 十位
			j3 = i - 100 * j1 - 10 * j2;// 个位

			if (i == j1 * j1 * j1 + j2 * j2 * j2 + j3 * j3 * j3) {
				System.out.println("此数值为满足条件的水仙花数:" + i);
			}
		}
	}
}


结语


本人会持续更新文章的哦!希望大家一键三连,你们的鼓励就是作者不断更新的动力

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麟-小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值