Java 查找数组的模式:无输出
我正在编写一个程序,以10个数字作为输入,使用并行数组显示数字的模式,并使用一个方法将数字数组作为参数,返回数组中最常出现的值。问题:然而,当我运行我的程序时,它绝对没有输出,而且,我不知道如何实现并行阵列。 有人知道吗? 多谢各位Java 查找数组的模式:无输出,java,arrays,try-catch,output,mode,Java,Arrays,Try Catch,Output,Mode,我正在编写一个程序,以10个数字作为输入,使用并行数组显示数字的模式,并使用一个方法将数字数组作为参数,返回数组中最常出现的值。问题:然而,当我运行我的程序时,它绝对没有输出,而且,我不知道如何实现并行阵列。 有人知道吗? 多谢各位 import java.util.Scanner; 公共课模式{ public static void main(String[] args) { } public int computeMode(int[] nums) { Scanner scanne
import java.util.Scanner;
公共课模式{
public static void main(String[] args) {
}
public int computeMode(int[] nums) {
Scanner scanner = new Scanner(System.in);
int maxValue = -1;
int maxCount = 0;
int x = 0;
//count how many times nums[i] appears in array
System.out.println("Enter 10 numbers: ");
for (int i = 0; i < 10; i++) {
try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
x = scanner.nextInt();
} catch (Exception e) {
System.out.println("Invalid input! Please reenter 10 integer values.");
scanner = new Scanner(System.in);
i = -1;
continue;
}
for (i = 0; i < nums.length; i++) {
int count = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] == nums[i]) {
count++;
}
}
if (count > maxCount) {
maxValue = nums[i];
maxCount = count;
System.out.println("The mode is: " + maxValue);
}
}
}
return maxValue;
}
publicstaticvoidmain(字符串[]args){
}
公共int计算模式(int[]nums){
扫描仪=新的扫描仪(System.in);
int maxValue=-1;
int maxCount=0;
int x=0;
//计算nums[i]在数组中出现的次数
System.out.println(“输入10个数字:”);
对于(int i=0;i<10;i++){
try{//try catch exception捕获十进制输入以及大于/小于10个整数
x=scanner.nextInt();
}捕获(例外e){
System.out.println(“输入无效!请重新输入10个整数值”);
扫描仪=新扫描仪(System.in);
i=-1;
继续;
}
对于(i=0;i最大计数){
maxValue=nums[i];
最大计数=计数;
System.out.println(“模式为:“+maxValue”);
}
}
}
返回最大值;
}
}代码写入
x
,但从不读取;读nums
,但从不写;并实现computeMode
,但从不调用它。主函数为空,因此它没有任何功能,
这个函数不需要任何参数,因为你可以用扫描仪读取数字
我想你需要这个:
import java.util.Scanner;
class Mode {
public static void main(String[] args) {
computeMode();
}
public static void computeMode(){
int nums[]=new int[10];
Scanner scanner = new Scanner(System.in);
int maxValue = -1;
int maxCount = 0;
int x = 0;
//count how many times nums[i] appears in array
System.out.println("Enter 10 numbers: ");
for (int i = 0; i < 10; i++) {
try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
x = scanner.nextInt();
nums[i]=x;
}
catch (Exception e) {
System.out.println("Invalid input! Please reenter 10 integer values.");
i =i -1;
scanner.nextLine();
continue;
}
}
for (int i = 0; i < nums.length; i++) {
int count = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] == nums[i]) {
count++;
}
}
if (count > maxCount) {
maxValue = nums[i];
maxCount = count;
}
}
System.out.println("The mode is: " + maxValue);
}
}
import java.util.Scanner;
类模式{
公共静态void main(字符串[]args){
computeMode();
}
公共静态void computeMode(){
int nums[]=新int[10];
扫描仪=新的扫描仪(System.in);
int maxValue=-1;
int maxCount=0;
int x=0;
//计算nums[i]在数组中出现的次数
System.out.println(“输入10个数字:”);
对于(int i=0;i<10;i++){
try{//try catch exception捕获十进制输入以及大于/小于10个整数
x=scanner.nextInt();
nums[i]=x;
}
捕获(例外e){
System.out.println(“输入无效!请重新输入10个整数值”);
i=i-1;
scanner.nextLine();
继续;
}
}
对于(int i=0;i最大计数){
maxValue=nums[i];
最大计数=计数;
}
}
System.out.println(“模式为:“+maxValue”);
}
}
Aha这很有效,谢谢。你知道这个方法是否使用并行数组吗?我将如何编辑异常以仅捕获字母或符号(任何不是数字的东西,因为程序只是指定数字…我假设它表示整数或十进制)@TurtleLoop只有一个数组(nums[]),则此方法不使用并行数组。这里有一个并行阵列的示例: