gpt4 book ai didi

java - 如何使用方法按升序和降序输入数组?

转载 作者:行者123 更新时间:2023-12-01 23:34:38 24 4
gpt4 key购买 nike

import java.io.*;
import java.util.*;

public class Grade

{//class
public static void main (String [] args)
{//main
Scanner keyBoard = new Scanner(System.in);

int [] gradeFreq = new int [101];`enter code here`
int input = 0;
System.out.print ("Enter next grade: ");
input = keyBoard.nextInt();
while(input >= 0){
gradeFreq[input]++;
System.out.print("Please enter next grade");
input = keyBoard.nextInt();
}
System.out.print ("To show results in ascending order press 1... or 2 for descending");
input = keyBoard.nextInt();

switch(input){
case 1:
printUp(gradeFreq);
break;
case 2:
printDown(gradeFreq);
break;
}
public static void printUp(int[] array){
System.out.print("Ascending");
for(int i = 0; i < array.length; i++){
if(array[i] > 0)
System.out.printf("%5d\t%5d\n",i,array[i]);
}
}
public static void printDown(int[] array){
System.out.print("Descending");
for(int i = 0; i < array.length; i--){
if(array[i] > 0)
System.out.printf("%5d\t%5d\n",i,array[i]);
}
}


}//end of main
}//end of class

我有这个错误。

Grade.java:37: error: illegal start of expression
public static void printUp(int[] array){
^
Grade.java:37: error: illegal start of expression
public static void printUp(int[] array){
^
Grade.java:37: error: ';' expected
public static void printUp(int[] array){
^
Grade.java:37: error: '.class' expected
public static void printUp(int[] array){
^
Grade.java:37: error: ';' expected
public static void printUp(int[] array){
^
Grade.java:44: error: illegal start of expression
public static void printDown(int[] array){
^
Grade.java:44: error: illegal start of expression
public static void printDown(int[] array){
^
Grade.java:44: error: ';' expected
public static void printDown(int[] array){
^
Grade.java:44: error: '.class' expected
public static void printDown(int[] array){
^
Grade.java:44: error: ';' expected
public static void printDown(int[] array){
^
10 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

我只是尝试使用一种方法以升序或降序显示我的数组(取决于用户选择的内容)。当我编译代码时,我在每个方法的第一行收到大约 10 个错误。我在谷歌上查了一下,但找不到任何有用的东西。请帮帮我。----jGRASP 执行:javac -g Grade.java

最佳答案

不能在方法内声明方法。

 public static void main(String args []){
// your code here..
}

public static void printUp(int[] array){
System.out.print("Ascending");
for(int i = 0; i < array.length; i++){
if(array[i] > 0)
System.out.printf("%5d\t%5d\n",i,array[i]);
}
}

public static void printDown(int[] array){
System.out.print("Descending");
for(int i = 0; i < array.length; i--){
if(array[i] > 0)
System.out.printf("%5d\t%5d\n",i,array[i]);
}
}

如果你不缩进你的代码,你将永远无法理解你所写的内容。顺便说一句,我认为您的 printUp 和 printDown 方法不会执行您想要的操作。首先你必须对我认为的数组进行排序。

关于java - 如何使用方法按升序和降序输入数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18885375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com