62,620
社区成员
发帖
与我相关
我的任务
分享
package com.keeya.csdn.test;
import java.util.Random;
import java.util.Scanner;
/*
*
用java 中的递归实现苹果机~ 苹果机:
共有八种图案,分别为
苹果(2倍) 桔子(4倍)
铃铛(6倍) 木瓜(6倍)
西瓜(8倍) 双星(10倍)
双七(12倍)大王(14倍)
用户初始化有10个金币。
一次只能压一种图案,如果中了,所压金币翻倍,
否则,损失金币。 如何用java 编程实现?
小弟我是初学者,希望有详细代码~!
*/
public class PlayGame {
public static void main(String[] args) {
PlayGame pg = new PlayGame();
pg.play();
}
// 用 1 2 3 ... 代替 苹果,桔子... public static
private int money;
private Scanner sc;
private Random rd;
public PlayGame() {
money = 10;
sc = new Scanner(System.in);
rd = new Random();
}
public void play() {
int in = 0, choice = 0;
System.out.println("请压注");
try {
in = sc.nextInt();
} catch (Exception e) {
System.out.println("输入格式不对,重新开始");
return;
}
System.out.println("请选择压什么");
try {
choice = sc.nextInt();
} catch (Exception e) {
System.out.println("输入格式不对,重新开始");
return;
}
// 用户输入的大于7 直接给取模,保证在 [1,7]
// 这里投机了下懒的写switch语句了
// 直接用的1到7 生成 2 到 14倍 没有2个 6倍了
if (choice % 7 + 1 == rd.nextInt(7) + 1) {
money += (2 * (choice % 7 + 1) - 1) * in;
System.out.println("恭喜你,压对了;赢了[" + 2 * choice + "]倍,你还有[" + money
+ "]大洋");
} else {
money -= in;
System.out.println("压错了,扣除[" + in + "]大洋,你还有[" + money + "]大洋");
}
if (money == 0) {
System.out.println("你输光了,下次再来吧");
} else {
System.out.println("退出请输入[exit],任意键继续");
if ("exit".equals(sc.next()))
return;
play();
}
}
}
package action;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
public int money = 10; //你的钱,初始金额。
public String[] stroption = {"apple","orange","lingdang","mugua","xigua","shuangxing","shuangqi","dawang"};//length 为8
public int[] iOption = {2,4,6,6,8,10,12,14}; //length 为8
public int getSystemOption()
{
return (int)(Math.random()*(iOption.length + 1)); //范围[0,9)
}
public void game(int imoney){
while(imoney >money || imoney <=0){
System.out.println("输入金额不合法或余额不足,您的余额是:"+money);
System.out.println("请输入钱,回车结束");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String input = br.readLine();
imoney = Integer.parseInt(input);
} catch (Exception e) {
//e.printStackTrace();
System.out.println("输入金额不合法");
}
}
int opt = getSystemOption();// 范围:[0,9)
if(opt >= stroption.length ){ // opt = 8
System.out.println("你没中,扣去" + imoney);
money -= imoney;
}
else{
System.out.println("你选中了" + stroption[opt] + ",得到" + imoney*iOption[opt]);
money += imoney*iOption[opt];
}
System.out.println("你的余额是"+money);
if(money<0)
return ;
else{
System.out.println("请输入钱,回车结束");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String input = br.readLine();
int iOption = Integer.parseInt(input);
game(iOption);
} catch (Exception e) {
System.out.println("输入金额不合法");
}
}
}
public static void main(String[] args) {
new test().game(0);
}
}
package action;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test {
/**
* @param args
*/
public int money = 10; //你的钱,初始金额。
public void game(int imoney){
if(imoney >money || imoney <=0){
System.out.println("输入金额不合法或余额不足"+money);
System.out.println("请输入钱,回车结束");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String input = br.readLine();
imoney = Integer.parseInt(input);
} catch (IOException e) {
e.printStackTrace();
}
}
int opt = getSystemOption() + 1;
if(opt > stroption.length){
System.out.println("你没中,扣去" + imoney);
money -= imoney;
}
else{
System.out.println("你选中了" + stroption[opt] + ",得到" + imoney*iOption[opt]);
money += imoney*iOption[opt];
}
System.out.println("你的余额是"+money);
if(money<0)
return ;
else{
System.out.println("请输入钱,回车结束");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String input = br.readLine();
int iOption = Integer.parseInt(input);
game(iOption);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String[] stroption = {"apple","orange","mugua","xigua","shuangxing","shuangqi","dawang"};
public int[] iOption = {2,4,6,6,8,10,12,14};
public int getSystemOption()
{
return (int)(Math.random()*iOption.length);
}
public static void main(String[] args) {
new test().game(0);
}
}