Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有办法使用1以外的数字作为输入以继续程序?它似乎没有认识到我所面临的问题;我有_Java - Fatal编程技术网

Java 是否有办法使用1以外的数字作为输入以继续程序?它似乎没有认识到我所面临的问题;我有

Java 是否有办法使用1以外的数字作为输入以继续程序?它似乎没有认识到我所面临的问题;我有,java,Java,所以我在做一个纸牌游戏,它几乎就像石头剪刀一样。这是规则,皇帝打公民,公民打奴隶,奴隶打皇帝。一方有4个公民和一个奴隶,另一方有4个公民和一个皇帝。我已经为每张牌设置了相等的数值,但由于某些原因,我似乎无法在不使用1来扮演皇帝的情况下使程序继续 public static void emperorsTurn() { Random cards = new Random(); int numberx = 0; for (int counter = 1; counter <

所以我在做一个纸牌游戏,它几乎就像石头剪刀一样。这是规则,皇帝打公民,公民打奴隶,奴隶打皇帝。一方有4个公民和一个奴隶,另一方有4个公民和一个皇帝。我已经为每张牌设置了相等的数值,但由于某些原因,我似乎无法在不使用1来扮演皇帝的情况下使程序继续

public static void emperorsTurn() {
    Random cards = new Random();
    int numberx = 0;
    for (int counter = 1; counter <= 3; counter++) {
        numberx = 1 + cards.nextInt(5);
    }

    Scanner sc = new Scanner(System.in);
    System.out.println("Please pick the card you are playing. \n 
                        if you are playing the Emperor press 1, 
                        if you are playing the citizen press 2");
    int eOS = sc.nextInt(); //fix the input

    if (eOS == 1 && numberx == 2) {
        System.out.println("you have played the emperor! \n 
                            the emperor defeats the citizen");
    }
    if (eOS == 1 && numberx == 1) {
        System.out.println("you have played the emperor! \n 
                            the emperor is defeated by the slave");

        if (eOS == 2 && numberx == 1) {
            System.out.println("you have played the citizen, this defeats the slave");

            if (eOS == 2 && numberx == 2) {
                System.out.println("you have played the citizen, this ties with the citizen");

                if (eOS == 2 && numberx == 3) {
                    System.out.println("you have played the citizen, this defeats the slave"); 
                }
            }
        }
    }
}
publicstaticvoidempersturn(){
随机卡片=新随机();
整数x=0;

对于(int counter=1;counter,这是因为这部分代码(注释)永远不会执行,因为eOs必须始终为1。如果eOs是任何其他数字,则if条件将始终失败:

if (eOS == 1 && numberx == 1) {
        System.out.println("you have played the emperor! \n the emperor 
 is defeated by the slave");

        /*if (eOS == 2 && numberx == 1) {
            System.out.println("you have played the citizen, this 
 defeats the slave");

            if (eOS == 2 && numberx == 2) {
                System.out.println("you have played the citizen, this 
 ties with the citizen");


                if (eOS == 2 && numberx == 3) {
                    System.out.println("you have played the citizen, 
 this defeats the slave");*/


 }}}}}
要实现所需,必须将代码重写为:

if (eOS == 1 && numberx == 1) {
        System.out.println("you have played the emperor! \n the emperor 
 is defeated by the slave");

}
else if (eOS == 2) 
{
        if (numberx == 1) {
             System.out.println("you have played the citizen, this 
             defeats the slave");
        }
        else if (numberx == 2) {
             System.out.println("you have played the citizen, this 
             ties with the citizen");
        }

        else if (numberx == 3) {
              System.out.println("you have played the citizen, 
           this defeats the slave");
        }
        else
        {
             //print out something else if number is not 1,2 or 3
        }
}
或者您可以通过以下方式进行:

switch(eOs)
{
     case 1:
         if(numberx == 1) {
             System.out.println("you have played the emperor! \n the emperor 
            is defeated by the slave");
         }
       break;
     case 2:
        if (numberx == 1) {
             System.out.println("you have played the citizen, this 
             defeats the slave");
        }
        else if (numberx == 2) {
             System.out.println("you have played the citizen, this 
             ties with the citizen");
        }

        else if (numberx == 3) {
              System.out.println("you have played the citizen, 
           this defeats the slave");
         }
        else
        {
             //print out something else if number is not 1,2 or 3
        }
       break;
}
编辑

此外,我建议,如果你打算将

  if(eOS ==1 && numberx== //some other values apart from 1)
  {

  }

因为numberx是随机的,可以有除1以外的其他值,所以可以对其进行分解,如下所示:

   if (eOS == 1) {
       if(numberx==1)
       {
            //print out something; 
       } 

       else if(numberx==//another value e.g. 2)
       {
           //print out something else;
       }

       else{
           //
       }
    }
    else if(eOS==2)//the rest of the code

不客气。如果答案解决了您面临的问题,您也可以勾选接受。您在
if(eOS==1&&numberx==1)中编写的代码{
没有任何意义,该块中所有嵌套的
if
块将永远不会执行,因为它要求
eOS
在始终
1
的位置为
2