java随机数扔色子_我如何问用户是否希望使用netbeans在Java中进行每个骰子掷骰后继续游戏...
小编典典
扫描仪可用于提示用户询问他/她是否要继续。
您甚至可以跟踪编号。掷骰子的次数。
import java.util.Scanner;
public class DiceGame {
public static int attempt = 1;
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int dice1 = (int) (Math.random() * 6 + 1);
int dice2 = (int) (Math.random() * 6 + 1);
int sum = dice1 + dice2;
while (true) {
System.out.println();
System.out.println("Rolling dice for " + attempt + " time!");
dice1 = (int) (Math.random() * 6 + 1);
dice2 = (int) (Math.random() * 6 + 1);
sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum == 2 || sum == 3 || sum == 12) {
System.out.println("Sorry with a " + sum + " you loose :(!");
System.out.println();
break;
} else if (sum == 7 || sum == 11) {
System.out.println("With a " + sum + " you win :)!");
System.out.println();
break;
}
System.out.println();
System.out.println("Do you wish to continue? Press 'y' for YES or ANY key for EXIT");
if (!scanner.next().equalsIgnoreCase("y")) {
break;
}
attempt++;
}
System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
}
}
EDIT: 如果要在总和为4 5 6 8 9 10时自动掷骰子,则不再需要Scanner,即用户输入是否继续。
这是相同的解决方案。
public class DiceGame {
public static int attempt = 1;
public static void main(String[] args) {
int dice1 = 0;
int dice2 = 0;
int sum = 0;
while (true) {
System.out.println();
System.out.println("Rolling dice for " + attempt + " time!");
dice1 = (int) (Math.random() * 6 + 1);
dice2 = (int) (Math.random() * 6 + 1);
sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum == 2 || sum == 3 || sum == 12) {
System.out.println("Sorry with a " + sum + " you loose :(!");
System.out.println();
break;
} else if (sum == 7 || sum == 11) {
System.out.println("With a " + sum + " you win :)!");
System.out.println();
break;
// this will roll the dices automatically
// when sum is 4, 5, 6, 8, 9 or 10
} else {
System.out.println();
System.out.println("With " + sum + " dices are rolled again automatically!!");
attempt++;
}
}
System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
}
}
样品运行
Rolling dice for 1 time!
Roll: total = 4
With a 4, dices are rolled again automatically!!
Rolling dice for 2 time!
Roll: total = 6
With a 6, dices are rolled again automatically!!
Rolling dice for 3 time!
Roll: total = 2
Sorry with a 2 you loose :(!
Thanks for playing dice game, you rolled the dice 3 times!
2020-11-26
java随机数扔色子_我如何问用户是否希望使用netbeans在Java中进行每个骰子掷骰后继续游戏...相关推荐
- java执行sql文件_面试官问你MyBatis SQL是如何执行的?把这篇文章甩给他
初识 MyBatis MyBatis 是第一个支持自定义 SQL.存储过程和高级映射的类持久框架.MyBatis 消除了大部分 JDBC 的样板代码.手动设置参数以及检索结果.MyBatis 能够支持 ...
- java反射 虚拟机优化_面试官问我:Java反射是什么?我回答竟然不上来......
每天凌晨00点00分,第一时间与你相约 每日英文 We all have moments of desperation. But if we can face them head on, that's ...
- Python后端转JAVA最快多久_【动力节点】老杜支招:Java小白学习入门攻略,涵盖学习路线...
动力节点在B站的直播相当好,相当受欢迎,教学总监亲自解惑.零基础学Java的同学确实把思路打开不少. 回顾:11月14日晚8:00,杜老师在动力节点B站大咖直播间,首次开播与粉丝面基,本场直播不吹,不 ...
- Java 随机数练习之从26个英文字母(含大小写)和 0 - 9 数字中随机抽出4个字符,然后拼接成一个随机验证码
从26个英文字母(含大小写)和 0 - 9 数字中随机抽出4个字符,然后拼接成一个随机验证码 判断输入的验证码 与 随机生成的验证码 是否相同(忽略大小写) 如果正确,则系统提示,"恭喜你验 ...
- java随机数排序算法_理解快速排序算法
快速排序在平均状况下,排序n个项目要Ο(n log n)次比较.在最坏状况下则需要Ο(n^2)次比较,但这种状况并不常见.事实上,快速排序通常明显比 其他Ο(n log n)算法更快,因为它的内部循环 ...
- java随机数带字母_随机生成一组随机数(带字母和数字)
随机生成一组随机数(带字母和数字) package com.qfx.util; import java.util.Random; public class CharacterUtils { // 得到 ...
- java随机数时间窗_蚁群算法(ACO)求解带时间窗的车辆路径(VRPTW)问题
今天为大家讲解使用蚁群算法(ACO)求解带时间窗的车辆路径(VRPTW)问题.在讲解蚁群算法求解VRPTW问题之前,不知道各位是否观察过现实生活中蚂蚁是怎么觅食的,说得形象一点的话就是成群的蚂蚁前赴后 ...
- java随机数去重排序_数组去重及排序/0~10随机数字/字符串中出现最多的字符及次数...
数组去重及排序: var arr = [1,5,1,2,6,8,1,81,9,0]; for(var i=0;i for(var j=i+1;j if(arr[i] == arr[j]){ arr.s ...
- 注会和java哪个更难_总有人问我:中级和注会哪个难?现在统一回复
原标题:总有人问我:中级和注会哪个难?现在统一回复 中级会计与注会既有联系又有区别,在就业方向上相辅相成,在学习的时候切记不要混淆方向,二者在内容上的考查有很大的不同! 一.注会的科目覆盖了中级的科目 ...
最新文章
- 基于inotify的文件监控方案
- rsync定时任务引起cpu负载高
- 推荐系统——GBDT+LR
- 日期getUTCSeconds()方法以及JavaScript中的示例
- Microsoft SignalR Map Controller具有页面可见性API和事件模糊功能,可映射文档管理系统(DMS)中的用户交互
- DecimalFormat的用法
- poj 3258 River Hopscotch 二分
- 表格是html文档的基本属性吗,Html
- springboot中application.yml 使用数组
- bio linux 创建_Linux设备驱动--块设备之概念和框架以及相关结构体
- mysql数据没有同步更新_MySQL数据库主从没有同步的两种解决方案
- f分布表完整图a=0.01_R语言具有Student-t分布改进的GARCH(1,1)模型的贝叶斯估计...
- 【JDK】win 10 / win 11:jdk 8 下载、安装、配置、验证
- ceph集群安装报错解决方法
- SpringMVC整合百度富文本编辑器
- 34.网络安全渗透测试—[信息收集篇3]—[whois查询和反查/IP查询和反查/C段查询/资产相关]
- 某音热门---图片转字符SpringBoot版
- 华为v2服务器系统安装系统,华为服务器RH 2288H v2安装系统
- 支持个性化定制的酷玩多功能桌面充电器上手体验
- Spring -- 迈向Spring之路
热门文章
- 美国漏洞众测平台HackerOne运营模式解读与分析
- SAP Fiori 学习(一) 环境搭建
- 西门子SMART控制三伺服程序
- 华为交换机默认密码大合集
- Python 之 处理 Excel 数据(5) —— 更新指定单元格的值
- 3D建模师会因为年龄大而失业吗?真实行业揭秘
- 寻找iPhone内存泄漏:Leaks工具指南
- 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Strange Optimization
- 怎样应对医疗电子设备的设计挑战?
- 客户被绑,蒙眼,惊问:“想干什么?” 对方不语,鞭笞之,客户求饶:“别打,要钱?” 又一鞭,“十万够不?” 又一鞭,“一百万?” 又一鞭。客户崩溃:“你们TMD到底要啥?” “要什么?......