java - 如何将点系统添加到我的游戏(java)
问题描述
我的代码在这里遇到了一个小问题,我想添加一个积分计数器系统,但我做不到
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package rockpapercissor;
import java.util.Scanner;
/**
*
* @author almx9
*/
public class RockPaperCissor {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int Player;
Scanner input = new Scanner(System.in);
System.out.println("How many rounds do you want to play");
int Rounds = input.nextInt();
for (int i = 0 ; i < Rounds ; i++)
{
int Computer = (int) (Math.random()*3)+1;
System.out.println("Choose one:\n 1- Rock \n 2- Paper \n 3- Cissor");
Player = input.nextInt();
Check(Computer,Player);
}
System.out.println(" \nGame Finished thank you for playing... \n\n\n");
}
public static void Check (int C , int P )
{
/*
1 = Rock
2 = Paper
3 = Cissor
*/
if ((C == 1) && (P == 2))
{
System.out.println("Computer is Rock and you are Paper \nYou won!!\n");
}
if ((C == 1) && (P == 3))
{
System.out.println("Computer is Rock and you are Cissor \nYou Lost!!\n");
}
if ((C == 2) && (P == 1))
{
System.out.println("Computer is Paper and you are Rock \nYou Lost!!\n");
}
if ((C == 2) && (P == 3))
{
System.out.println("Computer is Paper and you are Cissor \nYou won!!\n");
}
if ((C == 3) && (P == 1))
{
System.out.println("Computer is Cissor and you are Rock \nYou won!!\n");
}
if ((C == 3) && (P == 2))
{
System.out.println("Computer is Cissor and you are Paper \nYou Lost!!\n");
}
if ((C == 1) && (P == 1))
{
System.out.println("Computer is Rock and you are Rock \nIt's a Draw!!\n");
}
if ((C == 2) && (P == 2))
{
System.out.println("Computer is Paper and you are Paper \nIt's a Draw!!\n");
}
if ((C == 3) && (P == 3))
{
System.out.println("Computer is Cissor and you are Cissor \nIt's a Draw!!\n");
}
}
}
如何添加积分系统,例如如果玩家是摇滚而计算机是 Cissor 我想显示积分,我尝试在 if 语句中使用
int PPoints = 0;
int CPoints = 0;
if ((C == 3) && (P == 1))
{
PPoints++;
System.out.println("Computer is Cissor and you are Rock \nYou won!!\nPlayer Points:" + PPoints + "Computer Points:" + CPoints + "\n" );
}
但它总是将值返回 0,我是 java 新手,我还在学习。谢谢你
解决方案
您的代码有几个问题,我将一一解决:
1. 命名约定
我已经在评论中提到了这一点:
Java变量以小写字母开头,int Player;不好。它应该是 int player;同样适用于 int C , int P 和 int PPoints = 0; int CPoints = 0;。以大写字母开头的变量用于类。方法名称也以小写字母开头。– 辍学 3 分钟前
请参考https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html
2.条件逻辑的使用和check
方法
该方法有几个if
s,即使被触发也会导致检查其他条件。换句话说,如果您的第一个条件为真,那么为什么还要检查其他情况呢?这更有效:
public static int check(int computer, int player) {
/*
1 = Rock
2 = Paper
3 = Cissor
*/
if (computer == player) { //covers all draws
System.out.println("It's a draw!");
return 0;
} else {
if (computer == 1 && player == 2) { //R vs P
System.out.println("Player wins!");
return 2;
} else if (computer == 1 && player == 3) { //R vs S
System.out.println("Computer wins!");
return 1;
} else if (computer == 2 && player == 1) { //P vs R
System.out.println("Computer wins!");
return 1;
} else if (computer == 2 && player == 3) { //P vs S
System.out.println("Player wins!");
return 2;
} else if (computer == 3 && player == 2) { //S vs P
System.out.println("Computer wins!");
return 1;
} else if (computer == 3 && player == 1) { //S vs R
System.out.println("Player wins!");
return 2;
}
}
return -1;
}
请注意,该方法现在返回一个int
包含有关哪个玩家获胜的信息 - 1 是 player1,2 是 player2,0 是平局,-1 当出现问题时返回,但这不应该发生。一旦一个条件为真,它内部的 return 就会被调用,并且它不会在整个方法中进一步不必要地进行。
有更好的方法来实现 RPS 游戏,但为了时间和示例,让我们继续这样做。
3.最终实施评分系统
您需要将保存分数的变量放在循环函数之外,这样它们就不会在每个循环中重置回 0。然后在你玩游戏的周期内,你应该根据谁获胜来增加它们
public static void main(String[] args) {
int player;
int cScore = 0; //<-- here
int pScore = 0; //<-- here
Scanner input = new Scanner(System.in);
System.out.println("How many rounds do you want to play");
int rounds = input.nextInt();
for (int i = 0; i < rounds; i++) {
int computer = (int) (Math.random() * 3) + 1;
System.out.println("Choose one:\n 1- Rock \n 2- Paper \n 3- Scissors");
player = input.nextInt();
int winner = check(computer, player);
if (winner > 0) { //not a draw or an error
if (winner == 1) { //computer won
cScore++;
} else { //player won
pScore++;
}
}
}
System.out.println(String.format("Final score -\tCOMPUTER:%s\tPLAYER:%s",
cScore,
pScore
));
System.out.println(" \nGame Finished thank you for playing... \n\n\n");
}
推荐阅读
- c# - 在 SQL 中存储考勤数据
- azure-batch - AzureBatch ImageReference 类下的自定义图像不起作用
- shell - 在 Linux 中发送电子邮件时无法附加文件和包含正文
- sqlite - SQLite3::SQLException: 没有这样的表:"
- python - 使用 select django 渲染表单
- android - 显示 ProgressDialog 并在一段时间后调用 OnBackPressed()
- c# - 为什么调用 OnBackPressed 时我的应用程序崩溃?
- android - Android Volley 注册
- python - Python避免双重for循环
- flutter - Flutter 中的动态视图