gpt4 book ai didi

Java Blackjack 程序创建错误 : non-static variable this cannot be referenced from a static context

转载 作者:行者123 更新时间:2023-12-01 22:34:48 25 4
gpt4 key购买 nike

我正在尝试为这个问题创建一个解决方案:“让函数 BlackjackHighest(strArr) 接受传递的 strArr 参数,该参数将是代表二十一点牌的数字和字母数组。数组中的数字将被写出。例如,strArr 可能是 ["two","Three","ace","king"]。strArr 的完整可能性列表是:二、三、四、五、六、七、八、九、十、jack、queen、king、ace。你的程序应该输出below、above或blackjack,表示你是否有blackjack(数字加起来为21),以及你手中最大的牌与你是否有blackjack有关。如果数组包含一张 A,但你的手牌将高于 21,则必须将 A 计为 1。你必须始终尝试保持在 21 标记以下。因此,使用上述数组,输出应低于 King。在此示例中被计为 1,因为如果不是,您的分数就会高于 21。

另一个例子是,如果 strArr 为 ["four","ten","king"],则此处的输出应高于 king。如果您手上有 10 和一张人头牌,则将人头牌归还为“最大的牌”。如果您有多张人头牌,则重要性顺序是 J、Q、K。”

我尝试了以下代码

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

关于Java Blackjack 程序创建错误 : non-static variable this cannot be referenced from a static context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58532317/

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