我被要求制作一个 Create a new integer array with 16 elements
I have been asked to make an Create a new integer array with 16 elements
Java 代码(不是 Java 脚本)。我被要求创建一个包含 16 个元素的新整数数组。
用户(扫描仪)输入的数组中只能输入 1 到 7 之间的整数。
只允许有效的用户输入,任何超出范围输入的整数(即 < 1 或 > 7 都应排除在外,并显示一条警告消息。
设计一个对数组进行排序的程序。
程序应该显示排序数组的内容。
然后程序应该显示用户输入选择的每个数字的出现次数
但是我一直在尝试逐步完成此代码并利用我的知识来帮助我但需要帮助我当前的代码在下面我将不胜感激如果有人能够将我的代码编辑成上面的代码 wants.I 知道它需要通过用户输入存储输入数组并重复使用代码对数字进行排序以对数组进行排序。
结果应该打印出这样的东西 like this
“输入数组的数字是:”1、2、4、5、7
“您选择要搜索的号码是”7
“这在数组中发生”3“次”
import java.util.Scanner;
public class test20 {
public static void main (String[] args){
Scanner userInput = new Scanner (System.in);
int [] nums = {1,2,3,4,5,6,7,6,6,2,7,7,1,4,5,6};
int count = 0;
int input = 0;
boolean isNumber = false;
do {
System.out.println ("Enter a number to check in the array");
if (userInput.hasNextInt()){
input = userInput.nextInt();
System.out.println ("The number you chose to search for is " + input);
isNumber = true;
}else {
System.out.println ("Not a proper number");
}
for (int i = 0; i< nums.length; i++){
if (nums [i]==input){
count ++;
}
}
System.out.println("This occurs " + count + " times in the array");
}
while (!(isNumber));
}
private static String count(String string) {
return null;
}
}
import java.util.Scanner;
import java.util.Arrays;
public class test20 {
private static int readNumber(Scanner userInput) {
int nbr;
while (true) {
while(!userInput.hasNextInt()) {
System.out.println("Enter valid integer!");
userInput.next();
}
nbr = userInput.nextInt();
if (nbr >= 1 && nbr <= 7) {
return nbr;
} else {
System.out.println("Enter number in range 1 to 7!");
}
}
}
private static int count(int input, int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++){
if (nums[i] == input){
count++;
} else if (nums[i] > input) {
break;
}
}
return count;
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int[] nums = new int[16];
for (int i = 0; i < nums.length; i++) {
nums[i] = readNumber(userInput);
}
Arrays.sort(nums);
System.out.println ("Sorted numbers: " + Arrays.toString(nums));
int input = 0;
while(true) {
System.out.println("Search for a number in array");
input = readNumber(userInput);
System.out.println("The number you chose to search for is " + input);
System.out.println("This occurs " +
count(input, nums) + " times in the array");
}
}
}
因为数组是排序的,所以如果找到一个比我们要查找的元素大的元素,我就会中断循环;如果我们遇到一个更大的,那么在数组的其余部分就找不到其他匹配项了。
Java 代码(不是 Java 脚本)。我被要求创建一个包含 16 个元素的新整数数组。
用户(扫描仪)输入的数组中只能输入 1 到 7 之间的整数。
只允许有效的用户输入,任何超出范围输入的整数(即 < 1 或 > 7 都应排除在外,并显示一条警告消息。 设计一个对数组进行排序的程序。
程序应该显示排序数组的内容。 然后程序应该显示用户输入选择的每个数字的出现次数 但是我一直在尝试逐步完成此代码并利用我的知识来帮助我但需要帮助我当前的代码在下面我将不胜感激如果有人能够将我的代码编辑成上面的代码 wants.I 知道它需要通过用户输入存储输入数组并重复使用代码对数字进行排序以对数组进行排序。
结果应该打印出这样的东西 like this “输入数组的数字是:”1、2、4、5、7 “您选择要搜索的号码是”7 “这在数组中发生”3“次”
import java.util.Scanner;
public class test20 {
public static void main (String[] args){
Scanner userInput = new Scanner (System.in);
int [] nums = {1,2,3,4,5,6,7,6,6,2,7,7,1,4,5,6};
int count = 0;
int input = 0;
boolean isNumber = false;
do {
System.out.println ("Enter a number to check in the array");
if (userInput.hasNextInt()){
input = userInput.nextInt();
System.out.println ("The number you chose to search for is " + input);
isNumber = true;
}else {
System.out.println ("Not a proper number");
}
for (int i = 0; i< nums.length; i++){
if (nums [i]==input){
count ++;
}
}
System.out.println("This occurs " + count + " times in the array");
}
while (!(isNumber));
}
private static String count(String string) {
return null;
}
}
import java.util.Scanner;
import java.util.Arrays;
public class test20 {
private static int readNumber(Scanner userInput) {
int nbr;
while (true) {
while(!userInput.hasNextInt()) {
System.out.println("Enter valid integer!");
userInput.next();
}
nbr = userInput.nextInt();
if (nbr >= 1 && nbr <= 7) {
return nbr;
} else {
System.out.println("Enter number in range 1 to 7!");
}
}
}
private static int count(int input, int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++){
if (nums[i] == input){
count++;
} else if (nums[i] > input) {
break;
}
}
return count;
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int[] nums = new int[16];
for (int i = 0; i < nums.length; i++) {
nums[i] = readNumber(userInput);
}
Arrays.sort(nums);
System.out.println ("Sorted numbers: " + Arrays.toString(nums));
int input = 0;
while(true) {
System.out.println("Search for a number in array");
input = readNumber(userInput);
System.out.println("The number you chose to search for is " + input);
System.out.println("This occurs " +
count(input, nums) + " times in the array");
}
}
}
因为数组是排序的,所以如果找到一个比我们要查找的元素大的元素,我就会中断循环;如果我们遇到一个更大的,那么在数组的其余部分就找不到其他匹配项了。