洗牌在java中两个数组 [英] shuffle two arrays in java

查看:121
本文介绍了洗牌在java中两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我在初学java。我已经定义了两个数组。的类型之一是串,另一种是整数。现在,我想洗牌them.Assume ID = {12,45,78,23}和名称= {数学,物理,艺术,电脑}。例如洗牌后的阵列将成为ID = {} 78,45,23,12和名称= {物理,艺术,数学,电脑}。我写了下面code不工作。我该如何解决?

 公共类RandomNumber {公共静态无效的主要(字串[] args)
{
    长[]号=新长[4];
    扫描仪输入=新的扫描仪(System.in);
    随机ID =新的随机(4);
    的String [] NAME =新的String [4];    的for(int i = 0; I< = numbers.length;我++)
    {
        System.out.print(输入数字:);
        号码[I] = input.nextLong();
    }
    的for(int i = 0; I< = numbers.length;我++)
    {
        INT randomPosition = id.nextInt(4);
        长TEMP =号[I]
        编号[i] = randomPosition;
        号码[randomPosition] =温度;
    }
    的for(int i = 0; I< name.length;我++)
    {
        的System.out.println(输入名称:);
        名称由[i] = input.nextLine();
    }
    的for(int i = 0; I< name.length;我++)
    {
        INT randomPosition = id.nextInt(4);
        字符串TEMP =名称[I]
        名称由[i] = randomPosition;
        名称[randomPosition] =温度;
    }
    的for(int i = 0; I< numbers.length;我++)
    {
        的System.out.println(1 +ID =+号[I] +和NAME =+名字[I]);
    }
}
}


解决方案

您应该做的:

  INT randomPosition = id.nextInt(4);
    长TEMP =号[I]
    编号[i] =号[randomPosition]
    号码[randomPosition] =温度;

您有3号线不同。

 号码[I] = randomPosition;

这同样适用于名称

您有几个其他错误了。

下面是您的code固定。结果
与你进行比较。结果
你会看到发生了什么变化。

 导入了java.util.Random;
进口java.util.Scanner中;公共类RandomNumber {    公共静态无效的主要(字串[] args){
        长[]号=新长[4];
        扫描仪输入=新的扫描仪(System.in);
        随机ID =新的随机(4);
        的String [] NAME =新的String [4];        System.out.print(输入数字:);        的for(int i = 0; I< numbers.length;我++){
            号码[I] = input.nextLong();
        }        的for(int i = 0; I< numbers.length;我++){
            INT randomPosition = id.nextInt(4);
            长TEMP =号[I]
            编号[i] =号[randomPosition]
            号码[randomPosition] =温度;
        }        的System.out.println(输入名称:);        的for(int i = 0; I< name.length;我++){
            名称由[i] = input.next();
        }        的for(int i = 0; I< name.length;我++){
            INT randomPosition = id.nextInt(4);
            字符串TEMP =名称[I]
            名称由[i] =名称[randomPosition]
            名称[randomPosition] =温度;
        }
        的for(int i = 0; I< numbers.length;我++){
            的System.out.println(1 +ID =+号[I] +和NAME =+名字[I]);
        }
    }
}

Sorry, I am beginner in java. I have defined two arrays. one of the types is string and the other is integer. now, i want to shuffle them.Assume id = {12, 45, 78, 23} and name = {"math", "physic", "art", "computer"}. for example after shuffling the arrays will become id = {78,45,23,12} and name = {"physic", "art", "math", "computer"}. i wrote the below code which does not work. how can i fix it?

public class RandomNumber {

public static void main(String[] args) 
{
    long[] numbers = new long[4];
    Scanner input = new Scanner(System.in);
    Random id = new Random(4);
    String[] name = new String[4];

    for (int i=0; i<=numbers.length; i++)
    {
        System.out.print("Enter the numbers: ");
        numbers[i] = input.nextLong();
    }
    for (int i=0; i<=numbers.length; i++)
    {
        int randomPosition = id.nextInt(4);
        long temp = numbers[i];
        numbers[i] = randomPosition;
        numbers[randomPosition] = temp;
    }
    for (int i=0; i<name.length; i++)
    {
        System.out.println("Enter the name: ");
        name [i] = input.nextLine();
    }
    for (int i=0; i<name.length; i++)
    {
        int randomPosition = id.nextInt(4);
        String temp = name[i];
        name[i] = randomPosition;
        name [randomPosition] = temp;
    }
    for (int i=0; i<numbers.length; i++)
    {
        System.out.println(i + " ID = " + numbers[i] + " and name = " + name[i]);
    }
}  
}

解决方案

You should do:

    int randomPosition = id.nextInt(4);
    long temp = numbers[i];
    numbers[i] = numbers[randomPosition];
    numbers[randomPosition] = temp;

You have the 3rd line different.

numbers[i] = randomPosition;

The same applies to names.

You had a few other bugs too.

Here is your code fixed.
Compare it with yours.
You will see what was changed.

import java.util.Random;
import java.util.Scanner;

public class RandomNumber {

    public static void main(String[] args) {
        long[] numbers = new long[4];
        Scanner input = new Scanner(System.in);
        Random id = new Random(4);
        String[] name = new String[4];

        System.out.print("Enter the numbers: ");

        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextLong();
        }

        for (int i = 0; i < numbers.length; i++) {
            int randomPosition = id.nextInt(4);
            long temp = numbers[i];
            numbers[i] = numbers[randomPosition];
            numbers[randomPosition] = temp;
        }

        System.out.println("Enter the names: ");

        for (int i = 0; i < name.length; i++) {
            name[i] = input.next();
        }

        for (int i = 0; i < name.length; i++) {
            int randomPosition = id.nextInt(4);
            String temp = name[i];
            name[i] = name[randomPosition];
            name[randomPosition] = temp;
        }
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(i + " ID = " + numbers[i] + " and name = " + name[i]);
        }
    }
}

这篇关于洗牌在java中两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