9

似乎没有简单的方法可以做到这一点,但这是我迄今为止所做的,如果有人可以纠正它以使其工作,那就太好了。在“newarray [e] = array [i].intValue();” 我收到一个错误“在“java.lang.Object”类型中找不到名为“intValue”的方法。” 帮助!

/*
Description: A game that displays digits 0-9 and asks the user for a number N.
 It then reverses the first N numbers of the sequence. It continues this until
 all of the numbers are in order.
 numbers

*/

import hsa.Console;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;


public class ReversalGame3test

{
    static Console c;

    public static void main (String[] args)
{
    c = new Console ();

    c.println ("3. REVERSAL GAME");
    c.println ("");
    c.println ("Displayed below are the digits 0-9 in random order. You must then enter a");
    c.println ("number N after which the computer will reverse the first N numbers in the");
    c.println ("sequence. The goal of this game is to sort all of the numbers in the fewest");
    c.println ("number of reversals.");
    c.println (""); //introduction

    List numbers = new ArrayList ();
    numbers.add ("0");
    numbers.add ("1");
    numbers.add ("2");
    numbers.add ("3");
    numbers.add ("4");
    numbers.add ("5");
    numbers.add ("6");
    numbers.add ("7");
    numbers.add ("8");
    numbers.add ("9");
    Collections.shuffle (numbers);
    Object[] array = numbers.toArray (new String [10]); // declares + shuffles numbers and converts them to array

    c.print ("Random Order: ");
    for (int i = 0 ; i < 10 ; i++)
    {
        c.print ((array [i]) + " ");
    }
    c.println ("");

    boolean check = false;
    boolean check2 = false;
    String NS;
    int N = 0;
    int count = 0;
    int e = -1;
    int[] newarray = new int [10];

    //INPUT
    do
    {
        c.print ("Enter a number: ");
        NS = c.readString ();
        count += 1;

        check = isInteger (NS);
        if (check == true)
        {
            N = Integer.parseInt (NS);
            if (N < 1 || N > 10)
            {
                check = false;
                c.println ("ERROR - INPUT NOT VALID");
                c.println ("");
            }
            else
            {
                c.print ("Next Order: ");
                for (int i = N - 1 ; i > -1 ; i--)
                {
                    e += 1;
                    newarray [e] = array [i].intValue ();
                    c.print ((newarray [e]) + " ");
                }
                for (int i = N ; i < 10 ; i++)
                {
                    e += 1;
                    newarray [e] = array [i].intValue ();
                    c.print ((newarray [e]) + " ");
                }
                check2 = sorted (newarray);
            } // rearranges numbers if valid
        } // checks if N is valid number
    }
    while (check == false);
} // main method


public static boolean isInteger (String input)
{
    try
    {
        Integer.parseInt (input);
        return true;
    }
    catch (NumberFormatException nfe)
    {
        return false;
    }
} //isInteger method


public static boolean sorted (int array[])
{
    boolean isSorted = false;

    for (int i = 0 ; i < 10 ; i++)
    {
        if (array [i] < array [i + 1])
        {
            isSorted = true;
        }
        else if (array [i] > array [i + 1])
        {
            isSorted = true;
        }
        else
            isSorted = false;

        if (isSorted != true)
            return isSorted;
    }
    return isSorted;
} // sorted method

}

4

5 回答 5

10

您可以使用Integer.valueOf

Integer.valueOf((String) array [i])

该类Integer有一个valueOf将字符串作为值并返回int值的方法,您可以使用它。NumberFormatException如果传递给它的字符串不是有效的整数值,它将抛出一个。

此外,如果您使用的是 java5 或更高版本,您可以尝试使用泛型来使代码更具可读性。

于 2013-01-16T03:20:15.410 回答
5

您可以使用Generics实现相同的功能,这会更容易。

List<Integer> numbers = new ArrayList<Integer> ();
Integer[] array = numbers.toArray (new Integer [10]);
于 2013-01-16T03:20:26.443 回答
3

试试 commons-lang

org.apache.commons.lang.ArrayUtils.toPrimitive(Integer[])
于 2013-01-16T04:02:50.317 回答
2

您不能调用.intValue()an Object,因为Object该类缺少 method intValue()

相反,您需要先将其Object转换为Integer类,如下所示:

newarray[e] = ((Integer)array[i]).intValue();

编辑:只是一个关于 StackOverflow 的有用提示 - 请将您的代码限制在相关的范围内!尽管有时需要大块的代码,但在这种情况下,它不是。它使问题看起来更好,并且一定会以这种方式得到更好的回答。

另外,请不要使用标签。它目前已被弃用,并且正在老化。

于 2013-01-16T03:19:56.487 回答
1

我知道这已经很晚了,但这是我的 2 美分!

int[] newArray=new int[objectArray.length];
Object[] objectArray = {1,2,3,4,5};

for(int i=0;i<objectArray.length();i++){
    b[i]=(int)objectArray[i];
}
于 2020-02-19T10:16:10.047 回答