Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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 为什么赢了';这三条语句不是都打印出来的吗?_Java - Fatal编程技术网

Java 为什么赢了';这三条语句不是都打印出来的吗?

Java 为什么赢了';这三条语句不是都打印出来的吗?,java,Java,我们正在学习选择,必须为石头、布、剪刀游戏编写一个程序。我的程序运行时没有错误,但它只打印“计算机是。你是。”我不明白为什么它不会打印你赢/你输/这是平局声明 // Solution for exercise 3.17 import java.util.Scanner; public class PA6c { public static void main(String[] args) { Scanner input = new Scanner(System.in);

我们正在学习选择,必须为石头、布、剪刀游戏编写一个程序。我的程序运行时没有错误,但它只打印“计算机是。你是。”我不明白为什么它不会打印你赢/你输/这是平局声明

// Solution for exercise 3.17
import java.util.Scanner;

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

    Scanner input = new Scanner(System.in);

    // Get user input for number associated with rock, paper, or scissor
    System.out.print("Enter a number 0-2: ");

    int userNumber = input.nextInt();

    // Generate random number for computer
    int compNumber = (int)(Math.random() * 10);

    // Declare rock, paper, and scissor
    int scissor = 0;
    int rock = 1;
    int paper = 2;

    // Determine what the computer is
    if (compNumber == scissor) {
        System.out.print("The computer is scissor. ");
    }
    else if (compNumber == rock) {
        System.out.print("The computer is rock. ");
    }
    else {
        System.out.print("The computer is paper. ");
    }

    // Determine what the user is
    if (userNumber == scissor) {
        System.out.print("You are scissor. ");
    }
    else if (userNumber == rock) {
        System.out.print("You are rock. ");
    }
    else {
        System.out.print("You are paper. ");
    }

    // Determine who won
    if (compNumber == scissor) {
        if (userNumber == scissor) {
            System.out.println("It is a draw.");
        }
        else if (userNumber == rock) {
            System.out.println("You win.");
        }
        else if (userNumber == paper) {
            System.out.println("You lose.");
        }
    }
    else if (compNumber == rock) {
        if (userNumber == scissor) {
            System.out.println("You lose.");
        }
        else if (userNumber == rock) {
            System.out.println("It is a draw.");
        }
        else if (userNumber == paper) {
            System.out.println("You win.");
        }
    }
    else if (compNumber == paper) {
        if (userNumber == scissor) {
            System.out.println("You win.");
        }
        else if (userNumber == rock) {
            System.out.println("You lose.");
        }
        else if (userNumber == paper) {
            System.out.println("It is a draw.");
        }
        }
    }
}

问题在于您试图获取随机数的方式。你需要你的随机数在任何时候
{0,1,2}
,但是
数学.random()*10
会给你范围
{0,1,2,3,4,5,6,7,8,9}
,所以你的目的在这里失败了。因此,请确保您的
compNumber
始终是
{0,1,2}
中的一个

用这个替换随机数生成行

Random rand = new Random();
compNumber = rand.nextInt(0 , 3); // will give you 0 or 1 or 2

compNumber
不能是1、2或3。调试以查看它是什么。它是compNumber!我将(Math.random()*10)更改为(0+Math.random()*2),它工作正常。谢谢大家!<代码>整数compNumber=(int)(Math.random()*10)给您一个编号
0
9
@P夫人,不客气。你需要学会调试。这是相当明显的只是看看它。没有冒犯的意思,只是一些值得关注的事情。调试是一项至关重要的技能。我会记住这一点,谢谢。这是我第一个学期参加任何编程课程,所以我是一个超级高手初学者。我的教授建议我在Mac电脑上使用TextWrangler而不是TextPad(我们在课堂上使用的),我不知道如何在上面使用调试器。谢谢!我们根本没有使用过随机生成,我们唯一一次使用的是Math.random()*10,所以我想这就是为什么它一直萦绕在我的脑海中。如果答案有助于你,请投票并接受它作为答案,以帮助他人。