所以我必须使用 Math.random(); 创建三个随机数。方法然后使用 for 循环自己输出每个数字,然后使用连接将它们变成一个字符串。是为了我的任务。我已经完成了其他所有事情。我可以用不同的方式来做,但它需要一个 for 循环。我不能使用 Random 类。我在我的课程中没有学过它,因此不能应用它。
public class Lottery
{
public static void main(String[] args)
{
//declare and initialized variables and objects
Scanner input = new Scanner(System.in);
//Identify the repeated steps and use a for loop structure
for(int i=0; i < 3; i++)
{
double lotto = Math.random();
int lotteryNumberDigit = (int)(lotto*10);
String lotteryNumberString = Integer.toString(lotteryNumberDigit);
}
String firstNumber = lotteryNumberString.substring(0,0);
String secondNumber = lotteryNumberString.substring(1,1);
String thirdNumber = lotteryNumberString.substring(2,2);
String firstTwoWinner = firstNumber + secondNumber;
String lastTwoWinner = secondNumber + thirdNumber;
String allNumbersWinner = firstNumber + secondNumber + thirdNumber;
System.out.println("Please enter your three numbers (e.g. 123): ");
String userInput = input.next();
if(userInput.substring(0,2).equals(firstTwoWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, the front pair matched.");
}
else if (userInput.substring(1,3).equals(lastTwoWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, the end pair matched.");
}
else if (userInput.equals(allNumbersWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, both pairs matched.");
}
else
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Sorry, no matches. You only had one chance out of 100 to win anyway.");
}
}
}
我不得不取出阵列,因为我的老师说直到下一章才能预料到。