作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个需要这样的类的 A 代码:
问题对于本作业,您将编写一个模拟钓鱼游戏的程序。在这个游戏中,通过滚动六面骰子来确定用户捕获了什么。每个可能的元素都值得一定数量的钓鱼点。这些点数将保持隐藏状态,直到用户完成钓鱼,然后根据获得的钓鱼点数显示一条祝贺用户的消息。
以下是游戏设计的一些建议:
游戏的每一轮都是作为循环的迭代来执行,只要玩家想要钓鱼更多的元素,就会重复该循环。
每轮开始时,程序都会询问用户是否要继续钓鱼。
该程序模拟六面骰子的滚动
每个可以捕获的元素都由骰子生成的数字表示;例如,1 代表“大鱼”,2 代表“旧鞋”,3 代表“小鱼”,依此类推。
用户捕获的每个项目都具有不同数量的积分该循环保存用户钓鱼点的运行总数。
循环结束后,会显示钓鱼点的总数,并显示一条长长的消息,该消息根据所获得的点数而变化。
问题是,当我执行代码时,它总是给我零作为最终数字?
有人知道我可以做什么来解决这个问题吗?
import java.util.Random;
import java.util.Scanner;
import java.io.*;
public class apples {
public static void main(String[] args) {
Random dice = new Random();
Scanner kenny = new Scanner(System.in);
int die, choice;
int score = 0;
int points = 0;
int counter = 0;
//Opening statement
System.out.println("Let's go fishing!!!");
System.out.println("");
System.out.println("Would you like to fish?");
System.out.println("Type 1 for yes");
System.out.println("Type 2 for no");
//Variables set
//issue making the loop
do {
die = dice.nextInt(5);
choice = kenny.nextInt();
if (die == 0)
System.out.println("You caught: Aquaman, haha nice. +20");
points += 20;
if (die == 1)
System.out.println("You caught: A shark!! -30");
points -= 30;
if (die == 2)
System.out.println("You caught..Oh, there's my keys. +30");
points += 30;
if (die == 3)
System.out.println("You caught: Nemo. Come on man, that's not cool. -50");
points -= 50;
if (die == 4)
System.out.println("You caught: A still working copy of fight club! +50");
points += 50;
System.out.println("");
if (die == 5)
System.out.println("You caught: Iggy Azalea's newest album. Burn it. Now. -20");
points -= 20;
System.out.println("Would you like to fish?");
System.out.println("Type 1 for yes");
System.out.println("Type 2 for no");
counter++;
} while (choice == 1);
if (choice == 2) {
System.out.println("Game Over");
System.out.println("");
System.out.println("Your final score is: " + (points));
if (points > 0)
System.out.println("Yay! you won!");
if (points < 0)
System.out.println("Oh no! You lost :(");
System.out.println("Thanks for playing!");
}
}
}
最佳答案
你的缩进应该是一个重要的线索。采取这样的说法:
if (die == 0)
System.out.println("You caught: Aquaman, haha nice. +20");
points += 20;
由于此代码没有括号,因此 points += 20
行将独立于 if
语句执行。所有改变点的代码都会自行抵消,因此每次循环结束时您仍然会得到 0。
您希望每个 if 语句都这样:
if (die == 0)
{
System.out.println("You caught: Aquaman, haha nice. +20");
points += 20;
}
关于java - 鱼类模拟游戏错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26368666/
我是一名优秀的程序员,十分优秀!