Java 将布尔结果转换为字符串值

Java 将布尔结果转换为字符串值,java,string,boolean,Java,String,Boolean,我需要创建一个投币java应用程序 好的,它必须有一个实例变量boolean。 当这是真的时,硬币的一面将是正面。 如果为false,则为tails 我该怎么做呢? 然后,头部=1,尾部为0 这样做的目的是能够计算出每一方的使用次数。 谢谢 好的,我想我最好把需求文档放在这里,这样大家都能理解。干杯 (a) 设计、编写和测试代表硬币的类,并使用抛硬币的方法 硬币有一个实例变量,指示结果是正面还是反面。这个实例变量应该是什么类型 硬币的构造器应该将硬币的正面初始化为正面。构造函数没有参数 硬币有两

我需要创建一个投币java应用程序

好的,它必须有一个实例变量boolean。 当这是真的时,硬币的一面将是正面。 如果为false,则为tails

我该怎么做呢? 然后,头部=1,尾部为0

这样做的目的是能够计算出每一方的使用次数。 谢谢

好的,我想我最好把需求文档放在这里,这样大家都能理解。干杯

(a) 设计、编写和测试代表硬币的类,并使用抛硬币的方法

硬币有一个实例变量,指示结果是正面还是反面。这个实例变量应该是什么类型

硬币的构造器应该将硬币的正面初始化为正面。构造函数没有参数

硬币有两种方法:

•返回投掷结果的方法(即返回指示正面或反面的实例变量)。 •抛硬币的方法

抛硬币的方法需要一个随机数,0或1

我们可以用数学课上的方法得到一个随机数。random()返回一个介于0和1之间的双精度值。要将此值转换为整数(0或1),请使用以下代码

int num=(int)(Math.random()*2)//返回一个整数

(b) 编写完coin类后,使用main方法编写一个测试类,该类将创建一个coin对象,并将其丢弃。每次抛出时,打印出结果(正面或反面)


(c) 现在改变抛硬币的主要方法100次,并计算抛硬币产生正面的次数和产生反面的次数。你需要一个循环来完成这个任务,迭代100次。显示头数和尾数。

您可以编写一个简单的应用程序,如

import java.util.Random;
//This is for flipping the "coin"
public class coins{
private static Random random=new Random();
//This is the coin
private int amountOfHead=0;
//This is the int for amount of heads flipped
private int amountOfTails=0;
//This is the int for the amount of tails flipped
private static int a1;
//This is also the coin
public void flip(){
a1=random.nextInt(1);
//This "flips the coin" making it a 1 or 0
if(a1==0){
  amountOfTails+=1;
  //If the "coin" is 0 tails increases by 1
 }else{
  amountOfHeads+=1;
  //If anything else happens(such as a 1) heads increases by 1
}
System.out.println(amountOfHeads+", "+amountOfTails)
//This prints the results out
}
}

要运行此程序,只需调用flip()

到目前为止你做了什么?请输入你的代码here@AntonH我只完成了一点,已经很困惑了。看起来你在寻找家庭作业的解决方案。考虑需要实现什么逻辑来满足需求。然后研究你需要如何编写代码。。。如果你不亲自尝试,你将无法学习。prof并不愚蠢,他们会在流行的网站上查找骗子。
import java.util.Random;
//This is for flipping the "coin"
public class coins{
private static Random random=new Random();
//This is the coin
private int amountOfHead=0;
//This is the int for amount of heads flipped
private int amountOfTails=0;
//This is the int for the amount of tails flipped
private static int a1;
//This is also the coin
public void flip(){
a1=random.nextInt(1);
//This "flips the coin" making it a 1 or 0
if(a1==0){
  amountOfTails+=1;
  //If the "coin" is 0 tails increases by 1
 }else{
  amountOfHeads+=1;
  //If anything else happens(such as a 1) heads increases by 1
}
System.out.println(amountOfHeads+", "+amountOfTails)
//This prints the results out
}
}