从主方法打印方法不起作用-Java
因此,我被指示编写一个类,生成一个数组(返回),对生成的数组进行排序,反转排序后的数组(返回),然后检查排序后的数组是否有任何两个相邻的#相同(如果有,返回True,如果没有,返回False) 我已经设法写出了我所能说的一切,但是我的最后一组指令是用print语句填充main,这些语句调用我编写的较低的方法,并按编写的顺序显示 我成功地调用并打印了原始随机生成的数组,但是我没有像第一个那样调用任何其他方法,我尝试了我能想到的一切,让他们只打印结果,并提供了“这是废话废话: 如果有人能给我指出正确的方向,让我能够调用其他方法来打印它们的结果,并在每个打印输出中包含一条基本语句,如“随机数组为:”、“排序数组为:”、“反向数组为:”、“是(真/假)”,该数组具有相邻的重复项。”我将非常感激。在这一点上,我所尝试的一切都不起作用,我完全处于停滞状态。我一直试图和熟悉java的朋友们一起工作,但我们似乎也陷入了困境。这是我到目前为止写的从主方法打印方法不起作用-Java,java,methods,printing,method-call,Java,Methods,Printing,Method Call,因此,我被指示编写一个类,生成一个数组(返回),对生成的数组进行排序,反转排序后的数组(返回),然后检查排序后的数组是否有任何两个相邻的#相同(如果有,返回True,如果没有,返回False) 我已经设法写出了我所能说的一切,但是我的最后一组指令是用print语句填充main,这些语句调用我编写的较低的方法,并按编写的顺序显示 我成功地调用并打印了原始随机生成的数组,但是我没有像第一个那样调用任何其他方法,我尝试了我能想到的一切,让他们只打印结果,并提供了“这是废话废话: 如果有人能给我指出正确
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();
}
}
import java.util.*;
公共类排序序列
{
公共静态void main(字符串[]args)
{
int[]随机数=新的int[20];
随机数=生成数(20);
打印阵列(随机数);
//这就是我陷入困境的地方。我所尝试的一切都会导致编译错误
}
公共静态int[]生成器域(int n)
{
int[]genNumbers=新的int[n];
Random rand=新的Random();
for(int i=0;i
我编译了代码,没有任何错误
我添加了一个printary(反向(随机数))代码>到主类,如下所示:
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
这似乎是编译器的问题,或者是如何调用该类的问题
对后续问题的回答。
这是你所需要的分类类。方法获取数组并在其中手动移动它们:
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;
}
试运行:
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
再次列在底部
新行素材
看起来您希望它的格式不同。我会让您享受这样做的乐趣,但请注意:
System.out.print("bla")
将打印此内容,但不会在末尾添加换行符
System.out.println("blap");
将打印字符串并添加换行符
所以
就像跑步一样
System.out.print("bla blap\n blo")
两者都将打印:
bla blap
blo
所以我得到了3个整数数组来打印
不确定如何从main获取要打印的布尔值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();
}
}
/**
*这个类生成一个20整数长的随机数组
*数组,反转数组,检查是否有
*任何重复的相邻数字,如果有或,则打印为真
*如果没有,则为false,然后打印结果。
*
*@作家马修·杰克逊
*@2017年9月5日版本
*
*/
导入java.util.*;
公共类排序序列
{
公共静态void main(字符串[]args)
{
System.out.print(“随机数组为:”);
int[]随机数=新的int[20];
随机数=生成数(20);
打印阵列(随机数);
System.out.println();
System.out.println(“排序的数组是:”);
sortArray(随机数);
打印阵列(随机数);
System.out.println();
System.out.println(“反向数组为:”);
打印阵列(反向(随机数));
System.out.println();
System.out.println(“它是“+”这个数组有相邻的副本”);
if(相邻重复项(随机数))
{
//在这里放点东西来打印布尔T/F?
}
}
/**
*生成返回的int类型和大小为n的数组
*此数组将从方法中删除
*
*@参数n生成随机数数组
*@返回生成的数组列表
*/
公共静态int[]生成器域(int n)
{
在里面
bla blap
blo
/**
* 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();
}
}