2016-09-22 47 views
-5

我应该做一个简单的岩石剪刀代码,但我的println大部分都不起作用。我尝试添加另一个,看看它是否仅仅是我的变量赋值,用于选择和rand,但它也不输出任何内容。任何帮助深表感谢!System.out.Println被跳过

import java.util.Scanner; 

public class project4 { 

    public static void main(String[] args) { 
    Random in = new Random(); 
    Scanner key = new Scanner(System.in); 
    System.out.println("Please select one of [R/P/S]: "); 
    String choice = key.nextLine(); 
    int rand = in.nextInt(3) + 1; 
    int choose = 0; 
    if (choice.equals("r") || choice.equals("R")) { 
     choose = 1; 
     System.out.println("You chose Rock"); 
    } else { 
     if (choice.equals("P") || choice.equals("p")) { 
     choose = 2; 
     System.out.println("You chose Paper"); 
     } else { 
     if (choice.equals("s") || choice.equals("S")) { 
      choose = 3; 
      System.out.println("You chose Scissors"); 
     } 
     } 
     System.out.println("rand= " + rand + "choose =" + choose); 
     System.out.flush(); 
    } 
    if (rand == 1) { 
     System.out.println("I chose Rock"); 
    } else { 
     if (rand == 2) { 
     System.out.println("I chose Paper"); 
     } else { 
     System.out.println("I chose Scissors"); 
     } 

     if (choose == rand) { 
     System.out.println("Its a Tie!"); 
     } else { 
     if (choose == 1 && rand == 2) { 
      System.out.println("Paper beats Rock, You lose!"); 
     } else { 
      if (choose == 1 && rand == 3) { 
      System.out.println("Rock beats Scissors, You win!"); 
      } else { 
      if (choose == 2 && rand == 1) { 
       System.out.println("Paper beats Rock, You win!"); 
      } else { 
       if (choose == 2 && rand == 3) { 
       System.out.println("Scissors beats Paper, You lose!"); 
       } else { 
       if (choose == 3 && rand == 1) { 
        System.out.println("Rock beats Scissors, You lose!"); 
       } else { 
        System.out.println("Scissors beats Paper, You win!"); 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+2

第一步应永诺,鸵鸟政策开始怀疑Java代码不会运行。它最有可能不会跳过代码,而是怀疑自己的逻辑。还有什么不打印?对我来说,它工作 – SomeJavaGuy

+0

它说你选择哪一个之前,它说什么它选择,它应该输出什么变量兰特和选择有价值。那么它说你赢或输的任何一行或它的一个领带!在月蚀中我不会输出。 –

+0

有*路*太多'{}'s在这里为你不格式化这个干净;请修复它(-1)。 – ChiefTwoPencils

回答

0

您的System.out.println正在被跳过,因为您已将其中的一些放在'其他'条件中。由于这种情况,当'IF'条件成立时,else块被跳过,sysout也被跳过。下面的代码应该可以工作!

此外,我会建议使用别的,如果在地方的嵌套,如果因为这使情况更易读且不易出错。

package project4; 
import java.util.Random; 
import java.util.Scanner; 
public class project4 { 

public static void main(String[] args) { 
    Random in = new Random(); 
    Scanner key = new Scanner(System.in); 
    System.out.println("Please select one of [R/P/S]: "); 
    String choice = key.nextLine(); 
    int rand = in.nextInt(3) + 1; 
    int choose = 0; 
    if (choice.equals("r") || choice.equals("R")) { 
     choose = 1; 
     System.out.println("You chose Rock"); 
    } else { 
     if (choice.equals("P") || choice.equals("p")) { 
      choose = 2; 
      System.out.println("You chose Paper"); 
     } else { 
      if (choice.equals("s") || choice.equals("S")) { 
       choose = 3; 
       System.out.println("You chose Scissors"); 
      } 
     } 
    } 
    System.out.println("rand= " + rand + "choose =" + choose); 
    System.out.flush(); 
    if (rand == 1) { 
     System.out.println("I chose Rock"); 
    } else { 
     if (rand == 2) { 
      System.out.println("I chose Paper"); 
     } else { 
      System.out.println("I chose Scissors"); 
     } 
    } 

    if (choose == rand) { 
     System.out.println("Its a Tie!"); 
    } else { 
     if (choose == 1 && rand == 2) { 
      System.out.println("Paper beats Rock, You lose!"); 
     } else { 
      if (choose == 1 && rand == 3) { 
       System.out.println("Rock beats Scissors, You win!"); 
      } else { 
       if (choose == 2 && rand == 1) { 
        System.out.println("Paper beats Rock, You win!"); 
       } else { 
        if (choose == 2 && rand == 3) { 
         System.out.println("Scissors beats Paper, You lose!"); 
        } else { 
         if (choose == 3 && rand == 1) { 
          System.out.println("Rock beats Scissors, You lose!"); 
         } else { 
          System.out.println("Scissors beats Paper, You win!"); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

}