找不到适合的随机构造函数(int,int)

时间:2015-04-18 07:53:04

标签: java

我在unix中制作一个基本的猜谜游戏,我从1到10的可能数字中随机选择一个数字,并且用户有三次机会在“失败”或“获胜”之前输入猜测。我对使用unix相对较新,更不用说vim了,所以如果我能得到一些非常感激的帮助。 我不断收到这些错误:“no suitable constructor found for Random(int, int)”,以及error: cannot find symbolguess, secGuess”和thirGuess,即使我已经实例化了这些错误。我不知道我做错了什么。这是我的java代码:

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

class Guess {
    public static void main (String[] args) {
        Random num = new Random(1,10);
        int value = num.nextInt(10);
        Scanner sc = new Scanner(System.in);
        int guess;
        int secGuess;
        int thirGuess;

        System.out.println("guess a number between 1 and 10");
        int tryCount = 0;


        guess = input.nextInt();
        if(guess == value) {
            System.out.println("Yay! You win!");
            noLoop();
        }
        if(guess > value) {
            System.out.println("Nope! Your number is too high, son!");
            tryCount++;
        }
        if(guess < value) {
            System.out.println("Nope! That number is too low!");
            tryCount++;
        }


        secGuess = input.nextInt();
        if(secGuess == value) {
            System.out.println("Yay! You win!");
        }
        if(secGuess > value) {
            System.out.println("Nope! Your number is too high, son!");
            tryCount++;
        }
        if(secGuess < value) {
            System.out.println("Nope! That number is too low!");
            tryCount++;
        }

        thirGuess = input.nextInt();
        if(thirGuess == value) {
            System.out.println("Yay! You win!");
            noLoop();
        }
        if(thirGuess > value) {
            System.out.println("Nope! Your number is too high, son!");
            tryCount++;
        }
        if(thirGuess < value) {
            System.out.println("Nope! That number is too low!");
            tryCount++;
        }

        if(tryCount>3) {
            System.out.println("You've exceeded the number of tries. Sorry, but you lose!");
        } 
    }
}

2 个答案:

答案 0 :(得分:0)

没有带两个参数的Random构造函数。替换

Random num = new Random(1,10);

Random num = new Random(); 

guess = input.nextInt();中的下一个错误。名称为input的范围内没有任何内容。我想你的意思是sc。有三个这样的错误。

最后一个noLoop();。解决这个问题很棘手,因为我不知道你的意思。但可以试着猜测你想要结束你的程序。有两种方式returnSystem.exit(0)

答案 1 :(得分:0)

您也可以尝试这样做

Random num = new Random(); 
int value = num.nextInt() % 10;

此外,您可以循环测试3次而不是写3次。像这样 -

while(tryCount<3){
        guess = input.nextInt() % 10;;
        if(guess == value) {
            System.out.println("Yay! You win!");
            break;
        }
        if(guess > value) {
            System.out.println("Nope! Your number is too high, son!");
            tryCount++;
        }
        if(guess < value) {
            System.out.println("Nope! That number is too low!");
            tryCount++;
        }
}