简体   繁体   English

如何使用一种方法以升序和降序输入数组?

[英]How do I use a method to input an array in ascending and descending order?

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

And i have this error. 而且我有这个错误。

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.

I am simply trying to use a method to display my array in ascending or descending order(depending on what the user chooses). 我只是试图使用一种方法来以升序或降序显示我的数组(取决于用户选择的内容)。 When I compile the code i get about 10 errors all on the first line of each method. 当我编译代码时,我在每种方法的第一行都得到大约10个错误。 I have looked it up on google and I couldn't find anything useful. 我在Google上进行了查询,但找不到任何有用的信息。 Please Help me out. 请帮帮我。 ----jGRASP exec: javac -g Grade.java ---- jGRASP执行:javac -g Grade.java

You can't declare a method inside a method. 您不能在方法内部声明方法。

 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]);
     }
}

If you don't indent your code you'll never understand what you wrote . 如果你不缩进你的代码,你将永远不会明白你写的是什么 By the way i don't think that your printUp and printDown method is going to do what you want. 顺便说一句,我认为您的printUpprintDown方法不会做您想要的事情。 First you have to sort the array i think. 首先,您必须对我认为的数组进行排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何按降序和升序对奇数和偶数数组进行排序? - How do you sort odd and even array numbers in descending and ascending order? 如何使程序以升序而不是降序计数? - How do i make it so that the program counts in an ascending order instead of descending? 如何使这种合并排序以降序而不是升序进行? - How do I make this merge sort go in descending order rather than ascending? 如何使用CompareTo按升序和降序对PlaneMap进行排序 - How to use CompareTo to sort the PlaneMap by Ascending and Descending order 按升序和降序对数组的 LinkedHashmap 进行排序 - Java - Sort LinkedHashmap of Array in ascending and descending order - Java 在循环中检查数组是升序还是降序 - Checking if an Array is in Ascending or Descending order in a loop 如何按升序对2D字符串数组进行排序? - How do i sort a 2d String array in ascending order? 如何按升序对数组进行排序,但通过找到最大的 integer? - how do i sort an array in ascending order, but by finding the greatest integer? Java:如何在单个文本文件中打印升序列,然后在该列的旁边打印同一组整数,除了按降序排列 - Java : How do I print an ascending column and next to that column the same set of integers except in descending order all in one single text file 如何对数组的前半部分按升序排序,后半部分按降序排序 - How to sort first half of an array in ascending order and second half in descending order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM