GVKun编程网logo

为Random.nextInt()指定最大值和最小值?(random.nextint()取值范围)

5

在本文中,我们将给您介绍关于为Random.nextInt的详细内容,并且为您解答指定最大值和最小值?的相关问题,此外,我们还将为您提供关于android–如何在SeekBar上显示最大值和最小值?、

在本文中,我们将给您介绍关于为Random.nextInt的详细内容,并且为您解答指定最大值和最小值?的相关问题,此外,我们还将为您提供关于android – 如何在SeekBar上显示最大值和最小值?、android – 如何设置X轴最小值和最大值、C ++如何在n个元素的一维数组中查找最小值和最大值?、C#基础算法题 找出最大值和最小值的知识。

本文目录一览:

为Random.nextInt()指定最大值和最小值?(random.nextint()取值范围)

为Random.nextInt()指定最大值和最小值?(random.nextint()取值范围)

我想在逻辑范围内生成一个随机整数。因此,举例来说,我正在编写一个程序来“掷掷”具有指定边数的骰子。

public int rollDice(){     随机生成器= new Random();     返回generator.nextInt(sides);}

现在的问题是,它将返回边与零之间的值, 包括
0和0,这是没有意义的,因为大多数骰子从1到6、9等。因此,我如何指定nextInt应该在1和边数之间起作用?

答案1

小编典典

要在 fromto (包括)之间生成一个随机的int值(均匀分布),请使用:

from + rndGenerator.nextInt(to - from + 1)

以您的情况(1 ..面):

1 + rndGenerator.nextInt(sides)

android – 如何在SeekBar上显示最大值和最小值?

android – 如何在SeekBar上显示最大值和最小值?

我正在尝试做什么:

>我想在Android应用程序中实现SeekBar,在SeekBar上我还要显示最大值和最小值.最小值始终为0,但最大值取决于剪辑长度. (例如:0 — 180)
>有没有办法显示用户移动SeekBar时选择的值(在搜索栏本身上)?

我无法弄清楚,如何与android SeekBar一起实现这一点,因为似乎没有任何可用于显示值的选项.

解决方法:

I would like to implement a seekbar in an Android App. But on the
seekbar I would also like to display the max and min values. Min value
would always be 0 but max value is dependent on clip length. (Example:
0—180)

Is there a way to display the value (on the seek bar itself) that is selected when the user > moves the seekbar?

如果您希望这些值位于SeekBar之上,则扩展SeekBar类并覆盖onDraw方法.在调用super.onDraw(canvas)之后,您可以绘制自己的东西,例如SeekBar开始和结束时的最小/最大值或当前进度.制作看起来不错的东西(在所有看起来不同的Seekbars上)将会有点困难,因为您需要仔细计算绘制文本的位置和方式.

一个更简单的方法是使一个自定义组件包含SeekBar,左侧和右侧有一个TextView(下面有一个可选的TextView用于当前进度)并设置那些具有最小/最大值的组件(即使以编程方式设置最大值) ,最大的TextView可以“跟随”这些变化).知道SeekBar的宽度和当前进度,可以轻松计算和更新进度.

第二种情况的一个小例子:

public static class SeekBarWithValues extends RelativeLayout {

    private int mMax = 100;
    private TextView mMinText;
    private TextView mMaxText;
    private TextView mCurrentText;
    private SeekBar mSeek;

    public SeekBarWithValues(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(getContext()).inflate(
                R.layout.content, this);
        // the minimum value is always 0
        mMinText = (TextView) findViewById(R.id.minValue);
        mMinText.setText("0");
        mMaxText = (TextView) findViewById(R.id.maxValue);
        mCurrentText = (TextView) findViewById(R.id.curentValue);
        mSeek = (SeekBar) findViewById(R.id.seekBar);
        mSeek.setMax(100);
        mMaxText.setText(String.valueOf(mSeek.getMax()));
    }

    /**
     * This needs additional work to make the current progress text stay
     * right under the thumb drawable.
     * 
     * @param newProgress
     *            the new progress for which to place the text
     */
    public void updateCurrentText(int newProgress) {
        mCurrentText.setText(String.valueOf(newProgress));
        final int padding = mMinText.getWidth() + mSeek.getPaddingLeft();
        final int totalSeekWidth = mSeek.getWidth();
        final RelativeLayout.LayoutParams lp = (LayoutParams) mCurrentText
                .getLayoutParams();
        final int seekLocation = (mSeek.getProgress() * totalSeekWidth)
                / mMax - mCurrentText.getWidth() / 2;
        lp.leftMargin = seekLocation + padding;
        mCurrentText.setLayoutParams(lp);
    }

