[英]My program doesn't print out anything when I execute it
我正在做一个剪刀程序,在我的代码中,如果您向下滚动到determineWinner()
方法,我已经设置好了,但是在main()
方法中,当我调用它时,它不会显示是否您赢了,被绑或输了。
这是我的主要方法:
public class RockPaperScissorsMain {
public static void main(String[] args) {
RockPaperScissorsClass rc = new RockPaperScissorsClass();
System.out.println("Player is: " + rc.getPlayer());
System.out.println("Computer is: " + rc.getComputer());
System.out.println(rc.determineWinner());
}
}
这是我的课。
public class RockPaperScissorsClass {
private int wins;
private int losses;
private int ties;
private int CChoice;
private int PChoice;
public RockPaperScissorsClass(int wins, int losses, int ties, int computerPick, int playerPick) {
this.wins=wins;
this.losses=losses;
this.ties=ties;
this.CChoice=CChoice;
this.PChoice=PChoice;
}
public String getPlayer() {
Scanner in = new Scanner(System.in);
System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->");
int PPChoice = in.nextInt();
String PChoice = null;
if(PPChoice==1) {
PChoice="Rock";
}
else if(PPChoice==2) {
PChoice="Paper";
}
else if(PPChoice==3) {
PChoice="Scissors";
}
else {
while(true) {
System.out.println("You have entered an invalid choice. Please try again.");
System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->");
PPChoice = in.nextInt();
if(PPChoice==1) {
PChoice="Rock";
break;
}
else if(PPChoice==2) {
PChoice="Paper";
break;
}
else if(PPChoice==3) {
PChoice="Scissors";
break;
}
}
return PChoice;
}
public String getComputer() {
Random rand = new Random();
int CCChoice = rand.nextInt(3)+1;
String CChoice = null;
if(CCChoice==1) {
CChoice="Rock";
}
else if(CCChoice==2) {
CChoice="Paper";
}
else if(CCChoice==3) {
CChoice="Scissors";
}
return CChoice;
}
public String determineWinner() {
String detWinner = "";
if(PChoice==1 && CChoice==2) {
detWinner = "You Lose";
}
else if(PChoice==1 && CChoice==3) {
detWinner = "You Win";
}
else if(PChoice==2 && CChoice==3) {
detWinner = "You Lose";
}
else if(PChoice==2 && CChoice==1) {
detWinner = "You Win";
}
else if(PChoice==3 && CChoice==1) {
detWinner = "You Lose";
}
else if(PChoice==3 && CChoice==2) {
detWinner = "You Win";
}
else if(PChoice==1 && CChoice==1){
detWinner = "You Have Tied";
}
else if(PChoice==2 && CChoice==2) {
detWinner = "You Have Tied";
}
else if(PChoice==3 && CChoice==3){
detWinner = "You Have Tied";
}
return detWinner;
}
public RockPaperScissorsClass() {this(0,0,0,0,0);}
public void setPlayer(int p) {CChoice = p;}
}
您正在用局部变量遮盖字段
例如
int PPChoice = in.nextInt();
改成
PPChoice = in.nextInt();
编辑
实际上,情况更糟,您正在使用其他类型的变量。 局部变量是字符串。 IMO完全摆脱了String,甚至没有使用过。
尝试一些类似的代码
Scanner in = new Scanner(System.in);
PChoice = 0;
while (PChoice == 0) {
System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->");
PChoice = in.nextInt();
}
调用get方法时,应该同时设置播放器和计算机的选择。 固定在下面。
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissorsClass {
private int wins;
private int losses;
private int ties;
private int CChoice;
private int PChoice;
public RockPaperScissorsClass()
{
this(0,0,0,0,0);
}
public RockPaperScissorsClass(int wins, int losses, int ties, int computerPick, int playerPick)
{
this.wins=wins;
this.losses=losses;
this.ties=ties;
this.CChoice=computerPick;
this.PChoice=playerPick;
}
public void setPlayer(int p)
{
PChoice = p;
}
public String getPlayer()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->");
int PPChoice = in.nextInt();
String PChoice = null;
if(PPChoice==1)
{
PChoice="Rock";
}
else if(PPChoice==2)
{
PChoice="Paper";
}
else if(PPChoice==3)
{
PChoice="Scissors";
}
else
{
while(true)
{
System.out.println("You have entered an invalid choice. Please try again.");
System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->");
PPChoice = in.nextInt();
if(PPChoice==1)
{
PChoice="Rock";
break;
}
else if(PPChoice==2)
{
PChoice="Paper";
break;
}
else if(PPChoice==3)
{
PChoice="Scissors";
break;
}
}
}
this.setPlayer(PPChoice);
return PChoice;
}
public void setComputer(int c){
CChoice = c;
}
public String getComputer()
{
Random rand = new Random();
int CCChoice = rand.nextInt(3)+1;
String CChoice = null;
if(CCChoice==1)
{
CChoice="Rock";
}
else if(CCChoice==2)
{
CChoice="Paper";
}
else if(CCChoice==3)
{
CChoice="Scissors";
}
this.setComputer(CCChoice);
return CChoice;
}
public String determineWinner()
{
String detWinner = "";
if(PChoice==1 && CChoice==2)
{
detWinner = "You Lose";
}
else if(PChoice==1 && CChoice==3)
{
detWinner = "You Win";
}
else if(PChoice==2 && CChoice==3)
{
detWinner = "You Lose";
}
else if(PChoice==2 && CChoice==1)
{
detWinner = "You Win";
}
else if(PChoice==3 && CChoice==1)
{
detWinner = "You Lose";
}
else if(PChoice==3 && CChoice==2)
{
detWinner = "You Win";
}
else if(PChoice==1 && CChoice==1)
{
detWinner = "You Have Tied";
}
else if(PChoice==2 && CChoice==2)
{
detWinner = "You Have Tied";
}
else if(PChoice==3 && CChoice==3)
{
detWinner = "You Have Tied";
}
return detWinner;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.