java练习题变量的使用 if语句的使用 for循环的使用 数组的使用使用

变量和数据类型

输出个人简历

要求:使用变量存储数据,实现个人简历信息的输出
	public static void main(String[] args) {
		String studentNmae = "小明";
		int age = 25;
		byte workDate = 3;
		byte projectNumber = 5;
		String technicalDirection = "java";
		String habit = "篮球";
		System.out.println("这个同学的姓名为:"+studentNmae);
		System.out.println("年龄是:"+age);
		System.out.println("工作了"+workDate+"年了");
		System.out.println("做过"+projectNumber+"个项目");
		System.out.println("技术方向是"+technicalDirection);
		System.out.println("兴趣爱好是"+habit);
	}

输出台打印

输入并显示会员卡号

要求:使用Scanner类获取键盘输入的会员卡号,并将该数据存储在变量中,输出这个变量的信息
public static void main(String[] args){
	Scanner input = new Scanner(system.in);//需要先导包
	system.out.print("请输入您的会员卡号:");
	int cardNumber = input.nextInt(); 
	system.out.println("您的会员卡号为:"+cardNumber);
}

输出打印

求四位会员卡号之和

1、键盘输入四位数字的会员卡号
2、使用“/”和“%”运算符分解获得会员卡各个位上的数字
3、将各个位上数字求和
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个四位数的会员卡号:");
		int number = input.nextInt();
		int singleDigit = number%10;
		int numberTens = (number%100-singleDigit)/10;
		int numberHundreds = (number%1000-numberTens*10-singleDigit)/100;
		int numberThousands = (number-numberHundreds*100-numberTens*10-singleDigit)/1000;
		int sum = numberThousands+numberHundreds+numberTens+singleDigit;
		System.out.println("千位数:"+numberThousands+"	百位数:"+numberHundreds+"		十位数:"+numberTens+"		个位数:"+singleDigit);
		System.out.println("会员卡号"+number+"各位之和为:"+sum);
	}
	}

输出台打印

if选择结构

判断是否中奖

根据分解后的数字之和,判断用户是否中奖。
如果数字之和大于20,则中奖
public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个四位数的会员卡号:");
		int number = input.nextInt();
		int singleDigit = number%10;
		int numberTens = (number%100-singleDigit)/10;
		int numberHundreds = (number%1000-numberTens*10-singleDigit)/100;
		int numberThousands = (number-numberHundreds*100-numberTens*10-singleDigit)/1000;
		int sum = numberThousands+numberHundreds+numberTens+singleDigit;
		System.out.println("千位数:"+numberThousands+"	百位数:"+numberHundreds+"		十位数:"+numberTens+"		个位数:"+singleDigit);
		System.out.println("会员卡号"+number+"各位之和为:"+sum);
		String winner = ",恭喜您中奖了!奖品是MP3!";
		String loser = ",很遗憾,您没有中奖!";
		String result = sum > 20 ? winner : loser;
		System.out.println("会员卡号"+number+"的用户"+result);
	}

控制台输出

实现幸运抽奖

条件:会员号的百位数等于产生的随机数即为幸运会员
			
提示:产生随机数(0~9)的方法:int random  = (int Math.random()*10);
public class Demo_03 {
	//抽奖规则:会员号的百位数字等于产生的随机数字即为幸运会员
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您的会员卡号:");
		int cardNo = input.nextInt();
		int numberHundreds = cardNo%1000/100;
		int random = (int) (Math.random()*10);
		if (numberHundreds == random) {
			System.out.println("恭喜您中奖了!");
		} else {
			System.out.println("很遗憾,您没有中奖!");
		}
	}

在这里插入图片描述

for循环

循环输入某同学S1结业考试成绩

**条件:**循环输入某同学S1结业考试的5门课成绩,并计算平均分。

	public static void main(String[] args){
		int sum = 0 ;
		int average;
		Scanner input = new Scanner(System.in);
		System.out.print("请输入学生姓名:");
		String studentName = input.next();
		for(int i = 0 ; i < 5 ; i++ ){
			System.out.print("请输入5门课中第"+(i+1)+"门课的成绩:");
			int score = input.nextInt();
			sum += score;
		}
		average = sum/5;
		System.out.println(studentName+"的平均分是:"+average);
	}

在这里插入图片描述

加法口诀表

public static void main(String[] args) {
		//加法口诀表
		for(int i = 1; i < 10; i++){
			for (int j = i; j > 0 ; j--) {
				System.out.print("    "+i+" + "+j+" = "+(i+j));
			}
			System.out.println();
		}
	}

