RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java骰子代码,骰子代码的编程

Java程序求教,掷骰子

import java.util.Random;

创新互联成立以来不断整合自身及行业资源、不断突破观念以使企业策略得到完善和成熟,建立了一套“以技术为基点,以客户需求中心、市场为导向”的快速反应体系。对公司的主营项目,如中高端企业网站企划 / 设计、行业 / 企业门户设计推广、行业门户平台运营、手机APP定制开发移动网站建设、微信网站制作、软件开发、服务器托管等实行标准化操作,让客户可以直观的预知到从创新互联可以获得的服务效果。

public class ThrowDice {

public static int[] result = {1,2,3,4,5,6};

public static void doThrow() {

Random rand = new Random();

int i = rand.nextInt(6);

System.out.println("骰子的点数为--" + result[i]);

}

public static void main(String[] args) {

// 执行10次

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

doThrow();

}

}

}

完成一个java project的构建,创建一个“骰子”类,命名为Dice?

Dice代码如下:

import java.util.Random;

//(1)创建一个“骰子”类,命名为Dice。

public class Dice {

// 提示:初始化两个Dice对象。

//(2)“骰子”类有两个属性:①最大值为固定值6,②点数为1-6之间的整数。属性均设置为private。

private static int max = 6;

private int point;

// (3)“骰子”类有两个构造函数:①无形参的构造函数,将点数默认值设置为1;②有1个形参的构造函数,将形参赋值给点数。

public Dice() {

this.point = 1;

}

public Dice(int point) {

this.point = point;

}

// (4)自动生成骰子点数的get和set方法。

public static int getMax() {

return max;

}

public static void setMax(int max) {

Dice.max = max;

}

public int getPoint() {

return point;

}

public void setPoint(int point) {

this.point = point;

}

// (5)编写一个表示“掷骰子”的方法,将点数和函数返回值设置为1-6之间的随机整数。

// 提示:Math.random() //随机选取=0.0且1.0的double值

// double转int的方法:(int) double值 //转换后会舍弃小数点后面的值

public int throwDice() {

int result = 0;

while (true) {

int random = (int) (Math.random() * 10);

if (random 0 random = max) {

result = random;

break;

}

}

return result;

}

}

测试类方法如下:

import java.math.BigDecimal;

import java.util.ArrayList;

import java.util.List;

//(6)新建一个类,在其main()函数中调用Dice类,实现以下功能:

public class TestDice {

public static void main(String[] args) {

// ①掷两个骰子,显示每个骰子的点数,以及点数之和;

Dice dice = new Dice();

int one = dice.throwDice();

int two = dice.throwDice();

System.out.println("两次点数之和:" + (one + two));

// ②输入设置两个骰子的点数,显示两个骰子的点数之和。

Dice dice2 = new Dice(2);

Dice dice3 = new Dice(6);

System.out.println("所设置的点数之和:" + (dice2.getPoint() + dice3.getPoint()));

// ③连续10次掷两个骰子,显示每次掷骰子的结果,以及两个骰子10次点数的平均值。

Dice dice4 = new Dice();

ListInteger points1 = new ArrayList();

ListInteger points2 = new ArrayList();

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

int first = dice4.throwDice();

System.out.println("第一个骰子掷:" + (i + 1) + "次点数是:" + first);

int second = dice4.throwDice();

System.out.println("第二个骰子掷:" + (i + 1) + "次点数是:" + second);

points1.add(first);

points2.add(second);

}

long sum1 = points1.stream().reduce(Integer::sum).orElse(0);

System.out.println("第一个骰子10次点数的平均值:" + new BigDecimal(Long.valueOf(sum1).toString()).divide(new BigDecimal(Integer.valueOf(points1.size()).toString())));

long sum2 = points2.stream().reduce(Integer::sum).orElse(0);

System.out.println("第二个骰子10次点数的平均值:" + new BigDecimal(Long.valueOf(sum2).toString()).divide(new BigDecimal(Integer.valueOf(points2.size()).toString())));

}

}

