簡體   English   中英

在Java中對兩列數組進行排序並輸出值頻率時出現問題

[英]Problems Sorting a two column array and outputting value frequency in Java

這是我遇到的問題,我花了很長時間研究for循環,數組和temp變量,現在我的輸出只是幾個數字。

/ *編寫一個程序,該程序將從鍵盤上讀取數字到int []類型的數組中。 您可以假設數組中的條目數為50個或更少。 您的程序允許輸入任意數量的數字,最多50個。輸出將是一個兩列的列表。 第一列是不同數組元素的列表。 第二個是每個元素出現次數的計數。 該列表應按第一列中的條目大小從大到小排序。

對於數組:-12、3,-12、4、1、1,-12、1,-1、1、2、3、4、2、3,-12,輸出應為:N Count 4 2 3 3 2 2 1 4 -1 1 -12 4 * /

import java.util.Scanner;

public class Project2C {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int[][] twoColumn = new int[2][50];
        int[] inputValues = new int[50];
        int temp = 0;
        int valueFrequency = 0;
        int lastUsedSpace = 0;

        //gather user input to fill an array (up to 50 values);
        System.out.println("Input up to 50 values.");
        for (int i = 0; i < 50; i++) {
            System.out.println("value #" + (i + 1) + ":");
            inputValues[i] = keyboard.nextInt();
            /*System.out.println("Press 0 to stop, or 1 to continue.");
             if (keyboard.nextInt() == 0) {
             break;

             } 
             else if (keyboard.nextInt() == 1){
             continue;
             }
             else if (keyboard.nextInt() != 0 && keyboard.nextInt() != 1) {
             System.out.println("You must enter 0 or 1. Now you must re-enter the value.");
             i--;
             }*/
        }

        // checking if each value occurs more than once, and assigning it a place
        // in the two column array if it is unique.
        for (int i = 0; i < inputValues.length; i++) {
            for (int j = 0; j < inputValues.length; j++) {
                if (i == 0 && inputValues[i] != inputValues[j]) {
                    twoColumn[0][lastUsedSpace] = inputValues[i];
                } else if (i > 0 && inputValues[i] != inputValues[j]) {
                    twoColumn[0][lastUsedSpace + 1] = inputValues[i];
                }
            }
        }
        lastUsedSpace = -1;
        //Sorting the first column of the two column array
        for (int i = 0; i < twoColumn.length; i++) {
            for (int j = 0; j < twoColumn.length; j++) {
                if (twoColumn[0][i] < twoColumn[0][j + 1]) {
                    temp = twoColumn[0][j + 1];
                    twoColumn[0][j + 1] = twoColumn[0][i];
                    twoColumn[0][i] = temp;
                }
            }
        }
        // filling in the frequency column of the array
        for (int i = 0; i < inputValues.length; i++) {
            for (int j = 0; j < inputValues.length; j++) {
                if (inputValues[i] == inputValues[j]) {
                    valueFrequency = valueFrequency + 1;
                }
                if (j <= inputValues.length - 1 && lastUsedSpace == -1) {
                    lastUsedSpace = 0;
                    twoColumn[1][0] = valueFrequency;
                    valueFrequency = 0;
                } else if (j <= inputValues.length - 1 && lastUsedSpace > -1) {
                    twoColumn[1][lastUsedSpace + 1] = valueFrequency;
                    valueFrequency = 0;
                }

            }

        }
        //printing output
        for (int i = 0; i < twoColumn.length; i++) {
            System.out.println("Input    Frequency");
            System.out.println(twoColumn[0][i]+"    "+twoColumn[1][i]);
            }
        }
    }

}

在那里,我測試並修復了該問題,如果您希望用戶必須經歷整個50次,則應該取出-999爵士樂

import java.util.Arrays;

import java.util.Scanner;


