简体   繁体   English

Java-BubbleSort-有没有办法让我多次输入相同的输入?

[英]Java - BubbleSort - Is there a way for me to have the same input multiple times?

I'm trying to sort cards by their value using BubbleSort, but have got some problems when using the same card or a card with a similar value. 我正在尝试使用BubbleSort按卡的值对卡进行排序,但是在使用同一张卡或具有相似值的卡时遇到了一些问题。

import java.util.HashMap;

public class KartenSort {

private int zwei = 2;
private int drei = 3;
private int vier = 4;
private int fuenf = 5;
private int sechs = 6;
private int sieben = 7;
private int acht = 8;
private int neun = 9;
private int zehn = 10;
private int koenig = 10;
private int dame = 10;
private int bube = 10;
private int ass = 11;

public int[] liste ={drei,zwei,fuenf,vier,koenig,sieben,ass};
public int[] sortieren(){

    int unsortiert;
    for(int sortiert = 0; sortiert < liste.length -1; sortiert++){

        if(liste[sortiert] <= liste[sortiert+1]){
            continue;
        }

    unsortiert = liste[sortiert];
        liste[sortiert] = liste[sortiert+1];
        liste[sortiert+1] = unsortiert;
        sortieren();
    }
return  liste;
}

public static void  main (String[] args){

    HashMap<Integer,String> numbers = new HashMap<>();
    numbers.put(2, "zwei");
    numbers.put(3, "drei");
    numbers.put(4, "vier");
    numbers.put(5, "fuenf");
    numbers.put(6, "sechs");
    numbers.put(7, "sieben");
    numbers.put(8, "acht");
    numbers.put(9, "neun");
    numbers.put(10, "zehn");
    numbers.put(10, "koenig");
    numbers.put(10, "dame");
    numbers.put(10, "bube");
    numbers.put(11, "ass");

    KartenSort bs = new KartenSort();
    int[] array = bs.sortieren();
    for (int sortiert=0;sortiert < array.length; sortiert++){
        System.out.println(numbers.get(array[sortiert]));
    }

}

}

Instead of it printing out "koenig", "dame", etc... all cards with the value 10 will show as "bube". 而不是打印“ koenig”,“ dame”等...所有值10的卡都将显示为“ bube”。 Is there maybe a way to map them? 有没有可能将它们映射的方法?

This statements:- 这句话:-

if(liste[sortiert] < liste[sortiert+1]){
  continue;
}

When both operands same value it will be evaluated to false . 当两个操作数的值相同时,它将被评估为false End up recursive sortieren() calls without array index increase. 结束递归sortieren()调用而不增加数组索引。 Change to <= operator for continue statement to work. 更改为<=运算符,以使continue语句起作用。 For example:- 例如:-

if(liste[sortiert] <= liste[sortiert+1]){
  continue;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM