我正在用Java编写二十一点程序,在学习使用内部类时我遇到了一个问题。我尝试在其自身内部实例化Blackjack
对象以调用内部类Card
的方法,但程序找不到Blackjack
类。我怎么能解决这个问题:
import java.util.Scanner;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
public class Blackjack {
int playerWins = 0, dealerWins = 0;
// Inner Class - private for encapsulation
private class Card {
private int suit;
private int value;
private Card(int suit, int value) {
this.suit = suit;
this.value = value;
}
private int getValue() {
return value;
}
private int getSuit() {
return suit;
}
}
public static void main(String[] args) {
Deque<Card> deck = new ArrayDeque<Card>();
Deque<Card> player = new ArrayDeque<Card>();
Deque<Card> dealer = new ArrayDeque<Card>();
Blackjack game = new Blackjack();
game.createDeck(deck); // Create deck
game.shuffle(deck);// Shuffle
game.play(deck,player,dealer);// Deal
}
...
我在尝试编译时遇到的错误:
Exception in thread "main" java.lang.ClassNotFoundException: Blackjack
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
答案 0 :(得分:0)
我已经实现了createDeck,shuffle,play方法,并且该类正确地工作。 代码如下:
import java.util.Deque;
import java.util.ArrayDeque;
public class Blackjack {
int playerWins = 0, dealerWins = 0;
private void createDeck(Deque<Card> deck) {
System.out.println("createDeck");
}
private void shuffle(Deque<Card> deck) {
System.out.println("shuffle");
}
private void play(Deque<Card> deck, Deque<Card> player, Deque<Card> dealer) {
System.out.println("play");
}
// Inner Class - private for encapsulation
private class Card {
private int suit;
private int value;
private Card(int suit, int value) {
this.suit = suit;
this.value = value;
}
private int getValue() {
return value;
}
private int getSuit() {
return suit;
}
}
public static void main(String[] args) {
Deque<Card> deck = new ArrayDeque<Card>();
Deque<Card> player = new ArrayDeque<Card>();
Deque<Card> dealer = new ArrayDeque<Card>();
Blackjack game = new Blackjack();
game.createDeck(deck);
game.shuffle(deck);
game.play(deck, player, dealer);
}
}
编辑:
查看您的错误,也许Blackjack类的文件夹不在类路径中。如果从包含的文件夹中启动该类,则必须设置&#34;。&#34;类路径中的文件夹。