Java自学练习代码页

1.钱币兑换

将给定的RMB换成张数

package feng.java.learning;

import java.util.Scanner;
/*
 * 将给定的RMB换成小钱
 * */
public class 货币兑换 {
	public static void main(String[] args) {
		//输入部分
		Scanner input = new Scanner(System.in);
		System.out.print("输入钱的数量:");
		double money = input.nextDouble();
		
		//解决元部分
		int yuan = (int)money;
		int numOfTenYuan = yuan / 10;	//10 元的张数
		int numOfFiveYuan = (yuan % 10)/5;	//5元的张数
		int numOfOneYuan = (yuan % 10) % 5;	//1元的张数
		
		//解决角的张数
		int jiao = ((int)(money * 10)) % 10;
		int numOfFiveJiao = jiao / 5;	//5角部分
		int numOfOneJiao = jiao % 5;	//1角部分
		
		//输出打印
		System.out.println("10元的张数:" + numOfTenYuan);
		System.out.println("5元的张数:" + numOfFiveYuan);
		System.out.println("1元的张数:" + numOfOneYuan);
		System.out.println("5角的张数:" + numOfFiveJiao);
		System.out.println("1角的张数:" + numOfOneJiao);	
	}
}

2、书写密码错误三次退出(while循环)

package feng.java.learning;

import java.util.Scanner;

public class whileDemo3 {

	/**
	 * 书写密码错误三次退出
	 */
	public static void main(String[] args) {
/*自己写的
 * 		//1、设置密码
		final String mima = "12345a";
		int i = 0;	//定义输入次数
		while(i < 3){
			//2、用户输入密码
			Scanner input = new Scanner(System.in);
			System.out.print("输入密码:");
			String scan = input.nextLine(); 
			i++;
			//3、循环判断密码对错
			if (scan.equals(mima)){
				i = Integer.MAX_VALUE;
			}else if(i < 3){
				System.out.print("错误" + i + "次,请再次输入:");
			}else if(i == 3){
				System.err.println("滚吧!!!");
				System.exit(0);
			}
		}
		//4、打印
		System.out.println("登陆成功!!!");
*/
		
//大佬写的
		Scanner input = new Scanner(System.in);
		final String PASSWORD = "12345a";		//密码
		//1、写出循环结构
		int i = 0;
		while(i < 3){
			System.out.print("请输入密码:");
			String password = input.next();
			//Java中的字符串不允许这样判断:if(password == PASSWORD){}
			//if(password.equals("1234a"))	//判断字符串相等的固定方式!!!
			i++;
			if(!PASSWORD.equals(password)){
				System.out.print("错误" + i + "次,请再次输入:");
				if(i == 3){
					System.err.println("密码输入达到3次,滚!!!");
					System.exit(0);
				}
			}else{//密码正确
				i = Integer.MAX_VALUE;
			}
		}
		System.out.println("登录成功!!!");
	}
}

3、双十一2015年的交易额为800亿,每年递增25%。问:按此增长哪年可以达到2000亿(while循环)

package feng.java.learning;

public class whileDemo4 {

	/**
	 * 双十一2015年的交易额为800亿,每年递增25%
	 * 问:按此增长哪年可以达到2000亿
	 */
	public static void main(String[] args) {
		int year = 2015;	//年份
		int transaction = 800;	//交易额
		int transactionTemp = transaction;	//中间变量
		while(transactionTemp <= 2000){
			transactionTemp *= 1.25;	//每年增长
			year ++;	//年份+1
		}
		System.out.println(year + "年达到2000亿!");
	}
}

4、拳皇小游戏(有待改善)

package feng.java.learning;

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Scanner;

import javax.swing.Action;
import javax.swing.plaf.SliderUI;

public class 拳皇 {