public class swinging {
static Scanner keyboard = new Scanner(System.in);
static int[] inputValues = new int[50];
int temp = 0;
int valueFrequency = 0;
int lastUsedSpace = 0;
public static void main(String[] args){
int j = 0;
 for (; j < 50; j++) {
        System.out.println("value #" + (j + 1) + ":");
        inputValues[j] = keyboard.nextInt();
       if(inputValues[j]==-999)break;
    }
    theValues= bubbleSort(Arrays.copyOf(inputValues, j));

        for (int i = 0; i < theValues.length; i++) {
            System.out.println("Input    Frequency");
            System.out.println(theValues[i]+"    "+howMany[i]);
     }
    }
    static int[] theValues;
    static int[] howMany;
    public static int[] bubbleSort(int[] Is ){
    boolean switchedOne=true;
    int temp;
    howMany=new int[Is.length];
    Arrays.fill(howMany,1);
    int length=Is.length-1;
    while(switchedOne){switchedOne=false;
    for(int i=0;i<length;i++){
    if(Is[i]>Is[i+1]){temp=Is[i];Is[i]=Is[i+1];Is[i+1]=temp;switchedOne=true;
                       temp=howMany[i];howMany[i]=howMany[i+1];howMany[i+1]=temp;}
    if(Is[i]==Is[i+1]){Is=removeElement(Is,i+1);howMany=removeElement(howMany,i+1);howMany[i]++;length--;}
    }
    }
    return Is;
    }

    public static int[] removeElement(int[] Is,int index){
    for(int i=index;i<Is.length-1;i++){Is[i]=Is[i+1];}
    return Arrays.copyOf(Is,Is.length-1);
    }}

如果您不使用循環並希望在更高級別解決問題,則可以使用TreeMap和NavigableMap。 請參見下面的示例。

// ArrayGroupByCount.java
package com.geoloo.array;

import java.util.HashMap;
import java.util.NavigableMap;
import java.util.Scanner;
import java.util.TreeMap;

/*
 * Description: Display occurence of entered numbers in descending order
 * Sample input/output:
        Input up to 50 values. 0 to exit
        value #1:-12
        value #2:3
        value #3:-12
        value #4:4
        value #5:1
        value #6:1
        value #7:-12
        value #8:1
        value #9:-1
        value #10:1
        value #11:2
        value #12:3
        value #13:4
        value #14:2
        value #15:3
        value #16:-12
        value #17:0
        map: {1=4, 2=2, 3=3, 4=2, -12=4, -1=1}
        nmap: {4=2, 3=3, 2=2, 1=4, -1=1, -12=4}
 */

public class ArrayGroupByCount {

    public static void main(String[] args) {

        Integer input = 0;

        Scanner keyboard = new Scanner(System.in);
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        TreeMap<Integer, Integer> treemap = new TreeMap<Integer, Integer>();

        System.out.println("Input up to 50 values. 0 to exit");
        for (int i = 0; i < 50; i++) {
            System.out.print("value #" + (i + 1) + ":");
            input = (int)keyboard.nextInt();

            if(input==0){
                break;
            }
            int content = 0;

            if(map.containsKey(input))
                content = map.get(input);

            map.put(input, content+1);
        } 

        keyboard.close();
        treemap.putAll(map);
        NavigableMap<Integer, Integer> nmap=treemap.descendingMap();

        System.out.println("map: "+map);
        System.out.println("nmap: "+nmap);
    }
}

包project2c;

導入java.util.Scanner;

公共課程Project2C {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int valueAmount = 0;
    int temp = 0;
    int valueFrequency = 1;

    //gather user input to fill an array (up to 50 values);
    System.out.println("how many values would you like to process?");
    valueAmount = keyboard.nextInt();
    int[] inputValues = new int[valueAmount];
    for (int i = 0; i < valueAmount; i++) {
        System.out.println("value #" + (i + 1) + ":");
        inputValues[i] = keyboard.nextInt();

    }
    //sort values in descending order
    for (int i = 0; i < inputValues.length - 1; i++) {
        for (int j = 0; j < inputValues.length - 1; j++) {
            if (inputValues[j + 1] > inputValues[j]) {
                temp = inputValues[j + 1];
                inputValues[j + 1] = inputValues[j];
                inputValues[j] = temp;
            }
        }
    }
    //print out put
    System.out.println();
    System.out.println("Value  Frequency");
    for (int i = 0; i < inputValues.length - 1; i++) {
        if (inputValues[i] == inputValues[i + 1]) {
            valueFrequency = valueFrequency + 1;
        } else if (inputValues[i] > inputValues[i + 1]) {
            System.out.println(inputValues[i] + "     " + valueFrequency);
            valueFrequency = 1;
        }

    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM