- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
What is a NullPointerException, and how do I fix it?
(12 个回答)
6年前关闭。
我想通过让每个人都知道这是我第一次遇到堆栈溢出来引导这个问题,所以如果我不符合提问标准,请告诉我。
我正在制作一个和你一起玩摇滚、纸、剪刀的程序,就在我接近项目的后端时,出现了这个错误:
Exception in thread "main" java.lang.NullPointerException
at RockPaperScissors.getPlayerThrow(RockPaperScissors.java:93)
at RockPaperScissors.main(RockPaperScissors.java:26)
public class RockPaperScissors {
//sets the constants
static final int ROCK = 1;
static final int PAPER = 2;
static final int SCISSORS = 3;
//creates some variables
static int playerThrow, computerThrow, result, timesPlayed, playerWins, computerWins;
static String playAgain;
static Scanner fru;
/*
* The Results
* 0 = tie
* 1 = Player win
* 2 = Computer win
*/
public static void main(String[] args) {
//this do while loop is the whole game
do {
//decides the throws of the players
playerThrow = getPlayerThrow();
computerThrow = (int)(Math.random() * 3 + 1);
switch(playerThrow) {
//compares and displays the computer and player
//choices if the player chooses rock
case ROCK:
switch(computerThrow) {
case ROCK:
result = 0;
System.out.println("You threw rock and the computer threw rock!");
break;
case PAPER:
result = 2;
System.out.println("You threw rock and the computer threw paper!");
break;
case SCISSORS:
result = 1;
System.out.println("You threw rock and the computer threw scissors!");
break;
} break;
//compares and displays the computer and player
//choices if the player throws paper
case PAPER:
switch(computerThrow) {
case ROCK:
result = 1;
System.out.println("You threw paper and the computer threw rock!");
break;
case PAPER:
result = 2;
System.out.println("You threw paper and the computer threw paper!");
break;
case SCISSORS:
result = 3;
System.out.println("You threw paper and the computer threw scissors!");
break;
} break;
//compares and displays the computer and player
//choices if the player throws scissors
case SCISSORS:
switch(computerThrow) {
case ROCK:
result = 2;
System.out.println("You threw scissors and the computer threw rock!");
break;
case PAPER:
result = 1;
System.out.println("You threw scissors and the computer threw paper!");
break;
case SCISSORS:
result = 0;
System.out.print("You threw scissors and the computer threw scissors!");
break;
} break;
}
timesPlayed ++;
// will compare and decide the winner of the two players
finish();
} while (timesPlayed < 3);
}
public static int getPlayerThrow() {
//prompts weapon choice and stores said choice
System.out.println("Choose your weapon of choice:\n(1 for rock, 2 for paper, 3 for scissors)");
int choice = fru.nextInt();
//checks for validity and returns the choice
if (choice != 1 && choice != 2 && choice != 3) {
System.out.print("Not a valid input!\n Please try again: ");
choice = fru.nextInt();
}
return choice;
}
//compares and decides the winner of the two players
public static void finish() {
//displays the winner of the round accourding to aforementioned possible results
switch(result) {
case 0:
System.out.println("Its a tie!"); break;
case 1:
System.out.println("You are victorious! Man over machine!");
playerWins++; break;
case 2:
System.out.println("The computer has taken the round! Technological singularity approaches!");
computerWins++; break;
}
//cheks if the match is over and displays messages accordingly
switch(timesPlayed) {
case 1: break;
case 2:
if (playerWins == 2 || computerWins == 2) {
if (playerWins == 2) {
System.out.println("You win the match! Congratulations!\nWould you like to play another match?\n(y for yes, n for no)");
timesPlayed = 5;
playAgain = fru.nextLine();
//checks for validity
if (playAgain != "y" || playAgain != "n") {
System.out.print("Not a valid input!\n Please try again: ");
playAgain = fru.nextLine();
}
}
else if (computerWins == 2) {
System.out.println("The computer wins the match!\nPlay again! I know you can beat it.\n(y for yes, n for no)");
timesPlayed = 5;
playAgain = fru.nextLine();
//checks for validity
if (playAgain != "y" || playAgain != "n") {
System.out.print("Not a valid input!\n Please try again: ");
playAgain = fru.nextLine();
}
}
} break;
//will happen for any amount of times played over 2
default:
if (playerWins == 2) {
System.out.println("You win the match! Congratulations!\nWould you like to play another match?\n(y for yes, n for no)");
playAgain = fru.nextLine();
//checks for validity
if (playAgain != "y" || playAgain != "n") {
System.out.print("Not a valid input!\n Please try again: ");
playAgain = fru.nextLine();
}
}
else if (computerWins == 2) {
System.out.println("The computer wins the match!\nPlay again! I know you can beat it.\n(y for yes, n for no)");
playAgain = fru.nextLine();
//checks for validity
if (playAgain != "y" || playAgain != "n") {
System.out.print("Not a valid input!\n Please try again: ");
playAgain = fru.nextLine();
}
}
}
}
}
最佳答案
我看到你在很多地方使用 fru
扫描仪,但在您实际初始化它的地方绝对没有。你所拥有的基本上归结为:
import java.util.Scanner;
public class Test {
static Scanner fru;
public static void main(String[] arg) {
int x = fru.nextInt();
System.out.println(x+1);
}
}
fru = new Scanner(System.in);
import java.util.Scanner;
public class Test {
static Scanner fru;
public static void main(String[] arg) {
fru = new Scanner(System.in);
int x = fru.nextInt();
System.out.println(x+1);
}
}
Exception in thread "main" java.lang.NullPointerException
at RockPaperScissors.getPlayerThrow(RockPaperScissors.java:93)
at RockPaperScissors.main(RockPaperScissors.java:26)
int choice = fru.nextInt();
fru
未正确设置。从那里开始,需要追溯到您实际设置
fru
的位置。对有用的东西。在这种特殊情况下,它不存在,因此相对容易弄清楚。
关于java - Rock,Paper,Scissors程序中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32899924/
我一直在尝试编写一个简单的 RPS 游戏,用户可以在其中与计算机对战。此时只有 1 次迭代(因为我卡住了......)并且没有验证用户输入是否有效。我遇到的问题是,无论我尝试做什么以将计算机和用户的分
我正在解决一个问题,它说: In Big Bang Theory, Sheldon and Raj created a new game: "rock-paper-scissors-lizard-Sp
我一直在制作一款剪刀石头布游戏,它运行起来就像做梦一样。但是,当我尝试在(显示为 # 的)中添加一些验证时,我的游戏无法运行。我不确定为什么会这样。 我的代码如下: from random i
我是 python 的初学者。下面是我的剪刀石头布脚本。预期的输出是按照规则抛出“你赢了”或“你输了”或“平局”。但是,即使我按照下面给定的逻辑实际上赢了,它也会抛出“你输了”。 我哪里出错了?有人可
我的“剪刀石头布”作业也要求我使用非常具体的直径。它根本不起作用。该页面甚至不会加载,而不是出现任何记录的控制台。 我最初将控制台中的所有字符串作为在页面上弹出的警报,但这不起作用,所以我只是将其切换
我正在制作一个剪刀石头布游戏。我已经测试了某些部分,它们似乎可以单独工作,但不能一起工作。 我不知道该尝试什么,但我尝试了不同的方法来生成数字。 constructor() { super()
我知道有多种方法可以为“岩石,纸张,剪刀”游戏编写代码。但是我已经根据我对C++的基本知识尝试编写以下代码。 using std::cin; using std::cout; using std::e
昨晚我在想“石头剪刀布”连续 10 次得到相同结果的概率。我想出了如何做到这一点并完成了该任务,但后来我想挑战一下自己,所以我想调整该程序,使其运行初始程序多次(10,000),然后输出平均结果,我将
我正在使用 Javascript 进行编码。 http://www.programmingbasics.org/en/beginner/random.html(请按“下一步”,直到到达“石头剪刀布”部
嘿伙计们,我已经编写了下面的代码,一切似乎都工作正常,除了当它应该是平局时我不断得到“name2 wins”以及当答案应该无效时。我不知道出了什么问题,因为我的逻辑似乎是正确的。 print("Wel
这就是我为我的剪刀石头布游戏想出的: import random from time import sleep print "Please select: " print "1 Rock" prin
我一直在尝试批量制作石头剪刀布游戏。到目前为止,脚本运行,我可以让它与自己对战或与我对战(我需要将 B var 从/A 和随机数更改为/P 并有用户输入)。 问题是,在几轮(每次不同的数量)之后,脚本
我正在尝试使用 Scissoring 来剪辑使用 spriteBatch.DrawString 方法绘制的文本。 以下是我的 Draw 方法的一部分: public override void
我是这里的新成员,也是 JavaScript 的新手。直到 4 周前才开始编码。学习了 html,然后学习了 css,现在学习了 JavaScript。 我正在尝试制作一个rps 游戏,但是可以这么说
因此,当我运行它时,我开始输入值,但在输入值后它不会执行 switch 语句来告诉我谁赢了。我还没有把所有的案例都放好,但我想测试一下,但什么也没发生。我给 y 分配了一个随机值,但它不起作用,所以我
本文整理了Java中com.google.gwt.webgl.client.WebGLRenderingContext.scissor()方法的一些代码示例,展示了WebGLRenderingCont
一段时间以来,我一直在努力解决这个问题,但没有成功。我有一个名为 gameScore() 的函数,它应该跟踪玩家和计算机的得分以及平局。我将分数定义为全局变量,因此从函数内部更新它们应该没有问题。 我
我是 JavaScript 的新手。我只是不明白为什么这有时会给错误的赢家。它表示当用户选择 Rock 而计算机选择 Paper 时,Rock 获胜。其他几种组合也出现了错误。谁能告诉我哪里出错了?谢
我是 Micro:bit python 的初学者。我正在为带有计分系统的“剪刀石头布”游戏编写代码。但是,它会响应一次摇动,然后即使没有人摇动它也会自动显示一张图片。您能建议一种解决此问题的方法吗?
所以我正在尝试创建这个石头、剪刀、布程序来在 telnet 服务器上运行。但它不允许我输入“石头”、“布”或“剪刀”等词。 第一个 CPUhand 显示为未定义,并且未设置为我的 if 语句中的选项之
我是一名优秀的程序员,十分优秀!