我有一个类的A代码,需要这个:
问题所在 对于此作业,您将编写一个模拟钓鱼游戏的程序。在这个游戏中,滚动六面模具以确定用户捕获的内容。每个可能的项目都值得一定数量的钓点。在用户完成钓鱼之前,这些点将保持隐藏状态,然后根据获得的钓点数量显示祝贺用户的消息。
以下是游戏设计的一些建议:
每轮比赛都是作为循环的迭代进行的,只要玩家想捕获更多物品,该循环就会重复。
在每轮开始时,程序会询问用户他或她是否想继续钓鱼。
程序模拟六面模具的滚动
可以捕获的每个项目由从骰子生成的数字表示;例如,1为#34;巨大的鱼",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!");
}
}
}
答案 0 :(得分:3)
你的缩进应该是一个很大的线索。请接受以下声明:
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;
}
答案 1 :(得分:0)
在每个if语句后加上大括号。如果你不保持,那么你的输出将为零,因为20-30 + 30-50 + 50 + 20 = 0