This question already has answers here:
Non-static variable cannot be referenced from a static context

(12个答案)



Why we can not make an instance of inner class inside the main method of outer class in regular way?

(4个答案)


在8个月前关闭。





我正在尝试创建此问题的解决方案:“让函数BlackjackHighest(strArr)接受传递的strArr参数,该参数将是代表二十一点卡的数字和字母的数组。该数组中的数字将被写出。例如strArr可以是[“ two”,“ 3”,“ ace”,“ king”]。strArr的完整功能列表包括:两个,三个,四个,五个,六个,七个,八个,九个,十个,杰克,皇后,国王,王牌。您的程序应在下方,上方或二十一点输出,表示您是否有二十一点(数字加起来等于21),以及您手中是否有二十一点的最高牌。包含一个ace,但是您的手牌将超过21,因此您必须将ace视为1。您必须始终尝试将其保持在21标记以下。因此,使用上述数组,输出应位于king之下。在此示例中为1,因为如果不是,那么您将高于21。

另一个示例是,如果strArr为[“ four”,“ ten”,“ king”],则此处的输出应高于king。如果您的手牌和十张面孔牌之间有平局,请将面孔牌作为“最高牌”返还。如果您有多张面孔卡,则重要性顺序为杰克,皇后,国王。”

我尝试了以下代码

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



  public class Main {
    static String BlackjackHighest(String[] strArr) {
        Card[] cards = new Card[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            cards[i] = new Card(strArr[i]);
        }

        Arrays.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card card, Card card2) {
                if (card.rating > card2.rating) return 1;
                else if (card.rating < card2.rating) return -1;
                return 0;
            }
        });

        int total = 0;
        boolean hasAce = false, aceUsed = false;
        for (Card card : cards) {
            if (card.rating == 14) {
                hasAce = true;
            }

            if (total + card.topVal > 21) {
                total += card.lowVal;
            } else {
                if (card.rating == 14) {
                    aceUsed = true;
                }
                total += card.topVal;
            }
        }

        Card highest = cards[cards.length - 1];
        if (hasAce && !aceUsed) {
            highest = cards[cards.length - 2];
        }

        if (total < 21) {
            return "below " + highest.repr;
        } else if (total == 21) {
            return "blackjack " + highest.repr;
        }
        return "above " + highest.repr;
    }

    class Card {
        int lowVal = 0, topVal = 0, rating = 0;
        String repr;

        Card(String card) {
            card = card.toLowerCase();
            repr = card;

            if (card.equals("two")) {
                lowVal = 2; topVal = 2; rating = 2;
            } else if (card.equals("three")) {
                lowVal = 3; topVal = 3; rating = 3;
            } else if (card.equals("four")) {
                lowVal = 4; topVal = 4; rating = 4;
            }  else if (card.equals("five")) {
                lowVal = 5; topVal = 5; rating = 5;
            } else if (card.equals("six")) {
                lowVal = 6; topVal = 6; rating = 6;
            } else if (card.equals("seven")) {
                lowVal = 7; topVal = 7; rating = 7;
            } else if (card.equals("eight")) {
                lowVal = 8; topVal = 8; rating = 8;
            } else if (card.equals("nine")) {
                lowVal = 9; topVal = 9; rating = 9;
            } else if (card.equals("ten")) {
                lowVal = 10; topVal = 10; rating = 10;
            } else if (card.equals("jack")) {
                lowVal = 10; topVal = 10; rating = 11;
            } else if (card.equals("queen")) {
                lowVal = 10; topVal = 10; rating = 12;
            } else if (card.equals("king")) {
                lowVal = 10; topVal = 10; rating = 13;
            } else if (card.equals("ace")) {
                lowVal = 1; topVal = 11; rating = 14;
            }
        }
    }

    public static void main (String[] args) {
        // keep this function call here
        Scanner s = new Scanner(System.in);
        Main c = new Main();
        System.out.print(c.BlackjackHighest(s.nextLine()));
    }
}


但是得到一个错误:

Main.java:10: error: non-static variable this cannot be referenced from a static context
            cards[i] = new Card(strArr[i]);

最佳答案

这段代码成功了

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

 class Main {
    public static String BlackjackHighest(String[] strArr) {
        Card[] cards = new Card[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            cards[i] = new Card(strArr[i]);
        }

        Arrays.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card card, Card card2) {
                if (card.rating > card2.rating) return 1;
                else if (card.rating < card2.rating) return -1;
                return 0;
            }
        });

        int total = 0;
        boolean hasAce = false, aceUsed = false;
        for (Card card : cards) {
            if (card.rating == 14) {
                hasAce = true;
            }

            if (total + card.topVal > 21) {
                total += card.lowVal;
            } else {
                if (card.rating == 14) {
                    aceUsed = true;
                }
                total += card.topVal;
            }
        }

        Card highest = cards[cards.length - 1];
        if (hasAce && !aceUsed) {
            highest = cards[cards.length - 2];
        }

        if (total < 21) {
            return "below " + highest.repr;
        } else if (total == 21) {
            return "blackjack " + highest.repr;
        }
        return "above " + highest.repr;
    }

    public static class Card {
        int lowVal = 0, topVal = 0, rating = 0;
        String repr;

        Card(String card) {
            card = card.toLowerCase();
            repr = card;

            if (card.equals("two")) {
                lowVal = 2; topVal = 2; rating = 2;
            } else if (card.equals("three")) {
                lowVal = 3; topVal = 3; rating = 3;
            } else if (card.equals("four")) {
                lowVal = 4; topVal = 4; rating = 4;
            }  else if (card.equals("five")) {
                lowVal = 5; topVal = 5; rating = 5;
            } else if (card.equals("six")) {
                lowVal = 6; topVal = 6; rating = 6;
            } else if (card.equals("seven")) {
                lowVal = 7; topVal = 7; rating = 7;
            } else if (card.equals("eight")) {
                lowVal = 8; topVal = 8; rating = 8;
            } else if (card.equals("nine")) {
                lowVal = 9; topVal = 9; rating = 9;
            } else if (card.equals("ten")) {
                lowVal = 10; topVal = 10; rating = 10;
            } else if (card.equals("jack")) {
                lowVal = 10; topVal = 10; rating = 11;
            } else if (card.equals("queen")) {
                lowVal = 10; topVal = 10; rating = 12;
            } else if (card.equals("king")) {
                lowVal = 10; topVal = 10; rating = 13;
            } else if (card.equals("ace")) {
                lowVal = 1; topVal = 11; rating = 14;
            }
        }
    }

    public static void main (String[] args) {
        // keep this function call here
        Scanner s = new Scanner(System.in);
        Main c = new Main();
        System.out.print(c.BlackjackHighest(s.nextLine()));
    }
}

10-06 10:51