【问题标题】:Fish simulation game error鱼模拟游戏错误
【发布时间】:2023-04-11 06:24:02
【问题描述】:

我有一个需要这个的类的 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!");
    }
  }
}

【问题讨论】:

  • 请从代码中删除空行...以使其可读...
  • 除了你的问题(Andrew Arnold 已经回答了)在你的游戏结束时你会打印你赢了还是输了。但是当你的总点数等于 0 时,它们都不会被执行。因此,如果这不是故意的,您可能想在其中一个上添加= 标志:)

标签: java simulation


【解决方案1】:

你的缩进应该是一个很大的线索。采取这种说法:

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

【讨论】:

    【解决方案2】:

    在每个 if 语句后加上花括号。如果你不保留,那么你的输出将为零,因为 20-30+30-50+50+20=0

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2020-09-20
      • 1970-01-01
      • 2019-05-09
      • 2010-11-28
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多