因此我被指示编写一个生成数组的类(带返回),对生成的数组进行排序,反转排序的数组(带返回),然后检查已排序的以查看是否有任何2个相邻的#是相同(如果有则返回True,否则返回False)。
到目前为止,我已经设法写出了所有内容,但是我的最后一组说明是用print语句填充main语句,这些语句调用我编写的低级方法以按照写入的顺序显示。
我设法得到原始的随机生成的数组被调用和打印,但是我没有运气就像我第一次调用任何其他方法一样,并尝试了我能想到的所有方法来获取它们用所提供的“这就是等等等等等”来打印他们的结果。
如果有人能够指出我正确的方向,只要能够调用其他方法来打印他们的结果,那么每次打印都会包含一个基本语句,例如“随机数组是:”,“排序数组”是:“,”反转数组是:“,”它是(TRUE / FALSE)这个数组有相邻的重复。“我非常感激。在这一点上我没有尝试过任何工作,而且我已经完全停顿了。我一直在努力工作熟悉java的朋友,但我们似乎也陷入困境。这是我到目前为止所写的......
import java.util.*;
public class SortedSequence
{
public static void main(String[] args)
{
int[] randomNumbers = new int[20];
randomNumbers = generateRandom(20);
printArray(randomNumbers);
// This is where Im getting stuck at. Everything I've tried makes a compile error
}
public static int[] generateRandom(int n)
{
int[] genNumbers = new int[n];
Random rand = new Random();
for (int i = 0; i < genNumbers.length; i++)
{
int bubble = rand.nextInt(100);
genNumbers[i] = bubble;
}
return genNumbers;
}
public static void sortArray(int[] genNumbers)
{
Arrays.sort(genNumbers);
}
public static int[] reverse(int[] x)
{
int[] sortArray = new int[x.length];
for (int i = 0; i < x.length; i++) {
sortArray[i] = x[x.length - 1 -i];
}
return sortArray;
}
public static boolean adjacentDuplicates(int[] boo)
{
boolean duplicates = false;
for (int i = 0; !duplicates && i < boo.length-1; i++)
if (boo[i] == boo[i+1]);
{
duplicates = true;
}
return duplicates;
}
public static void printArray(int[] print)
{
for (int i = 0; i < print.length; i++) {
System.out.print(print[i] + " "); }
System.out.println();
}
}
答案 0 :(得分:1)
我编译了代码,但我没有收到任何错误。
我在主类中添加了printArray(reverse(randomNumbers));
,如下所示:
public static void main(String[] args)
{
int[] randomNumbers = new int[20];
randomNumbers = generateRandom(20);
printArray(randomNumbers);
printArray(reverse(randomNumbers));
// This is where Im getting stuck at. Everything I've tried makes a compile error
}
并在运行程序时返回:
28 87 13 22 85 0 60 59 90 30 52 15 32 72 9 76 83 89 36 39
39 36 89 83 76 9 72 32 15 52 30 90 59 60 0 85 22 13 87 28
看起来这是编译器的问题,或者你是如何调用该类的。
以下是排序类所需的内容。 Arrays.sort()方法接受数组并在其中手动移动它们。 :
public static int[] sortArray(int[] genNumbers)
{
Arrays.sort(genNumbers);
return genNumbers;
}
此外,您将其返回设置为void,而不是int []。
老实说,我不知道邻近的冥王星到底发生了什么。看起来if和for方法什么也没做,因为你在;
之后直接放了一个)
。
您的代码需要介于)
和;
之间,或者您需要在for循环声明后使用{}
(如下所示)。
另外,使用那些下划线括号对于可读性来说很糟糕,我建议不要这样做。以下是adjacentDuplicates方法的样子。
public static boolean adjacentDuplicates(int[] boo) {
boolean duplicates = false;
for (int i = 0; !duplicates && i < boo.length-1; i++) {
if (boo[i] == boo[i+1]) duplicates = true;
}
return duplicates;
}
现在,我们将如何打印所有内容并获取可读信息:
public static void main(String[] args)
{
int[] randomNumbers = new int[20];
randomNumbers = generateRandom(20);
System.out.print("Array: ");
printArray(randomNumbers);
System.out.print("Reversed: ");
printArray(reverse(randomNumbers));
System.out.print("Sorted: ");
printArray(sortArray(randomNumbers));
System.out.print("Adjacent Duplicates?: ");
System.out.println(adjacentDuplicates(randomNumbers));
// If we sort out the numbers, then any duplicates will become adjacent to each other
System.out.print("Duplicates at all?: ");
System.out.println(adjacentDuplicates(sortArray(randomNumbers)));
// This is where Im getting stuck at. Everything I've tried makes a compile error
}
试运行:
Array: 92 18 5 16 68 10 85 58 50 56 91 48 45 28 63 98 94 15 93 64
Reversed: 64 93 15 94 98 63 28 45 48 91 56 50 58 85 10 68 16 5 18 92
Sorted: 5 10 15 16 18 28 45 48 50 56 58 63 64 68 85 91 92 93 94 98
Adjacent Duplicates?: false
Duplicates at all?: false
Here's the full class.再次在底部列出。
您希望它的格式不同。我会让你享受这样做的乐趣,但请注意:
System.out.print("bla")
将打印此内容而不是在最后添加换行符
System.out.println("blap");
将打印字符串并添加换行符。
所以
System.out.print("bla");
System.out.println("blap");
System.out.print("blo");
就像跑步一样
System.out.print("bla blap\n blo")
两者都会打印出来:
bla blap
blo
答案 1 :(得分:0)
所以我得到了3个int数组来打印。
不确定如何从主...
获取布尔值T或F.我也不确定如何让初始随机数组与我在数组之前需要提供的txt相同的行上进行打印,但确实如此,特别是在所有其他代码相同但不做同样的情况下事情......
有关如何使阵列与描述性文本在同一行上打印的建议?我的布尔情况怎么样?
/**
* This class generates a random array of 20 int long, sorts
* the array, reverses the array, check to see if there are
* any duplicate adjacent numbers, prints true if there are or
* false if there aren't, and then prints the results.
*
*@author Matthew Jackson
*@version 9/5/2017
*
*/
import java.util.*;
public class SortedSequence
{
public static void main(String[] args)
{
System.out.print("The Random array is: ");
int[] randomNumbers = new int[20];
randomNumbers = generateRandom(20);
printArray(randomNumbers);
System.out.println();
System.out.println("The Sorted array is: ");
sortArray(randomNumbers);
printArray(randomNumbers);
System.out.println();
System.out.println("The Reverse array is: ");
printArray(reverse(randomNumbers));
System.out.println();
System.out.println("It is" + "this array has adjacent duplicates");
if(adjacentDuplicates(randomNumbers))
{
// Put something here to print the boolean T/F?
}
}
/**
* Generate an array of type int and size n that returns
* this array from the method
*
*@param n Generate random number array
*@return Return array list that was generated
*/
public static int[] generateRandom(int n)
{
int[] genNumbers = new int[n];
Random rand = new Random();
for (int i = 0; i < genNumbers.length; i++) {
int bubble = rand.nextInt(100);
genNumbers[i] = bubble; }
return genNumbers;
}
/**
*Sort array of randomly generated numbers
*
*@param genNumbers Calls method of randomly generated number array
*/
public static void sortArray(int[] genNumbers)
{
Arrays.sort(genNumbers);
}
/**
*Reverse array of sorted array numbers
*
*@param Reverses list of sorted array numbers.
*@return Return array in reverse original order
*/
public static int[] reverse(int[] x)
{
int[] sortArray = new int[x.length];
for (int i = 0; i < x.length; i++) {
sortArray[i] = x[x.length - 1 -i];
}
return sortArray;
}
/**
*Check array list to see if there are any duplicates
*adjacent to each other
*
*@param duplicates True if adjacent numbers are same,
* false if not.
*@return Returns True/False if there are adjacent duplicates or not
*/
public static boolean adjacentDuplicates(int[] boo)
{
boolean duplicates = false;
for (int i = 0; !duplicates && i < boo.length-1; i++)
if (boo[i] == boo[i+1]);
{ //else infront of the {?
duplicates = true;
}
return duplicates;
}
/**
*Prints given array
*
*@param print Prints any method called to it
*/
public static void printArray(int[] print)
{
for (int i = 0; i < print.length; i++) {
System.out.print(print[i] + " "); }
System.out.println();
}
}