如何在Java游戏中修复此错误?
我有一点web开发语言的编程经验,如HTML、CSS、JQuery、JavaScript、PHP、Python等,但我不会在学校上AP计算机科学课来学习Java。为了了解它在语法上与其他语言有多相似,以及如何处理不同的问题,我决定制作一个简单的21点游戏,我以前用其他语言制作过这个游戏 我遇到了一个错误,我想检查用户输入的内容,如果是某个角色,则输出不同的内容,例如帮助语句、欢迎消息,或者只是启动或停止游戏。出于某种原因,它读取输入,我可以将其记录到控制台,但它不会运行我在while循环中的if语句中创建和调用的方法。我将发布整个脚本,因为我现在只使用Java编程2周左右,不知道我的错误可能在哪里如何在Java游戏中修复此错误?,java,Java,我有一点web开发语言的编程经验,如HTML、CSS、JQuery、JavaScript、PHP、Python等,但我不会在学校上AP计算机科学课来学习Java。为了了解它在语法上与其他语言有多相似,以及如何处理不同的问题,我决定制作一个简单的21点游戏,我以前用其他语言制作过这个游戏 我遇到了一个错误,我想检查用户输入的内容,如果是某个角色,则输出不同的内容,例如帮助语句、欢迎消息,或者只是启动或停止游戏。出于某种原因,它读取输入,我可以将其记录到控制台,但它不会运行我在while循环中的if
import java.util.Scanner;
public class testClass {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
// Creates a scanner to get input from the keyboard
boolean gameStart = false;
// starts false, turns true when 'B' is input
welcome();
//displays the welcome message
while (true) {
// loop to play the game
String input = kbd.nextLine().toUpperCase();
// gets the next input from the user
if (input == "B") {
// checks the users input
gameStart = true;
// 'turns on' the game
double[] hand = drawCards();
// makes an array of 2 cards
System.out.print(input); //REMOVE
} else if (input == "?") {
// checks the users input
help(gameStart);
// displays the help message depending on if the game has started or not
System.out.print(input); //REMOVE
} else if (input == "Q") {
// checks the users input
System.out.print(input); // REMOVE
break;
// ends the game loop
} // ends if
} // ends while
} // end main
public static void welcome() {
// displays the welcome message
System.out.printf("Welcome to blackjack! %nEnter 'B' to begin. %nEnter '?' at any time to see help %nEnter 'Q' at any time to quit %n => ");
// the welcome message
}
public static void help(boolean gameStart) {
//displays the help message
if (gameStart) {
System.out.printf("Enter '?' at any time to see help %nEnter 'Q' at any time to quit %nEnter 'H' to hit %nEnter 'S' to stay %n => ");
// the help message when the game has already started
} else {
System.out.printf("Welcome to blackjack! %nEnter 'B' to begin. %nEnter '?' at any time to see help %nEnter 'Q' at any time to quit %nEnter 'H' to hit %nEnter 'S' to stay %n => ");
// the help message when the game hasn't started
}
} // end help
public static double randCard() {
// creates a random card
double card = Math.round(Math.random() * 10);
// number 1-10
if (card == 10 || card == 0) {
// no cards at 10 or 0
double x = Math.round(Math.random() * 10);
// variable used to choose J Q K or A when card is 0 or 10
if (x < 3) {
// Jack
card = 11;
} else if (x < 5) {
// Queen
card = 12;
} else if (x < 7) {
// King
card = 13;
} else {
// Ace
card = 10;
}
}
return card;
// returns a single card
} // end randCard
public static double[] drawCards() {
//creates 2 random cards and returns an array
double card1 = randCard();
double card2 = randCard();
// stores the 2 random cards in variables
double[] cards = {card1, card2};
// places the 2 random cards in an array
return cards;
// returns the array of cards
} // end drawCards
} // end class
import java.util.Scanner;
公共类testClass{
公共静态void main(字符串[]args){
扫描仪kbd=新扫描仪(System.in);
//创建扫描仪以从键盘获取输入
布尔gameStart=false;
//开始为false,输入“B”时变为true
欢迎();
//显示欢迎信息
while(true){
//循环来玩游戏
字符串输入=kbd.nextLine().toUpperCase();
//获取用户的下一个输入
如果(输入=“B”){
//检查用户的输入
gameStart=true;
//“打开”游戏
双[]手=抽卡();
//制作一个由2张牌组成的数组
System.out.print(输入);//删除
}else if(输入=“?”){
//检查用户的输入
帮助(游戏开始);
//根据游戏是否已启动显示帮助消息
System.out.print(输入);//删除
}else if(输入=“Q”){
//检查用户的输入
System.out.print(输入);//删除
打破
//结束游戏循环
}//如果
}//结束时
}//末端总管
公共静态无效欢迎(){
//显示欢迎信息
System.out.printf(“欢迎使用二十一点!%n输入'B'开始。%n输入'?'随时查看帮助%n输入'Q'随时退出%n=>”;
//欢迎辞
}
公共静态无效帮助(布尔gameStart){
//显示帮助消息
如果(游戏开始){
System.out.printf(“随时输入“?”以查看帮助%n随时输入'Q'以退出%n输入'H'以点击%n输入'S'以保持%n=>”;
//当游戏已经开始时显示帮助消息
}否则{
System.out.printf(“欢迎使用二十一点!%n输入'B'开始。%n输入'?'随时查看帮助%n输入'Q'随时退出%n输入'H'点击%n输入'S'停留%n=>”;
//游戏尚未启动时的帮助消息
}
}//结束帮助
公共静态双随机卡(){
//创建一张随机卡片
双卡=Math.round(Math.random()*10);
//1-10号
如果(卡片==10 | |卡片==0){
//10点或0点没有牌
double x=Math.round(Math.random()*10);
//当卡为0或10时,用于选择J Q K或A的变量
if(x<3){
//杰克
卡片=11;
}else if(x<5){
//女王
卡片=12;
}else if(x<7){
//国王
卡片=13;
}否则{
//王牌
卡片=10;
}
}
回程卡;
//返回一张卡
}//结束randCard
公共静态双[]抽卡(){
//创建2张随机卡并返回一个数组
双卡1=随机卡();
双卡2=随机卡();
//将2张随机卡存储在变量中
双[]卡={card1,card2};
//将2张随机卡放置在一个阵列中
回程卡;
//返回卡片数组
}//结束抽卡
}//结束类
使用equals()
字符串方法,而不是=
。希望这能有所帮助。这里列出了您需要纠正的事项:
- 按照惯例,建议使用以大写字母开头的类名
- 字符串比较应该通过
完成,而不是通过比较引用的“=”完成 这意味着,更改以下事件的发生:equals()
到input==“B”
input.equals(“B”)
到input==“?”
input.equals(“?”)
到input==“Q”
input.equals(“Q”)
输入
为null
,将导致NullPointerException。更好的做法是在这种情况下使用“B”.equals(输入)
。看起来这些已经作为评论的一部分得到了回答
p.S:我不能说您的程序在这次更改后会正常工作,但这些更改应该会对您有所帮助。还要注意,通常,类名以大写字母开头,当使用
==
进行双重更改时,请将此(input==“B”)
与(input.equals(“B”))
。您也可以这样尝试(“B”。等于(输入))。我所能说的就是Java是面向对象的;)