基于java的简单英雄联盟胜率计算

基于java的简单英雄联盟胜率计算

  首先声明,楼主是一个LOLer,技术还说的过去。今天下午楼主的同学看到楼主匹配胜率感人,非说楼主是小学生,非说匹配胜率要50%以上才算不坑,55%以上才能有carry作用。所以楼主想算算再赢多少盘能算不坑,于是就做了个算法。下图是楼主的胜率和程序的运行结果。




楼主胜率感人,想成为不坑的人有些难啊,大笑下面是代码实现。

主函数

package 胜率;

import java.util.Scanner;

/**
 * 
 * @author Sakuragi
 * 
 */
public class Demo {
	private static Scanner sc;

	public static void main(String[] args) {
		int all;// 总场次
		int win;// 胜利场次
		sc = new Scanner(System.in);
		System.out.println("输入总场次");
		all = sc.nextInt();
		System.out.println("输入获胜场次");
		win = sc.nextInt();
		new Pencent(win, all);
		System.out.println("输入您想要的胜率,如: 50%");
		String pec = sc.next();
		new Pencent(win, all, pec);

	}
}

 

 胜率计算函数

package 胜率;

import java.text.NumberFormat;

/**
 * 
 * @author Sakuragi
 * 
 */
public class Pencent {

	Pencent() {
	};

	public Pencent(int win, int all) {
		// 创建一个数值格式化对象

		NumberFormat numberFormat = NumberFormat.getInstance();

		// 设置精确到小数点后2位

		numberFormat.setMaximumFractionDigits(2);

		String result = numberFormat.format((float) win / (float) all * 100);

		System.out.println("您当前的胜率为:" + result + "%");

	}

	public Pencent(int win, int all, String pec) {
		// 创建一个数值格式化对象

		NumberFormat numberFormat = NumberFormat.getInstance();

		// 设置精确到小数点后2位

		numberFormat.setMaximumFractionDigits(2);
		/**
		 * pec2:去掉%的胜率 强制转换为float类型便于计算。
		 */
		float pec2 = (float) (Float.parseFloat(pec.substring(0,
				pec.length() - 1)) * 0.01);
		// result为需要获胜的盘数
		String result = numberFormat.format(((float) win - (float) all * pec2)
				/ (pec2 - 1));

		System.out.println("您需要胜利" + result + "次");
		System.out.println("您的胜率才为" + pec);
		System.out.println("您的总场次将为:" + (all + Integer.parseInt(result))
				+ "您的胜利场次为" + (win + Integer.parseInt(result)));

	};
}

总结

1.楼主注释写的很详细了,算法也很简单,注意类型转换。

2.了解NumberFormat和parseInt和parseFloat的用法。

3.楼主不是“小学生”,胜率低原因很多,自行脑补。

 

 

猜你喜欢

转载自bs-yg.iteye.com/blog/2255998