在这里插入图片描述

用着三种循环方式求偶数只和

要求: 使用while、do-while、for循环三种编程方式实现:计算100以内(包括100)的偶数只和

public static void main(String[] args) {
		int sum1 = 0;
		int sum2 = 0;
		int sum3 = 0;
		int m = 2;
		while(m<=100){
			sum1+=m;
			m+=2;
		}
		System.out.println("sum1="+sum1);
		System.out.println("-----------------------");
		int n = 2;
		do{
			sum2+=n;
			n+=2;
		}while(n<=100);
		System.out.println("sum2="+sum2);
		System.out.println("-----------------------");
		for(int i = 2 ; i <= 100;){
			sum3+=i;
			i+=2;
		}
		System.out.println("sum3="+sum3);
		System.out.println("-----------------------");
		int sum4 = 0;
		for(int i = 1 ; i <= 100 ; i++){
			if(i%2==0){
				sum4+=i;
			}
		}
		System.out.println("sum4="+sum4);
	}

在这里插入图片描述

数组

猜数游戏

**要求:**有一个数列:8、4、2、1、23、344、12,循环输出数列的值,求数列中的所有数的和,猜数游戏:从键盘中任意输入一个数据,判断数列中是否包含此数。

public static void main(String[] args){
	//定义初始化一个数组
	int[] number = {8,4,2,1,23,334,12};
	//循环输出数列的值
	for(){}
	
}

冒泡排序

public class BubbleSort {
	//冒泡排序
	public static void main(String[] args) {
		int[] num = new int[8];
		Scanner input = new Scanner(System.in);
		for (int i = 0; i < num.length; i++) {
			System.out.println("请输入第"+(i+1)+"个数");
			num[i] = input.nextInt();
		}
		for (int i = 0; i < num.length; i++) {
			for (int j = i+1; j < num.length; j++) {
				if(num[i]>num[j]){
					int temp = num[i];
					num[i] = num[j];
					num[j] = temp;
				}
			}
		}
		for (int i = 0; i < num.length; i++) {
			System.out.print(num[i]);
		}
	}

二分查找法

	public static void main(String[] args) {
	
		//二分查找法
		Scanner input = new Scanner(System.in);
		int[] score = new int[]{15,25,6,8,47,59,58,14,54};
		Arrays.sort(score);
		System.out.println(Arrays.toString(score));
		
		System.out.println("请输入您要查找的数:");
		int searchNum = input.nextInt();
		int startIndex = 0;
		int endIndex = score.length;
		int midIndex = -1;
		boolean isFind = false;
		do {
			midIndex = (startIndex+endIndex)/2;
			if (score[midIndex] == searchNum) {
				System.out.println("恭喜你,找打了!下标是:"+midIndex);
				isFind = true;
				break;
			}else if (score[midIndex] > searchNum) {
				endIndex = midIndex - 1;
			}else {
				startIndex = midIndex+1;
			}
		} while (startIndex<=endIndex);
		if(!isFind){
			System.out.println("对不起,未找到:"+searchNum);
		}
	}

使用二维数组计算总成绩

已经知道有3个班级各有5名学员,请使用二维数组计算各个班级的总成绩

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int[][] score = new int[3][5];
		for (int i = 0; i < score.length; i++) {
			System.out.println("********第"+(i+1)+"个班********");
			for (int j = 0; j < score[i].length; j++) {
				System.out.print("请输入第"+(j+1)+"个学生的成绩:");
				score[i][j] = input.nextInt();
			}
		}
		for (int i = 0; i < score.length; i++) {
			int sum = 0;
			for (int j = 0; j < score[i].length; j++) {
				sum+=score[i][j];
			}
			System.out.println((i+1)+"班总成绩:"+sum);
		}
	}