java怎么写出当骰子点数为6时,在掷一遍的代码

加一个判断就好了,比如这样写

public void Dice(){

Random random = new Random();

int count = random.nextInt(6) + 1;//这里的骰子点数用随机数生成一个[1,6]之间的整数

//这里写你的代码逻辑

if(count == 6){

Dice();//再掷一次

}

//这里写你的代码逻辑

}

写一个java程序,摇两个骰子,用random,直到两个值相等为止

不知道你说的是random类还是math.random,所以写了两个

1. Math.random

public class Test1 {

public static void main(String[] args) {

int a, b;

a = (int)(1+Math.random()*(6));

b = (int)(1+Math.random()*(6));

while (a != b) {

System.out.println("Not equal! a=" + a + ", b=" + b);

a = (int)(1+Math.random()*(6));

b = (int)(1+Math.random()*(6));

}

System.out.println("Equal! a=b=" + a);

}

}

2. random类

import java.util.Random;

public class Test2 {

public static void main(String[] args) {

int a, b;

Random ra = new Random();

a = ra.nextInt(6)+1;

b = ra.nextInt(6)+1;

while (a != b) {

System.out.println("Not equal! a=" + a + ", b=" + b);

a = ra.nextInt(6)+1;

b = ra.nextInt(6)+1;

}

System.out.println("Equal! a=b=" + a);

}

}

java中编程实现如下的骰子游戏:丢下两个骰子,若分值的总值为7点,则“赢”;否则“输”。

public class Test {

public static void main(String[] args){

DieGame dieGame = new DieGame();

if (dieGame.play()) {

System.out.println("你赢了!");

} else {

System.out.println("你输了!");

}

}

}

class Die {

private int faceValue;

public int getFaceValue() {

return faceValue;

}

public void setFaceValue(int faceValue) {

this.faceValue = faceValue;

}

public void roll() {

this.faceValue = (int) (Math.random() * 6 + 1);

}

}

class DieGame {

private Die die1 = new Die();

private Die die2 = new Die();

public boolean play() {

die1.roll();

System.out.println("第一次点数:" + die1.getFaceValue());

die2.roll();

System.out.println("第二次点数:" + die2.getFaceValue());

if (die1.getFaceValue() + die2.getFaceValue() == 7) {

return true;

} else {

return false;

}

}

}

求一个简单又有趣的JAVA小游戏代码

具体如下:

连连看的小源码

package Lianliankan;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组

JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮

JLabel fractionLable=new JLabel("0"); //分数标签

JButton firstButton,secondButton; //

分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮

int grid[][] = new int[8][7];//储存游戏按钮位置

static boolean pressInformation=false; //判断是否有按钮被选中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标

int i,j,k,n;//消除方法控制

代码(code)是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系。

对于字符和Unicode数据的位模式的定义,此模式代表特定字母、数字或符号(例如 0x20 代表一个空格,而 0x74 代表字符“t”)。一些数据类型每个字符使用一个字节;每个字节可以具有 256 个不同的位模式中的一个模式。

在计算机中,字符由不同的位模式(ON 或 OFF)表示。每个字节有 8 位,这 8 位可以有 256 种不同的 ON 和 OFF 组合模式。对于使用 1 个字节存储每个字符的程序,通过给每个位模式指派字符可表示最多 256 个不同的字符。2 个字节有 16 位,这 16 位可以有 65,536 种唯一的 ON 和 OFF 组合模式。使用 2 个字节表示每个字符的程序可表示最多 65,536 个字符。

单字节代码页是字符定义,这些字符映射到每个字节可能有的 256 种位模式中的每一种。代码页定义大小写字符、数字、符号以及 !、@、#、% 等特殊字符的位模式。每种欧洲语言(如德语和西班牙语)都有各自的单字节代码页。

虽然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失。

如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符。而且,在代码页间不停地转换需要花费大量的处理时间。


网站标题:java骰子代码,骰子代码的编程
地址分享:http://scyingshan.cn/article/hccoie.html