Java 关于日期和随机数程序的问题
我正在尝试创建一个生成随机数的程序。当生成的随机数与当前日期匹配时,它会打印一个计数器,显示尝试次数。日期格式为ddMMyyyy,生成的随机数为8位。最后,它将日期和计数打印五次。这是我到目前为止得到的,但它从未返回输出Java 关于日期和随机数程序的问题,java,date,random,counter,java-time,Java,Date,Random,Counter,Java Time,我正在尝试创建一个生成随机数的程序。当生成的随机数与当前日期匹配时,它会打印一个计数器,显示尝试次数。日期格式为ddMMyyyy,生成的随机数为8位。最后,它将日期和计数打印五次。这是我到目前为止得到的,但它从未返回输出 import java.util.Random; import java.util.Date; import java.text.*; public class Program1 { public static void main(String[] args) {
import java.util.Random;
import java.util.Date;
import java.text.*;
public class Program1 {
public static void main(String[] args) {
Date date = new Date();
Random value = new Random();
int x = value.nextInt();
int counter = 0;
do {
counter++;
} while(!date.equals(value));
if (date.equals(value)){
for(int i = 1; i < 6; i++) {
SimpleDateFormat sdf = new SimpleDateFormat ("ddMMyyyy");
System.out.println("The date is " + sdf.format(date) + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
}
}
}
import java.util.Random;
导入java.util.Date;
导入java.text.*;
公共课程1{
公共静态void main(字符串[]args){
日期=新日期();
随机值=新的随机值();
int x=value.nextInt();
int计数器=0;
做{
计数器++;
}而(!date.equals(value));
如果(日期等于(值)){
对于(int i=1;i<6;i++){
SimpleDataFormat sdf=新的SimpleDataFormat(“ddMMyyyy”);
System.out.println(“日期为“+sdf.format(date)+”!”;
System.out.println(“计数为:“+计数器”);
System.out.println();
}
}
}
}
获取一个从历元起0到毫秒数范围内的long
数字,并应用格式化程序以获得所需格式的日期字符串。然后将其与今天的日期字符串进行比较
演示:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String strToday = sdf.format(now);
int counter = 0;
while (true) {
// Instantiate Date with minutes of 8 digits
Date date = new Date(TimeUnit.MILLISECONDS
.convert(ThreadLocalRandom.current().nextLong(10000000, 100000000), TimeUnit.MINUTES));
String strDate = sdf.format(date);
if (Objects.equals(strToday, strDate)) {
for (int i = 1; i < 6; i++) {
System.out.println("The date is " + strDate + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
break;
} else {
counter++;
}
}
}
}
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
// Change the ZoneId as applicable e.g. ZoneId.of("Europe/London")
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime now = ZonedDateTime.now(zoneId);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMuuuu", Locale.ENGLISH);
String strToday = dtf.format(now);
int counter = 0;
while (true) {
// Instantiate Date with minutes of 8 digits
LocalDate date = Instant
.ofEpochMilli(TimeUnit.MILLISECONDS
.convert(ThreadLocalRandom.current().nextLong(10000000, 100000000), TimeUnit.MINUTES))
.atZone(zoneId).toLocalDate();
String strDate = dtf.format(date);
if (Objects.equals(strToday, strDate)) {
for (int i = 1; i < 6; i++) {
System.out.println("The date is " + strDate + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
break;
} else {
counter++;
}
}
}
}
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
请注意,java.util
date-time API及其格式化API,SimpleDataFormat
已经过时且容易出错。建议完全停止使用,并切换到*
使用现代日期时间API:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String strToday = sdf.format(now);
int counter = 0;
while (true) {
// Instantiate Date with minutes of 8 digits
Date date = new Date(TimeUnit.MILLISECONDS
.convert(ThreadLocalRandom.current().nextLong(10000000, 100000000), TimeUnit.MINUTES));
String strDate = sdf.format(date);
if (Objects.equals(strToday, strDate)) {
for (int i = 1; i < 6; i++) {
System.out.println("The date is " + strDate + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
break;
} else {
counter++;
}
}
}
}
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
// Change the ZoneId as applicable e.g. ZoneId.of("Europe/London")
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime now = ZonedDateTime.now(zoneId);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMuuuu", Locale.ENGLISH);
String strToday = dtf.format(now);
int counter = 0;
while (true) {
// Instantiate Date with minutes of 8 digits
LocalDate date = Instant
.ofEpochMilli(TimeUnit.MILLISECONDS
.convert(ThreadLocalRandom.current().nextLong(10000000, 100000000), TimeUnit.MINUTES))
.atZone(zoneId).toLocalDate();
String strDate = dtf.format(date);
if (Objects.equals(strToday, strDate)) {
for (int i = 1; i < 6; i++) {
System.out.println("The date is " + strDate + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
break;
} else {
counter++;
}
}
}
}
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
*无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。
除了使用过时且臭名昭著的类
Date
和SimpleDateFormat
之外,您的程序中还有一些错误
do
-循环中,您既没有修改日期
也没有修改值
。因此,如果它们在循环之前不相等,它们将永远不会相等,并且循环将无限期地运行
日期
和值
永远不能相等date
是一个java.util.date
,而值
是一个随机的。日期
不能等于随机
。Java的一个令人困惑的特性是,它在语法上允许比较它们。一个好的IDE或者像SpotBugs这样的代码分析工具应该警告你,它不可能做你想做的事情
int
overflow:value.nextInt()
可以从Integer.MIN\u value
到Integer.MAX\u value
生成超过40亿个可能的值。因此,在随机数生成器达到正确的值之前需要一段时间。因此,您的计数有时会溢出int
所能容纳的值,最高可达Integer.MAX\u值,或略高于20亿
if
语句再次测试它。意思是:if
语句是多余的。你可能会说它也没有害处,但它确实有害处:它使读者感到困惑Arvind Kumar Avinash在回答的后半部分给出了避免日期和简化格式的解决方案。1,2的解。四,。在其他答案的代码中也有。解决方案3。将使用一个
long
作为计数器。随机值=新值()代码>这不是有效的Java。发布代码时,请使用IDE中的复制/粘贴,不要重新键入代码。另外,请参加,访问并阅读以了解如何有效使用此网站。问题是您的do…while
循环不会修改值的值。它在循环之前设置,在循环内部不会更改。循环所做的唯一一件事就是增加计数器。顺便说一下,可怕的Date
&SimpleDateFormat
类在几年前被JSR 310中定义的现代java.time类所取代。我建议您不要使用SimpleDateFormat
和Date
。这些类设计得很糟糕,而且早已过时,其中前者尤其令人讨厌。相反,请使用来自billy parker的LocalDate
和DateTimeFormatter
。billy parker,请不要故意破坏您自己的问题,为他人创造更多的工作,这样答案就没有意义了。如果你想问一个新问题,请改为问一个新问题。@billyparker-为什么要在程序中添加毫秒?-java.util.Date
表示自称为“历元”的标准基准时间(即1970年1月1日00:00:00 GMT
(或UTC))以来的毫秒数。.nextLong(10000000,毫秒)
部件生成long
值。在10000000
和millis
@billyparker之间-是否有方法不使用时间值而仅使用日期值-此解决方案仅基于日期,但为了从毫秒推导日期,您需要首先获取日期时间对象,从中可以提取日期。@billyparker-这也会生成8位数的数字吗?-是的,我错过了这部分。我刚刚更新了我的答案以满足此要求。如果在每月的前9天运行,则没有真正的8位数字将等于日期。