「java投币实验」JAVA实验三

博主:adminadmin 2023-01-05 02:48:08 634

今天给各位分享java投币实验的知识,其中也会对JAVA实验三进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 编写程序,用0和1之间的随机数来模拟扔硬币实验,统计扔1000次后出现的正反面次数!求前

int z=0; //记录正面次数

int f=0; //记录反面次数

for(int i=0;i1000;i++){

if((int)(Math.random()*2)==0){

z++; //产生随机数为0时 正面出现次数+1 否则出现的为反面

}else{

f++;

}

}

System.out.println("正面出现次数"+z);

System.out.println("反面出现次数"+f);

以上回答你满意么?

JAVA编程:简单投硬币 急!!!

//投掷六枚硬币,第几次出现三正三反的情况?输出次数

public class Counter {

public static void main(String[] args) {

int count = 0;

boolean init = true;

while (init) {

count++;

//开始投币

Coin coin1 = new Coin();

Coin coin2 = new Coin();

Coin coin3 = new Coin();

Coin coin4 = new Coin();

Coin coin5 = new Coin();

Coin coin6 = new Coin();

//下面判断是否抛出三正三反

if ((coin1.flowCoin + coin2.flowCoin +coin3.flowCoin + coin4.flowCoin + coin5.flowCoin + coin6.flowCoin) == 3) {

System.out.println("三正三反投出: " + count);

init = false;

} else {

System.out.println("投币: " + count);

}

}

}

}

class Coin { //硬币

final int flowCoin;

public Coin() {

flowCoin = ((int)(Math.random()*100) + 1) % 2; //产生随机数,用0,1表正反

}

}

跪谢!求用JAVA编写一个自动贩卖机的程序

import java.util.Scanner;

public class Shop

{

private String coffee="咖啡";

private String coke="可乐";

private String tea="茶";

private double coffeeprice=2.00;

private double cokeprice=3.00;

private double teaprice=5;

private double price=0.0;

public Shop()

{

}

public void getName(int i)

{

if(i==1)

{

System.out.println("你选择的是咖啡");

}

if(i==2)

{

System.out.println("你选择的是可乐");

}

if(i==3)

{

System.out.println("你选择的是茶");

}

}

public double getShopping(int i,double p)

{

if(i==1)

{

if(pcoffeeprice)

{

System.out.println("您的钱不够买此商品");

}

else

{

price=p-coffeeprice;

}

}

if(i==2)

{

if(pcokeprice)

{

System.out.println("您的钱不够买此商品");

}

else

{

price=p-cokeprice;

}

}

if(i==3)

{

if(pteaprice)

{

System.out.println("您的钱不够买此商品");

}

else

{

price=p-teaprice;

}

}

return price;

}

public static void main(String[] args)

{

Shop shop=new Shop();

try

{

System.out.println("请选择你想要的商品\n 1.咖啡 2.可乐 3.茶");

Scanner si=new Scanner(System.in);

int i=si.nextInt();

System.out.println(" ");

if(i3||i1)

{

System.out.println("没有此产品");

}

else

{

shop.getName(i);

System.out.println(" ");

System.out.print("请付账:");

Scanner sp=new Scanner(System.in);

double p=sp.nextDouble();

System.out.println(" ");

if(shop.getShopping(i,p)!=0.0)

{

System.out.println("找你的钱:"+shop.getShopping(i,p));

}

}

}

catch(Exception ex)

{

System.out.println("请正确输入!");

}

}

}

用java模拟饮料自动售卖工作程序,如价格已知,投入高于它本事价格自动计算找零,低于会提示请继续投

下附代码,可以在键盘输入加入判断,比如是否输入的是数字而不是字母等等,是否输入的是正数而不是其他符号... 另外可以将下面代码优化,封装,在main方法只做调用即可

package test;

import java.util.Scanner;

public class test {

public static void main(String[] args) {

boolean flag = true;

while(flag){

System.out.println("欢迎使用自动饮料售卖机");

System.out.println("1.雪碧 3.00");

System.out.println("2.可乐 3.00");

System.out.println("3.特仑苏 5.00");

System.out.println("4.农夫山泉 2.00");

@SuppressWarnings("resource")

Scanner input = new Scanner(System.in);

System.out.print("请输入你的选择是:");

int choise = input.nextInt();

System.out.print("请投币:");

int money = input.nextInt();

money(money, choise);

System.out.print("是否继续'1.继续 0.退出' :");

int goon = input.nextInt();

if(goon==1){

flag = true;

}else{

flag = false;

}

}

}

public static void money(int money,int choise){

switch(choise){

case 1:

if(money-30){

System.out.println("找零"+(money-3)+"元");

}else if(money-30){

System.out.println("请再投入"+(3-money)+"元");

}else if(money-3==0){

System.out.println("生活愉快");

}else{

System.out.println("输入有误!");

}

break ;

case 2:

if(money-30){

System.out.println("找零"+(money-3)+"元");

}else if(money-30){

System.out.println("请再投入"+(3-money)+"元");

}else if(money-3==0){

System.out.println("生活愉快");

}else{

System.out.println("输入有误!");

}

break ;

case 3:

if(money-50){

System.out.println("找零"+(money-5)+"元");

}else if(money-50){

System.out.println("请再投入"+(5-money)+"元");

}else if(money-5==0){

System.out.println("生活愉快");

}else{

System.out.println("输入有误!");

}

break ;

case 4:

if(money-20){

System.out.println("找零"+(money-2)+"元");

}else if(money-20){

System.out.println("请再投入"+(2-money)+"元");

}else if(money-2==0){

System.out.println("生活愉快");

}else{

System.out.println("输入有误!");

}

break ;

}

}

}

JAVA 公共接口类 设计投币然后购买得到产品

//就写在一个文件里了

public class TestVendingMachine {

public static void main(String[] args) {

//新建一个售货机

VendingMachine vMachine = new VendingMachine();

System.out.println(vMachine.getProduct(5));

}

}

class VendingMachine {

private MapDouble, Product pros = new HashMap();

//初始化,有3种商品

public VendingMachine() {

Product sw = new SpringWater();

pros.put(sw.getPrice(), sw);

Product coffee = new Coffee();

pros.put(coffee.getPrice(), coffee);

Product coke = new Coke();

pros.put(coke.getPrice(), coke);

}

public Product getProduct(double price) {

if (pros.containsKey(price))

return pros.get(price);

else {

return null;

}

}

}

class Product {

private String name;

private double price;

public Product() {

}

protected Product(String name, double price) {

super();

this.name = name;

this.price = price;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return "Name:"+getName()+",price:"+getPrice();

}

}

class SpringWater extends Product {

public SpringWater() {

super("矿泉水", 2.0);

}

}

class Coffee extends Product {

public Coffee() {

super("咖啡", 10.0);

}

}

class Coke extends Product {

public Coke() {

super("可乐", 5.0);

}

}

4,(硬币正面和反面的概率)编写java程序,实现投币100次,计算正面与反面的概率

你好 我提供下思路吧

100次投币 一个for循环 i=0 ; i100;i++

每次循环产生一个随机数 1或者0

然后记录下 1和0出现的的次数

就好了

希望能帮到你 谢谢

关于java投币实验和JAVA实验三的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。