我刚刚开始介绍Java编程课程,所以我并不了解。我在获取彩票变量的中间数字时遇到麻烦。我使用int lotteryDigit1 = lottery / 100;
和int lotteryDigit2 = lottery % 100;
来获取第一个和最后一个数字,但是我很难获取中间的数字。
import java.util.Scanner;
public class Lottery {`enter code here`
public static void main(String[] args) {
//generate random lottery numbers
int lottery = (int)(Math.random() * 100);
//ask user to enter a 3 digit guess
Scanner numbers = new Scanner(System.in);
System.out.println("Please enter your lottery pick. Three Digits Only");
int guess = numbers.nextInt();
//digits for lottery
int lotteryDigit1 = lottery / 100;
int lotteryDigit2 = lottery % 100;
//digits for guess
int guessdigit1 = guess / 100;
int guessdigit2 = guess % 100;
最佳答案
试试这个代码
int lottery = 123;
System.out.println(lottery);
System.out.println(lottery % 10);
System.out.println(lottery % 100 / 10);
System.out.println(lottery / 100);
输出
123
3
2
1