检查已存储在数组中的整数的整数变量

时间:2016-04-13 16:39:03

标签: java arrays

免责声明:代码仍然很新,并且只有java的基本技能。试着尽可能多地学习我自己和别人。目前不在大学学习。

大家好。

我正在尝试创建一个具有小容量(5个整数)的数组,以在每个数组元素中存储随机生成的整数。随机生成的整数处于设定范围(0-75),这没有问题。

我无法弄清楚怎么做是如何生成一个新的整数,然后在存储它并继续下一个数组元素之前,检查每个数组元素中当前存在的整数。

我试过的是:

public class ItemSet
{
public static void main(String []args)
 {
 int[] itemSet;
 itemSet = new int[5];

 itemSet[0] = 0; /* to initialise the array elements. Im not sure if i actually have to do this or not */
 itemSet[1] = 0;
 itemSet[2] = 0;
 itemSet[3] = 0;
 itemSet[4] = 0;

 int count = 1;
 int assignItem = 0;
 int countTwo = 1;
 while (count > 5) {  
   while (countTwo == 1) {  
     assignItem = (int)(Math.random()*76); 
  // here is where i get totally lost
     if (assignItem.compareTo(itemSet)) { 
       countTwo = 1;
     } else {
       itemSet[(count--)] = assignItem; 
       countTwo = 0; 
     }
     }
     count = count++;
    }
 }
}

我一直在看这个,所以我的脑袋开始受伤了。我很感激你能给我的任何建议。 :) 先感谢您。 XX

编辑:无法在任何&#34中找到我需要的解决方案;我如何测试数组是否包含某个值"键入问题,因为如果它确实与另一个先前生成的整数相同,我需要能够在将数字存储在数组元素之前再次使用数字随机化。

6 个答案:

答案 0 :(得分:0)

你只是填充数组做得太多了。要填充数组,请尝试使用“for”循环,如下所示:

Consumer

要打印存储在数组中的值,请尝试使用enhanced-for循环:

public class ItemSet {
    public static void main(String []args) {
        int[] itemSet;
        itemSet = new int[5];

        int count = 1;
        int assignItem = 0;
        int countTwo = 1;

        for (int i = 0; i < itemSet.length; i++) {
            itemSet[i] = (int) (Math.random() * 76);
        }
    }
}

要在存储下一个整数之前检查值(例如,确保新存储的值是唯一的),您可以使用嵌套的for循环,该循环从外部循环下方的值开始并向后移动,比较每个值之前是刚存储的值,如果它们相同,它将递减外部计数器,然后覆盖下一个循环的数据:

for (int element : itemSet) {
    System.out.println(element);
}

答案 1 :(得分:0)

<property name="birthdate" not-null="true" type="org.joda.time.LocalDate"/>

答案 2 :(得分:0)

有很多方法可以解决这个问题,我建议使用一种帮助方法来查找数组中的重复项。这将是一个有效的例子:

Task task = Task.Factory.StartNew(() =>
            {
              //put you code here the first one 
            }).ContinueWith((rs) =>{
              //Launch your event or anything you want
            });

答案 3 :(得分:0)

我很难理解你所要求的东西,但不管怎样,不管怎样,希望这是你想要的:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "9999997560"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:461)
    at java.lang.Integer.valueOf(Integer.java:554)
    at jsobcpuburnerpd3.Main.main(Main.java:22)
    Java Result: 1

检查生成的数字是否等于某个位置,如果位置为空(0),如果它不相等(一个新数字),那么它将为您的数组赋值。

答案 4 :(得分:0)

最直接的方法是在插入当前随机值之前检查数组中的现有值。

  1. 生成随机值
  2. 检查数组中是否存在值
  3. 如果已存在于数组中,则生成另一个值
  4. 如果数组中不存在,则将值设置为当前元素
  5. 代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    using System.Threading;
    
    namespace Desicions
    // Typo I know.
    {
        class Program
        {
        static void Main(string[] args)
          {
    
            bool WrongInput = true;
    
            while (WrongInput)
            {
                Console.Clear();
                Console.WriteLine("Would you perfer what's behind door 1, 2, or 3?");
                string userValue = Console.ReadLine();
                if (userValue == "1")
                {
                    Console.WriteLine("You won a new car!");
                    WrongInput = false;
                }
                else if (userValue == "2")
                {
                    Console.WriteLine("You won a boat!");
                    WrongInput = false;
                }
                else if (userValue == "3")
                {
                    Console.WriteLine("Aww, you did not win this time please play again.");
                    WrongInput = false;
                }
                else
                {
                    Console.WriteLine("Please choose door number 1, 2, or 3. Press Enter to return to game.");
                    Console.ReadLine();
                }
            }
            Thread.Sleep(2000);
            Console.WriteLine();
            Console.WriteLine("Would you like to play again? (Y/N)");
    
            while ((Console.ReadLine() == "Y") || (Console.ReadLine() == "y"))
                //Should I use this instead?
                //while String.Equals(Console.ReadLine(), "y", StringComparison.CurrentCultureIgnoreCase);
                //if so, why?
            {
                //What goes here for (Y)/(y) input to return into top of while (WrongInput) loop?
                Console.WriteLine("Currently in Dev [:");
            }
            while ((Console.ReadLine() == "N") || (Console.ReadLine() == "n"))
            {
                //I would like for a (N)/(n) input to WriteLine("Exiting game..."); Thread.Sleep(2000); then exit. Solutions plz por favor?
            }
    
    
            //BONUS c# <3 for code sample for setting doors numbers randomly!
    
            Console.ReadLine();
    
        }
    }
    

答案 5 :(得分:0)

如果你知道你的设定范围(0-75)并且空间不是问题,我建议维护一个未使用的整数池。这样可以保证每个值都是唯一的,并且可以避免迭代整数数组来检查重复值:

public static void main(String[] args) {

    List<Integer> unusedNumbers = new ArrayList();
    // Populate list with values
    for (int i = 0; i <= 75; i++) unusedNumbers.add(i);

    int[] itemSet = new int[5];

    for (int i = 0; i < 5; i++) {
        int randomIndex = (int) (Math.random() * unusedNumbers.size());
        int randomInt = unusedNumbers.remove(randomIndex);
        itemSet[i] = randomInt;
    }
}
相关问题