使用java.util.Random从另一个中减去一个interger

时间:2018-06-19 08:54:38

标签: operators

我试图从1-100范围内随机生成的另一个使用Scanner减去一个整数输入但是当我尝试减去时这两个数字被认为是随机的,我尝试的时候得到的答案是-1044612689,消极或积极的。到目前为止我的代码是:

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

public class RandomNumber {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int number, number2, answer;

        System.out.println("I'm thinking of a number between 1 and 100");
        System.out.println("(including both). Can you guess what it is? ");      
        Random random = new Random();
        Scanner in = new Scanner(System.in);

        System.out.print("Type a number ");
        number = in.nextInt(); 

        System.out.println("Your guess is " + number);
        number = random.nextInt(); 

        System.out.println("The number I was Thinking of is: ");
        number2 = random.nextInt(100) + 1;
        System.out.println(number2);

        answer = (number - number2);
        System.out.print(answer);          
    }
}

这一切都有效,直到我试图在结尾处减去2个数字,我完全感到困惑。我想坚持使用java.util.Scanner和java.util.Random,因为这是一项任务。 谢谢

0 个答案:

没有答案