简单的图书管理系统

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

		String[][] bookInfor = initi().clone();
		while (true) {
			System.out
					.println("-----------------------------------------------");
			System.out
					.println("1:新增图书  2、查看图书  3、删除图书  4、借出图书  5、归还图书  0、退出图书系统");
			System.out
					.println("-----------------------------------------------");
			System.out.println("请输入您需要操作的序号:");
			int no = input.nextInt();
			if (no == 0) {
				break;
			}
			switch (no) {
			case 1:
				addBook(bookInfor);
				break;
			case 2:
				checkBook(bookInfor);
				break;
			case 3:
				deleteBook(bookInfor);
				break;
			case 4:
				lendBook(bookInfor);
				break;
			case 5:
				returnBook(bookInfor);
				break;
			default:
				System.out.println("输入有误!请重新输入:");
				break;
			}
		}
	}
	
	// 初始化操作
	public static String[][] initi() {
		Date date = new Date();
		String[][] bookInformation = new String[6][4];
		bookInformation[0][0] = "语文";
		bookInformation[0][1] = "可借";
		bookInformation[0][2] = date.toString();
		bookInformation[0][3] = "0";
		bookInformation[1][0] = "数学";
		bookInformation[1][1] = "可借";
		bookInformation[1][2] = date.toString();
		bookInformation[1][3] = "0";
		for (int i = 2; i < bookInformation.length; i++) {
			for (int j = 0; j < bookInformation[i].length; j++) {
				bookInformation[i][j] = null;
			}
		}

		return bookInformation;
	}

	// 查看书本
	public static void checkBook(String[][] bookInfor) {
		System.out.println("书名\t\t\t\t是否可借\t\t\t\t借出日期\t\t\t\t\t\t\t借出次数");
		for (int i = 0; i < bookInfor.length; i++) {
			if (bookInfor[i][0]!=null) {
				for (int j = 0; j < bookInfor[i].length; j++) {
					System.out.print(bookInfor[i][j] + "\t\t\t\t");
				}
				System.out.println();
			} else {
				continue;
			}
		}
	}

	// 添加书本
	public static void addBook(String[][] bookInfor) {
		Date date = new Date();
		Scanner input = new Scanner(System.in);
		int count = 0;
		for (int j = 0; j < bookInfor.length; j++) {
			if (bookInfor[j][0]!=null) {
				count++;
			}
		}
		if(count==6){
			System.out.println("书本数已经添加满了。");
			return ;
		}else {			
			for (int i = 0; i < bookInfor.length; i++) {
				if (bookInfor[i][0] == null) {
					System.out.println("请输入书名:");
					bookInfor[i][0] = input.next();
					bookInfor[i][1] = "可借";
					bookInfor[i][2] = date.toString();
					bookInfor[i][3] = "0";
					break;
				}
			}
		
		}
	}

	// 删除书本
	public static String[][] deleteBook(String[][] bookInfor) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您需要删除的书名:");
		String bookName = input.next();
		int count = 0;
		for (int i = 0; i < bookInfor.length; i++) {
			if (bookInfor[i][0] != null) {
				if (bookInfor[i][0].equalsIgnoreCase(bookName)) {
					for (int j = 0; j < bookInfor[i].length; j++) {
						bookInfor[i][j] =null;
						count++;
					}
				}
			}
		}
		if(count>0){
			System.out.println("删除图书完成!");			
		}else {
			System.out.println("未找到您想要删除的图书");
		}
		return bookInfor;
	}

	//借出图书
	public static String[][] lendBook(String[][] bookInfor){
		Scanner input = new Scanner(System.in);
		int count = 0;
		for (int i = 0; i < bookInfor.length; i++) {
			if (bookInfor[i][0]!=null) {
				System.out.println(bookInfor[i][0]+"\t"+bookInfor[i][1]);				
			}
		}
		System.out.println("请输入您要借的书名:");
		String bookNameString = input.next();
		for (int i = 0; i < bookInfor.length; i++) {
			if(bookInfor[i][0]!=null){				
				if (bookInfor[i][0].equalsIgnoreCase(bookNameString)) {
					if(bookInfor[i][3].hashCode()==(48+6)){//判断借书次数是否为6
						System.out.println("不好意思,该图书已借完!");
						count++;
						break;
					}else{
						Date date = new Date();
						bookInfor[i][2] = date.toString();
						int num =(bookInfor[i][3].hashCode()+1);
						char ch = (char)num;
						bookInfor[i][3] = ""+ch;						
						count++;			
					}
				}
			}
		}
		if (count>0) {
			System.out.println("借出图书操作完成!");
		}else {
			System.out.println("未找到您想借阅的图书!");
		}
		return bookInfor;
	}
	//归还图书
	public static String[][] returnBook(String[][] bookInfor){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您要归还的书名:");
		String bookNameString = input.next();
		for (int i = 0; i < bookInfor.length; i++) {
			if(bookInfor[i][0]!=null){				
				if (bookInfor[i][0].equalsIgnoreCase(bookNameString)) {
					if (bookInfor[i][3].hashCode()<49) {
						System.out.println("该书没有借出!");
						break;
					}else {
						int num =(bookInfor[i][3].hashCode()-1);
						char ch = (char)num;
						bookInfor[i][3] = ""+ch;												
						System.out.println("归还图书操作完成!");		
					}
				}
			}
		}
		return	 bookInfor;
	}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值