	/**
	 * 使用循环模拟现实玩家BOSS
	 * 1、双方初始HP均为100
	 * 2、每次攻击5-15
	 * 3、HP最先到达零或以下的被KO
	 * 4、玩家可以选择技能
	 * 5、根据所掉血量现实玩家和BOSS的状态
	 * @throws MalformedURLException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws MalformedURLException, InterruptedException {
		Scanner input = new Scanner(System.in);
		//音乐播放代码
		File muisc = new File("muisc/嚣张.wav");
		AudioClip sound_choose = Applet.newAudioClip(muisc.toURL());
		sound_choose.play();	//播放音乐

		//需要让程序暂停下,以便播放音乐
//		System.out.println("输入任意键继续!");
//		input.nextLine();
		
		//1、选名称
		System.out.print("输入您的名称:");
		String userName = input.next();
		String comName = "BOSS";
		
		//2、初始化生命,攻击力
		int hp1 = 100;	//玩家生命
		int hp2 = 100;	//boss生命

		
		//3、开打,玩家优先
		while(hp1 > 0 && hp2 > 0){
			int attack1 = (int)(Math.random() * 10000) % 11 + 5;	//攻击力由随机数生成,范围[5 - 15]
			int attack2 = (int)(Math.random() * 10000) % 11 + 5;
			hp2 -= attack1;
			Thread.sleep(2000);
			System.out.println("玩家打出伤害:" + attack1 + "," + comName + "剩余血量:" + hp2);
			hp1 -= attack2;
			Thread.sleep(2000);
			System.err.println("boss打出伤害:" + attack2 + "," + userName +"剩余血量:" + hp1);
		}
		
		//4、打印结果
		System.out.println("KO!");
		System.out.println("玩家\t生命值");
		System.out.println(userName + "\t" + hp1);
		System.out.println(comName + "\t" + hp2);
		if(hp1 < hp2){
			System.out.println("抱歉,未能打败boss,回去修炼吧!!!");
		}else{
			System.out.println("牛逼,成功打败boss,成仙了!!!");
		}
	}
}

计算器

package feng.java.learning;

import java.util.Scanner;

public class 计算器 {

	/**
	 * 实现简单的计算器功能+、-、*、/、%
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String choice = null;
		do{
			System.out.print("输入第一个数字:");
			double num1 = input.nextDouble();
			System.out.print("输入第二个数字:");
			double num2 = input.nextDouble();
			System.out.print("输入操作符:");
			String symbol = input.next();
			switch (symbol) {
			case "+":System.out.println(num1 + "+" + num2 + "=" + (num1 + num2));break;
			case "-":System.out.println(num1 + "-" + num2 + "=" + (num1 - num2));break;
			case "*":System.out.println(num1 + "*" + num2 + "=" + (num1 * num2));break;
			case "/":if(num1 == 0){
				System.out.println("傻狗,除数不能为0!!!");
			}else System.out.println(num1 + "/" + num2 + "=" + (num1 / num2));break;
			case "%":System.out.println(num1 + "%" + num2 + "=" + (num1 % num2));break;
			default:
			System.out.println("输入错误,傻旺财!!!");
				break;
			}
			System.out.print("是否继续计算?(y/n)");
			choice = input.next();
		}while("y".equalsIgnoreCase(choice));	//字符串判断用equals
		System.out.println("欢迎下次使用!!!");
	}
}

猜数字小游戏(循环)

package feng.java.learning;

import java.util.Scanner;

public class guessNum {

	/**
	 * 猜数字游戏
	 */
	public static void main(String[] args) {
		
		//1、用户要猜的数字
		int realNum = ((int)(Math.random() * 10000) % 9) + 1;

		//3、判断
		//4、循环次数
		for(int i = 0;i < 5 ; i++){
			int userNum = -1;	//用户输入数字初始化
			int count = 0;		//用户输入次数初始化
			//2、用户输入
			Scanner input = new Scanner(System.in);
			System.out.print("输入您猜的数字(1-9):");
			userNum = input.nextInt();
			count ++;	//用户输入次数累加
			if(userNum == realNum){	//猜对的情况
				if(count == 1){		//一次猜对
					System.out.println("真牛逼,开挂了!!!");
				}else if(count >= 2 && count <=3){	//2-3次猜对
					System.out.println("表现不错!!!");
				}else{	//4-5次猜对
					System.out.println("差点机会就用完了!!!");
				}
				break;
			}else if(userNum < realNum){	//数字小了
				System.out.print("小了!\t");
				System.out.println("猜错:" + count + "次");
				if(count == 5 ){	//五次机会用完
					System.out.println("机会用完了,下次再来!");
					break;
				}
			}else if(userNum > realNum){	//数字大了
				System.out.print("大了\t");
				System.out.println("猜错:" + count + "次");
				if(count == 5 ){	//五次机会用完
					System.out.println("机会用完了,下次再来!");
					break;
				}
			}
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值