Java硬币题?

Java硬币题?,第1张

//硬币

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.")

}

}

}

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

1、Java源程序是此弊由类定义组成的,每个程序中可以定义若干个类,但是只有一个类是主类。在Java Application中,这个主类是指包含___main____方法的类。

2、多态是指____一个程序中有多个同名的不同方法___,在Java中有两种多态,一种是使用方法的___覆盖___实现多态,另一种是使用方法的___重载___实现多态。

3、在Java程序中,通过类的定义只能实现___单__重继承,但通过接口的定义可以实现___多__重继承关系。

4、字符串分为两大类,一类是字符串常量,使用__String__类的对象表示;另罩族一类是字符串变量,使用___StringBuffer__类的对象表示。

5、在Java的基本数据类型中,char型采用Unicode编码方案,每个Unicode码占用____字节内存空间,这样,无论是中文字符还是英文字符,都是占

用____字节内存空间。

6、若x = 5,y = 10,则x <y和x >= y的逻辑值分别为__________和_______。

7、_______方法是一种仅有方法头,没有具体方法体和 *** 作实现的方法,该方法必须在抽象类之中定义。_______方法是不能被当前类的子类重新定义的方法。

8、创建一个名为 MyPackage 的包的语是___________________________,

该语句应该放在程序的位置为:_________________。

9、 创建一个森闷族标识有“嘉华职业培训学校”字样的标签对象gxdd的语句为____Label gxxd=new Label(“嘉华职业培训学校”)______。

10、以下代码段执行后的输出结果为__1____

int x=3; int y=10;System.out.println(y%x)

各位高手,小弟已经做了一部分了,还剩一部分不知道怎么做,请大家帮小弟做做,顺便也帮忙检查以下我做的对不对。谢谢啦,谢谢大家啦!

解析:

1:

2:

3:

4:

5:2个

6:true,false

7:

8:package

9:

10:1

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

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表正反

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12220405.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存