    public SeekBar getSeekBar() {
        return mSeek;
    }

    public void updateSeekMaxValue(int newValue) {
        mMax = newValue;
        mMaxText.setText(mMax);
        mSeek.setMax(mMax);
    }

}

内容布局在哪里:

<merge xmlns:android="http://schemas.android.com/apk/res/android" >

<TextView
    android:id="@+id/minValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/seekBar"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@id/seekBar"
    android:gravity="center" />

<TextView
    android:id="@+id/maxValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/seekBar"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/seekBar"
    android:gravity="center" />

<SeekBar
    android:id="@id/seekBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/maxValue"
    android:layout_toRightOf="@id/minValue" />

<TextView
    android:id="@+id/curentValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/seekBar"
    android:gravity="center" />

目前的文本往往比它应该更加令人满意,并且需要额外的工作才能将其置于寻求句柄之下.

android – 如何设置X轴最小值和最大值

android – 如何设置X轴最小值和最大值

我正在尝试设置X轴最小值和最大值.我找到了方法mRenderer.setXAxisMax(value);
            mRenderer.setXAxisMin(值);但我不知道在TimeSeries中如何设置X.

当我使用时间戳mRenderer.setXAxisMin(1347963701812)时,它表示int超出范围

当我使用字符串mRenderer.setXAxisMin(“2012-09-20 15:00:00”)时,它表示不适用于String.

如何在TimeSeries上设置最小和最大X日期?

解决方法

在你的问题中,值得一提的是,当你说“它”时,“它”就是 Java编译器.

只是让编译器明白你传递的是一个不适合int的double值:

mRenderer.setXAxisMin(1347963701812.0);

C ++如何在n个元素的一维数组中查找最小值和最大值?

C ++如何在n个元素的一维数组中查找最小值和最大值?

您错过了算法中的第一个值永远不会设置为最小值或最大值。 假设所有数字都是正数

int main()
{
    int n;
    cin>>n;
    int ma=0,mi=2147483647;
    int num[n];
    for(int i=0;i<n;i++)
        {
            cin>>num[i];
            if(num[i] > ma)
                ma = num[i];
            if(num[i] < mi)
                mi = num[i];
        }


    cout<<mi<<endl;
    cout<<ma;
}
,

我会尝试以下代码:

std ::vector < int > v (n);
std ::copy_n (std ::istream_iterator < int,char > (cin),begin (v),n);
auto const r ((std ::minmax_element) (begin (v),end (v)));
return std ::cout << * r .first << '\n' << * r .second ? EXIT_SUCCESS: EXIT_FAILURE;

C#基础算法题 找出最大值和最小值

C#基础算法题 找出最大值和最小值

找出最大值和最小值

题目要求

输入n个数,n<=100,找到其中最小的数和最大的数

实现代码

using System;

namespace _1.求最大最小
{
    class Program
    {
        public static int GetMax(int[] numbers)
        {
            int max = numbers[0];
            for (int i = 0; i < numbers.Length; i++)
            {
                if (max < numbers[i])
                {
                    max = numbers[i];
                }
            }
            return max;
        }
        public static int GetMin(int[] numbers)
        {
            int min = numbers[0];
            for (int i = 0; i < numbers.Length; i++)
            {
                if (min > numbers[i])
                {
                    min = numbers[i];
                }
            }
            return min;
        }
        static void Main(string[] args)
        {
            string[] temp = Console.ReadLine().Split('' '');
            int[] numbers = new int[temp.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                numbers[i] = Convert.ToInt32(temp[i]);
            }

            Console.WriteLine("Max = " + GetMax(numbers));
            Console.WriteLine("Min = " + GetMin(numbers));
            Console.ReadKey();
        }
    }
}

今天关于为Random.nextInt指定最大值和最小值?的分享就到这里,希望大家有所收获,若想了解更多关于android – 如何在SeekBar上显示最大值和最小值?、android – 如何设置X轴最小值和最大值、C ++如何在n个元素的一维数组中查找最小值和最大值?、C#基础算法题 找出最大值和最小值等相关知识,可以在本站进行查询。

本文标签: