本文目录一览:
- 1、java投硬币题目
- 2、Java硬币题?
- 3、java编写:计算一元钱硬币有多少种表达方
- 4、Java 抛掷硬币,H代表正面朝上,T代表反面朝上,用迭代法编写程序
- 5、关于java抛硬币的一个对象。
- 6、java 硬币1,1,2,2,4,4,8,8.支付n元有多少种方法 java
java投硬币题目
硬币面枚举:
public enum CoinSide {
HEAD(0), TAIL(1);
private int value;
CoinSide(int value) {
this.value = value;
}
}
2.硬币类
import java.util.Random;
public class Coin {
private String name;
private CoinSide coinSide;
public Coin(String name) {
this.name = name;
}
public CoinSide getCoinSide() {
return coinSide;
}
public void setCoinSide(CoinSide coinSide) {
this.coinSide = coinSide;
}
public CoinSide tossCoin() {
Random random = new Random();
int value = random.nextInt(2);
if (value == 0) {
this.coinSide = CoinSide.HEAD;
} else {
this.coinSide = CoinSide.TAIL;
}
this.printCoinSide();
return this.coinSide;
}
public void printCoinSide() {
System.out.println(this.name + " toss " + this.coinSide.name());
}
}
3.主类
public class Main {
public static void main(String[] args) {
Coin coin1 = new Coin("coin1");
Coin coin2 = new Coin("coin2");
while (true) {
coin1.tossCoin();
coin2.tossCoin();
if (coin1.getCoinSide() == coin2.getCoinSide() coin1.getCoinSide() == CoinSide.TAIL) {
break;
}
System.out.println("-------------------");
}
}
}
运行演示:
Java硬币题?
//硬币类
public class Coin {
private int head = 1;
private int tail = 0;
private int result;
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public Coin(){
result = 1;
}
public void throwCoin(){
int random = (int) (Math.random() * 10 / 5);
if(head == random){
result = head;
}else{
result = tail;
}
}
public int getHead() {
return head;
}
public void setHead(int head) {
this.head = head;
}
public int getTail() {
return tail;
}
public void setTail(int tail) {
this.tail = tail;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Coin coin = new Coin();
coin.throwCoin();
if(coin.getResult() == coin.getHead()){
System.out.println("You Win.");
}else if(coin.getResult() == coin.getTail()){
System.out.println("You Lose.");
}
}
}
java编写:计算一元钱硬币有多少种表达方
import java.util.ArrayList;
/*
* 一元钱硬币有多少种表达方式
* 可供选择:1分,2分,5分,1角,2角,5角,1元
* 如:
* 1元=1元;
* 1元=5角+5角;
* ....
*/
public class Test {
private static int count;
public static void main(String args[]){
int max = 100;//一元
int[] cents = {100,50,20,10,5,2,1};//币值
String[] money = {"1元","5角","2角","1角","5分","2分","1分"};
ArrayList collect = new ArrayList();
collectMoney(cents, money,0, max, 0, collect);
System.out.println("总共有"+count+"种搭配方法!");
}
public static void collectMoney(int[] cents,String[] money,int beginIndex,int max,int result,ArrayList collect){
if(resultgt;=max){
if(result==max){
count++;
System.out.print("1元=");
for(int i=0;ilt;collect.size();i++){
System.out.print(money[(Integer) collect.get(i)]);
if(ilt;collect.size()-1){
System.out.print("+");
}
}
System.out.println();
}
return;
}
for(int i=beginIndex;ilt;cents.length;i++){
int cent = cents[i];
collect.add(i);
collectMoney(cents, money,i, max, result+cent, collect);
collect.remove(Integer.valueOf(i));
}
}
}
Java 抛掷硬币,H代表正面朝上,T代表反面朝上,用迭代法编写程序
JAVA练习之——抛硬币
import java.util.*;
import java.io.*;
interface game{
String get(int a);
}
interface gamefactory{
game getgame();
}
class yingbi implements game{
public String get(int a){
if(a==0) return "正面";
else return "反面";
}
}
class yingbifactory implements gamefactory{
public game getgame(){
return new yingbi();
}
}
class shazi implements game{
public String get(int a){
return String.valueOf(a+1);
}}
class shaizifactory implements gamefactory{
public game getgame(){return new shazi(){
public void a(){System.out.println("new shazi()");
}
};}
}
public class Game {
public static void games(gamefactory factory,int a){
game s1=factory.getgame();
System.out.println(s1.get(a));
}
public static void print(ArrayListString e){
for(String y:e)
System.out.print(y);
}
/**
* @param args
*/
public static void main(String[] args) throws IOException{
DataInputStream in=new DataInputStream(System.in);
while(true){
System.out.println("请选择1:开始");
System.out.println("1:抛硬币");
int opinion=Integer.parseInt(in.readLine());
switch(opinion){
case 1:
System.out.println("开始?Y/N");
String a=in.readLine();
ArrayListString yin=new ArrayListString();
while(a.equals("Y")||a.equals("y")){
Random rand=new Random();
yingbifactory q=new yingbifactory();
int w=rand.nextInt(2);
yin.add(q.getgame().get(w));
games(q,w);
a=in.readLine();
}
print(yin);
System.out.println();
break;
default:
System.exit(0);
}
// TODO 自动生成方法存根
}
}
}
关于java抛硬币的一个对象。
package test;
public class Coin{
private final int HEADS=0;//正面
private final int TAILS=1;//反面
private int face;//存储flip方法得到的随机值
public Coin()//调用硬币这个方法
{
flip();
}
public void flip()//硬币发放中的抛硬币方法
{
face=(int) (Math.random()*2);// Math.random()会得到0到1之间的数,乘2得到0到2之间的小数,因为强制转换int类型所以,小于1的小数转换成了0,同理大于等于1小于2的转换成了1.代表了正反面
}
public boolean isHeads()//判断是否正面,未调用过,可能在其他类会用到判断方法。
{
return (face==HEADS); //返回布尔类型值
}
public String toString()//将正反面的0,1转换成字符串的方法
{
String faceName;
if(face==HEADS)//之前得到了face整数0或1,当face等于HEADS即等于0时,将faceName赋值为Heads
faceName="Heads";
else
faceName="Tails";//同上
return faceName;
}
}
java 硬币1,1,2,2,4,4,8,8.支付n元有多少种方法 java
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Scanner;
public class Test {
static HashSetInteger[] resultSet = new HashSetInteger[]();//存放结果
static HashMapInteger, Integer coinsMap;//将硬币分类 种类——数量
static int max = 0;
public static void main(String[] args) throws IOException {
int coins[] = {1,1,2,2,4,4,8,8};//硬币集合
// Scanner sc = new Scanner(System.in);
// int n = sc.nextInt();//输入n的值
int n = 16;
count(coins, n);
System.out.println();
System.out.println(resultSet.size());
/* 输出结果 每种硬币的数量*/
for(Integer[] ints : resultSet){
int i = 0;
for(EntryInteger, Integer entry : coinsMap.entrySet()){
System.out.println("coins:" + entry.getKey() + " num:" + ints[i++] );
}
System.out.println();
}
}
/**
* 将硬币归类并统计每类的数量
* @param coins
* @param n
* @throws IOException
*/
public static void count(int[] coins, int n) throws IOException{
coinsMap = new HashMapInteger,Integer();
for(int i = 0;i coins.length;i ++){
int count = 0;
if(coinsMap.containsKey(coins[i])){
count = coinsMap.get(coins[i]);
}
++ count;
max += coins[i];
coinsMap.put(coins[i], count);
}
Integer[][] pool = new Integer[coinsMap.size()][2];
int index = 0;
for(EntryInteger, Integer entry: coinsMap.entrySet()){
pool[index][0] = entry.getKey();
pool[index][1] = entry.getValue();
index ++;
}
if(n max){
System.out.println("超过所以硬币总额");
}
else{
recursive(pool, 0, 0, n,new Integer[pool.length]);
}
}
/**
* 利用递归的方式求硬币的组合
* @param pool
* @param coin
* @param sum
* @param n
* @param result
* @throws IOException
*/
public static void recursive(Integer[][] pool,int coin,int sum,int n,Integer result[]) throws IOException{
if(coin == pool.length){//已经考虑了所以种类的硬币时,结束递归
return;
}
int tSum = sum;
for(int i = 0;i = pool[coin][1];i ++){
int temp = i * pool[coin][0];
tSum = sum + temp;
Integer[] _result = new Integer[pool.length];
for(int j = 0; j coin;j ++){
_result[j] = result[j];
}
_result[coin] = i;
// for(int k = 0;k coin;k++) System.out.print(" ");
// System.out.println("coin:"+pool[coin][0] + " num:"+ i + " " + sum + "_" + tSum + " " );
if(tSum == n coin == pool.length - 1){//已经考虑到最后一种硬币类型且硬币组合的面值与n相等
resultSet.add(_result);
// System.out.println("here is an answer");
}
else{
recursive(pool, coin + 1, tSum, n, _result);
}
}
}
}
好久没写这种的,写了好久。如有不对,欢迎指正!