2016-01-13 22 views
0
package rps; 


import java.util.Random; 
import java.util.Scanner; 

public class Rps 
{ 
public static void main(String[] args) 
{ 

    int rock, paper, scissors; 
    int compa; 
    String Computer; 
    String Human; 


Scanner keyboard = new Scanner(System.in); 
System.out.println("Rock, paper, or scissors?"); 
Human= keyboard.nextLine(); 
System.out.println("You chose"); 
System.out.println(Human); 

try 
{ 
    int HC = Integer.parseInt(Human.trim()); // i know this part is wrong 
} 
catch (NumberFormatException nfe) 
{ 

Random generator = new Random(); 
compa=generator.nextInt(3); 

switch(compa){ 
     case 0: 
     rock=0; 
     System.out.println("I choose Rock"); 
     if (Human.equals(2)); 
     System.out.println("Rock crushes scissors, i win"); 
     if(Human.equals(0)); 
System.out.println("You must be psychic, i chose rock too!"); 

     if (Human.equals(1)); 
System.out.println("paper covers rock, you lost"); 
break; 

     case 1: 
     paper=1; 
     System.out.println("I chose paper"); 
     if (Human.equals(0)); 
     System.out.println("Paper covers rock, you lose"); 
     if (Human.equals(1)); 
     System.out.println("It's a tie!"); 
     if (Human.equals(2)); 
     System.out.println("Paper covers rock, i lost"); 
     break; 
    case 2: 
     scissors=2; 
     System.out.println("I chose scissors"); 
     if (Human.equals(1)); 
    System.out.println("scissors cut through paper, i win."); 
     if (Human.equals(2)); 
    System.out.println("We both chose scissors"); 
     if (Human.equals(0)); 
    System.out.println("Rock smashed scissors, i lost"); 
    break; 


     } 
    } 

我要串人转换为整数这就是我的教练告诉我,做之前,我插入的try/catch我该如何修复我的java代码的岩石剪刀剪刀?

因为人类并不像(123)的值,我图的尝试catch可以用于数字格式异常。

在我的原代码,我没有使用HC为变量

我继续使用人力 但它仍然列出的所有问题的答案,而不是人比较计算机

+0

首先,你的'if'语句被打破。 if语句需要用'{'和'}'来包围它们的身体。像这样:'if(1 + 1 == 2){return 2; }'。 “if”部分旁边没有分号,只在主体('return 2;')中。试着看看[Codecademy](https://www.codecademy.com/learn/learn-java)来补充你在学校的学习。或者,如果您已分配了课本,则可能需要更频繁地参考。您也可以为您提供辅导资源。修复程序中的现有问题可能会帮助您解决问题。 – ordonezalex

+0

在上面的代码中为什么你在catch块中使用switch,并且你在catch块的catch下面编码,因为你没有关闭它。 –

回答

0

从你的代码的开关部分,长相像你接受用户输入为整数的权利?

如果是这样的话,为什么不使用nextInt()来读取int输入而不是nextLine()

0

取而代之的是尴尬的try/catch块的,我建议你给用户的菜单促使他们输入一个整数,你可以很容易地用它来比较你的随机整数,如:

Scanner keyboard = new Scanner(System.in); 
System.out.println("Make Selection:"); 
System.out.println("1) Rock"); 
System.out.println("2) Paper"); 
System.out.println("3) Scissors "); 
int choice = keyboard.nextInt()-1; 

如果你需要接受一个字符串是你赋值的一部分,那么使用equals方法将字符串通过if语句转换为一个整数。

int choice; 
string human= keyboard.nextLine(); 
if(human.equals("Rock")){ 
    choice = 0; 
}else if(human.equals("Paper"){ 
    choice = 1; 
}else if(human.equals("Scissors"){ 
    choice = 2; 
}else{ 
    System.out.print("Invalid Selection"); 
} 

有在你的代码(注意,评论)其他错误,但至少应该给你一些选项来克服获取用户输入的东西你的程序可以方便地使用的驼峰。

2

有在这条线的两大错误,并在你的代码中的许多类似的路线:

if (Human.equals(2)); 

第一个错误是,分号是错误的。 Java(像C和其他语言)有一个“空”语句,只是一个分号,没有别的,它什么都不做。什么,你实际上已经做的是

if (Human.equals(2)) 
    /* do nothing */ ; 
System.out.println("Rock crushes scissors, i win"); 

if没有任何影响,而println发生无论是否if是真还是假。摆脱分号。此外,最好在使用总是花括号为if的身体的习惯来获得,像

if (Humans.equals(2)) { 
    System.out.println("Rock crushes scissors, i win"); 
} 

的Java不需要括号,但最好要始终使用它们。一些公司和风格检查员确实需要大括号。而且它们有助于防止在您认为陈述属于if的情况下出现的错误,而事实并非如此。

第二个是你将StringHuman)与不起作用的整数(2)进行比较。这将永远返回false,不管是什么Human是。 Java会让你比较两个不同类的对象,但结果总是false,除非你写了自己的自定义equals,它可以比较两个不同类的对象(通常不是一个好主意,除非它们是相同的子类祖先类)。

它看起来像你想要解析输入字符串作为一个整数,给出一个int,然后使用int做你的比较。(这对用户来说有点令人困惑,因为你没有告诉他们输入一个数字,但我会继续这样做,因为它可以让我做出其他重要的一点。)这就是你所尝试的:

try 
{ 
    int HC = Integer.parseInt(Human.trim()); // i know this part is wrong 
} 
catch (NumberFormatException nfe) 
{ 

你的逻辑的其余部分是catch内,这意味着它将执行只有Human不是有效的整数,这可能不是你想要的。 catch块中的代码仅在发生异常时执行。您可以通过逻辑的其余部分移动到try块(大括号)解决这个问题,然后该代码可以比较HC而不是Human,像这样:

if (HC == 2) { 
    System.out.println("Rock crushes scissors, i win"); 
} 

你可以做的另一件事是早些时候关闭catch区块。你可以这样做:

int HC; 
try 
{ 
    HC = Integer.parseInt(Human.trim()); // i know this part is wrong 
} 
catch (NumberFormatException nfe) 
{ 
    HC = -1; 
} 

我不建议处理异常这样,但我这样做是为了自己的观点。如果您在try的花括号内部说int HC = Integer.parseInt(Human.trim());,则只能在花括号内看到。因此,低于catch块的逻辑无法看到HC。通过将声明HC移到那些大括号外,现在可以通过catch块之后的逻辑来看到它。所以,那么你可以再次改变if (Human.equals(2))

if (HC == 2) 
0

首先,我想记的问题,导致一些错误的原代码。

  1. 结束标记}在代码结束时缺失以关闭该类。
  2. 正如在评论中提到的那样,您的if语句在其末尾应该没有分号,而应该在if语句之前立即使用开头和结尾括号{}。现在,据说您可以不使用方括号,但是只有在if语句之前的行才会嵌套在if语句>下。
  3. try...catch应该用来捕捉程序中的任何错误并正确处理它们。在你的代码中,你正在使用它来帮助运行程序;这是不好的做法,应尽可能避免。

现在至于解决您的问题,我看不到用这里显示的逻辑来做到这一点的方法。在你的算法你:

  1. 输入用户的选择
  2. 尝试将单词转换成数字
  3. 如果它的工作原理,它分配给一个变量,并结束该程序
  4. 如果它不工作在程序

这里的错误是,根据您所期待的输入,你总是会失败try/catch并没有真正用户的输入转换为数字,执行代码的其余部分。这会导致后面的错误,因为您的if语句正在检查Human的值以查看它是否是数字,而不是。

正如在一个不同的答案中提到的,你可以改变输入以接受和期待一个数字而不是一个字符串(即“输入数字为:Rock = 0,Paper = 1,Scissors = 2:”) 。这将从一开始就处理数字问题,并有助于简化您的代码。

解决这个问题的另一种方法是,如果您宁愿坚持使用文本输入而不使用数字,那么将包括一个简单的开关,将用户的输入转换为数字。例如:

int humanNumber = -1; 
int rock = 0; 
int paper = 1; 
int scissors = 2; 
switch(Human) { 
    case 'rock': 
    humanNumber = rock; 
    break; 
    case 'paper': 
    humanNumber = paper; 
    break; 
    case 'scissors': 
    humanNumber = scissors; 
    break; 
    default: 
    System.out.println("Invalid input"); 
} 

这样做将允许输入保持为文本,该文本转换为数字,并处理任何不必要的投入。

,我想用是建议的最终解决方案:

所有的
public static void main(String[] args) { 
    int rock = 0, paper = 1, scissors = 2; 
    int computer = -1, human = -1; 
    String input; 

    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Rock, paper, or scissors?"); 
    input = keyboard.nextLine(); 
    System.out.println("You chose " + input); 

    switch(input.toLowerCase()) { 
     case "rock": 
      human = rock; 
      break; 
     case "paper": 
      human = paper; 
      break; 
     case "scissors": 
      human = scissors; 
      break; 
     default: 
      System.out.println("Invalid input"); 
    } 
    if (human >= 0) { 

     Random generator = new Random(); 
     computer = generator.nextInt(3); 
     System.out.println(human + " " + computer); 
     switch(computer) { 
      case 0: 
       System.out.println("I chose Rock"); 
       if (human == scissors) 
        System.out.println("Rock crushes scissors, i win"); 
       else if (human == rock) 
        System.out.println("You must be psychic, i chose rock too!"); 
       else 
        System.out.println("paper covers rock, i lost"); 
       break; 
      case 1: 
       System.out.println("I chose Paper"); 
       if (human == rock) 
        System.out.println("Paper covers rock, you lose"); 
       else if (human == paper) 
        System.out.println("It's a tie!"); 
       else 
        System.out.println("Paper covers rock, i lost"); 
       break; 
      case 2: 
       System.out.println("I chose scissors"); 
       if (human == paper) 
        System.out.println("scissors cut through paper, i win."); 
       else if (human == scissors) 
        System.out.println("We both chose scissors"); 
       else 
        System.out.println("Rock smashed scissors, i lost"); 
       break; 
     } 
    } 
}