小编TLM*_*TLM的帖子

Java:获取当前星期几的价值

目前,我正在创建一个java计划应用程序,我想知道如何在使用Calendar类获取一周的第一天时找到一周中的当前日期.

这是我在课堂上使用的方法,用于设置星期计划

public static String getSunday() {
    SimpleDateFormat d = new SimpleDateFormat(dayDate);
    Calendar specific = cal;
    specific.add(Calendar.DAY_OF_YEAR, (cal.getFirstDayOfWeek() - ??));
    return d.format(specific.getTime());
}
Run Code Online (Sandbox Code Playgroud)

java calendar weekday

5
推荐指数
1
解决办法
6058
查看次数

C++,随机数,范围为1-6

我想知道使用rand()方法得到1-6范围的随机数的方法是什么.这是为了模拟我需要的骰子卷来找到3个骰子卷的平均值,因此类型将是双倍的.

c++ random

5
推荐指数
1
解决办法
2万
查看次数

Java:Integer obj无法强制转换为Comparable

我试图从驱动程序类传递一个Integer对象作为我创建的SortedArray Generic类的函数的参数时遇到问题.从我的驱动程序类,我将用户的int输入转换为Integer对象,以转换为我的SortedArray类的Comparable.

我继续收到错误:"线程中的异常"主"java.lang.ClassCastException:java.lang.Integer无法强制转换为Comparable".我查看了一些同学的源代码,但发现参数/参数设置没什么差别,但是他们的代码工作得很好.我一直在寻找几个小时试图找到我所犯的错误,但我仍然无法找到为什么我的Integer对象无法转换为Comparable.

这里有一点来自我的SortedArray类

public class SortedArray implements Comparable{
    public int size;
public int increment;
public int top;
    Comparable[] a = new Comparable [size];

public SortedArray(int initialSize, int incrementAmount)
{
        top = -1;
        size = initialSize;
        increment = incrementAmount;

}
public int appropriatePosition(Comparable value)
{
        int hold = 0;
        if(top == -1)
        {
            return 0;
        }
        else
        {    
            for(int i = 0; i <= top; i++)
            {
                if(a[i].compareTo(value) > 0)
                {
                    hold = i;
                    break;
                }
            }
        }
        return hold; …
Run Code Online (Sandbox Code Playgroud)

java generic-programming comparable

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×2

c++ ×1

calendar ×1

comparable ×1

generic-programming ×1

random ×1

weekday ×1