我想比较彩票计划的2位数字
,2位是随机的
所以我写了这段代码:
package home6;
import java.util.Scanner;
public class Lottery {
public static void main(String[] arga) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (two digits): ");
int guess = input.nextInt();
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
int lottery = (int) Math.random();
System.out.println("The lottery number is " + lottery);
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
if (guess == lottery) {
System.out.println("Match all digits: you win $10,000");
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win $3,000");
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win $1,000");
} else {
System.out.println("Sorry, no match");
}
}
}
在第17行,// int lottery = (int) Math.random();
,问题是什么?
如何随机抽奖?
这是我的问题。
答案 0 :(得分:1)
您想要一个0到99之间的数字:
int lottery = (int) (Math.random() * 100);
答案 1 :(得分:1)
Math.random()
返回0.0~1.0之间的结果。所以你把它投到Integer
,总是为0.如果你想要抽奖号码是0~100,那就把它(int) (Math.random()*100)
。
答案 2 :(得分:0)
通常的方法是:
Random random = new Random();
public void test() {
int zeroToNinetyNine = random.nextInt(100);
}
答案 3 :(得分:0)
import java.util.Scanner;
public class Lottery {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your two lucky Digits:");
int think = input.nextInt();
int thinkdigit1 = think / 10;
int thinkdigit2 = think % 10;
int casino= (int) (Math.random() * 100);
System.out.println("The lottery number is:" +casino);
int casinonumber1= casino / 10;
int casinonumber2 = casino % 10;
if (think==casino)
{
System.out.println("Perfact match!. you won $10,000");
}
else if (thinkdigit1==casinonumber2 && thinkdigit2==casinonumber1)
{
System.out.println("Match Both Digit: you won $3000");
}
else if (thinkdigit1==casinonumber1||thinkdigit1==casinonumber2||thinkdigit2==casinonumber1||thinkdigit2==casinonumber2)
{
System.out.println("Only One Digit matched: you won $1000");
}
else { System.out.println("Sorry!! No match! Better Luck Next Time!! you can't try again!");}
}
}