投币java编程,在投币游戏上实现Java代码

I have looked all over this website and other websites trying to figure how to do this with the knowledge that my teacher has provided me.

Basically the program is supposed to be a user input system where you can make bets on a head or tails coin flipping game.

So far, I've had no luck in what to do. Not asking someone to finish it for me, but to help me in the right direction.

Thank you in advance.

Here is my code so far:

package Homework6;

import java.util.Scanner;

import java.lang.Math;

public class CoinFlip {

public static void main(String[]args){

Scanner input = new Scanner(System.in);

int money = 10;

double flip = Math.random();

String heads = "heads";

String tails = "tails";

int bet = input.nextInt();

while(bet < 0 || bet > money){

if(true){

System.out.println("How much do you want to bet?");

input.next();

}

}

System.out.println("Guess if the coin will be heads or tails.");

String guess = input.next();

String coin;

if (flip <0.5) coin = heads;

else coin = tails;

if(guess == coin){

int correct = money + bet;

System.out.println("The flip was"+ coin);

System.out.println("You win" + bet);

System.out.println("")

}

}

}

解决方案

A few notes for you (since you're starting out and still a student):

1) Your while loop is a little bit poorly constructed. That System Out will constantly be printing to the screen until there is some input. And that if statement is essentially doing no conditional checking. It is always true.

2) You never originally prompt the user for how much they want to bet. You're only prompting them after they've input a value. So when you originally hit play, the console is just blank. But really it's waiting for some input.

3) Your if statement for changing the coin state is a little extraneous.

4) The user's amount of money is never actually updated, and the output at the end could be a little more informative.

5) What if someone wins the first one and wants to play again? They probably want to keep that bank growing! So you could encompass the entire thing inside of a do/while that runs while bank > 0.

6) You should only use the == operator when comparing primitive types. Pretty much all other times, use Object.equals(object);

I know you didn't ask someone to complete it for you, but this site works best by sharing information. Below should work for you, I've commented it extensively. Hopefully this can teach some things beyond just this homework assignment.

public static void main(String[] args)

{

// Setup some initial variables

int bank = 10;

double flip = Math.random();

// Instantiate a new Scanner object

Scanner input = new Scanner(System.in);

// Let the user bet until they run out of money in the bank

do

{

// Ask the user how much money to wager

System.out.println("How much do you want to bet?");

int bet = input.nextInt();

// The bet was invalid

while(bet < 0 || bet > bank)

{

System.out.println("Invalid bet (" + bet + "). Minimum bet = 0. Maximum bet = " + bank);

bet = input.nextInt();

}

// Ask the user for which side

System.out.println("Heads or tails?");

String guess = input.next();

// The coin is either heads or tails, so it can always start as tails. If the value goes under .5, change it to heads. If not, it remains the same, "tails".

String coin = "tails";

if (flip < 0.5)

{

coin = "heads";

}

// Tell the user the result, regardless of win or lose

System.out.println("The flip was " + coin);

// Update the user's bank and inform them of the new value

if(guess.equalsIgnoreCase(coin)) // this allows for any form of HeaDs or TaILs

{

bank += bet;

System.out.println("You win " + bet);

System.out.println("Your bank contains " + bank);

}

else

{

bank -= bet;

System.out.println("You lose " + bet);

System.out.println("Your bank contains " + bank);

}

}

while(bank > 0);

// Goodbye message

System.out.println("Thanks for playing!");

// Don't forget to close your Scanner object!

input.close();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Java面向对象自动贩卖机程序的示例代码,其中包括多个子类、多态、接口、继承、投币支付、微信支付和支付宝支付、库存不足异常以及具体货架位置等: 首先,我们定义一个自动贩卖机类 VendingMachine,它包含了一个 Map 对象 shelves,用于记录货架位置和货架编号的对应关系: ```java import java.util.HashMap; import java.util.Map; public class VendingMachine { protected Map<String, String> shelves; public VendingMachine() { shelves = new HashMap<>(); } public void showShelves() { System.out.println("Shelves: " + shelves.toString()); } } ``` 然后,我们定义一个商品类 Item,它包含了商品的名称、价格和数量: ```java public class Item { private String name; private double price; private int count; public Item(String name, double price, int count) { this.name = name; this.price = price; this.count = count; } public String getName() { return name; } public double getPrice() { return price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } } ``` 接下来,我们定义一个货架类 Shelf,它包含了一个商品对象和一个位置编号: ```java public class Shelf { private Item item; private String position; public Shelf(Item item, String position) { this.item = item; this.position = position; } public Item getItem() { return item; } public String getPosition() { return position; } } ``` 然后,我们定义一个支付接口 Payable,它包含了投币支付、微信支付和支付宝支付的方法: ```java public interface Payable { public void coinPay(double amount); public void weChatPay(double amount); public void aliPay(double amount); } ``` 接下来,我们定义一个自动贩卖机的子类 CoinVendingMachine,它实现了支付接口 Payable: ```java public class CoinVendingMachine extends VendingMachine implements Payable { private double balance; public CoinVendingMachine() { super(); balance = 0; } public void coinPay(double amount) { balance += amount; System.out.println("Coin payment of " + amount + " accepted."); System.out.println("Current balance: " + balance); } public void weChatPay(double amount) { System.out.println("WeChat payment is not supported."); } public void aliPay(double amount) { System.out.println("AliPay payment is not supported."); } public void buyItem(String position) throws OutOfStockException { String shelfNumber = shelves.get(position); Shelf shelf = getShelf(shelfNumber); Item item = shelf.getItem(); if (item.getCount() == 0) { throw new OutOfStockException("Out of stock: " + item.getName()); } item.setCount(item.getCount() - 1); System.out.println("Item purchased: " + item.getName()); balance -= item.getPrice(); System.out.println("Current balance: " + balance); } private Shelf getShelf(String shelfNumber) { // Implementation omitted } } ``` 我们还定义了一个自动贩卖机的子类 MobileVendingMachine,它实现了支付接口 Payable,并支持微信支付和支付宝支付: ```java public class MobileVendingMachine extends VendingMachine implements Payable { private double balance; public MobileVendingMachine() { super(); balance = 0; } public void coinPay(double amount) { System.out.println("Coin payment is not supported."); } public void weChatPay(double amount) { balance += amount; System.out.println("WeChat payment of " + amount + " accepted."); System.out.println("Current balance: " + balance); } public void aliPay(double amount) { balance += amount; System.out.println("AliPay payment of " + amount + " accepted."); System.out.println("Current balance: " + balance); } public void buyItem(String position) throws OutOfStockException { String shelfNumber = shelves.get(position); Shelf shelf = getShelf(shelfNumber); Item item = shelf.getItem(); if (item.getCount() == 0) { throw new OutOfStockException("Out of stock: " + item.getName()); } item.setCount(item.getCount() - 1); System.out.println("Item purchased: " + item.getName()); balance -= item.getPrice(); System.out.println("Current balance: " + balance); } private Shelf getShelf(String shelfNumber) { // Implementation omitted } } ``` 最后,我们定义了一个库存不足异常类 OutOfStockException,它继承自 Exception 类: ```java public class OutOfStockException extends Exception { public OutOfStockException(String message) { super(message); } } ``` 这就是一个简单的Java面向对象自动贩卖机程序的示例代码,它包含了多个子类、多态、接口、继承、投币支付、微信支付和支付宝支付、库存不足异常以及具体货架位置等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值