谁能帮忙注释一下这个代码每一行是什么意思啊,关于入门java的

public class SnakeEyes { public static void main (String[] args) { final int ROLLS = 500; int num1, num2, count = 0; Die die1 = new Die();《=====其实主要是不明白这两句是干啥的=。= Die die2 = new Die(); for (int roll=1; roll <= ROLLS; roll++) { num1 = die1.roll();还有这句 num2 = die2.roll(); if (num1 == 1 && num2 == 1) count++; } System.out.println ("Number of rolls: " + ROLLS); System.out.println ("Number of snake eyes: " + count); System.out.println ("Ratio: " + (float)count / ROLLS); } }

第1个回答  2019-05-04
public
class
SnakeEyes
//我没学过JAVA
但学C#
凑合的解释给你听吧,
一看就是PUBLIC全局类
{
public
static
void
main
(String[]
args)
//
static
静态修饰,可以不用实列化调用
内有一参数为字符串型
{
/
final
int
ROLLS
=
500;
//
final
定义后锁的意思
ROLLS
不能修改值
int
num1,
num2,
count
=
0;
Die
die1
=
new
Die();
这里肯定很明显实列化一个类的对象
定义为
为die1
然后
die1就可以调用
该类DIE的方法

属性
比如
die1.new();
Die
die2
=
new
Die();
下面又定义了一个,但是DIE1

DIE2
都属于一个对象那边实列过来的,他们的方法一样,但是不冲突,各归各用。
for
(int
roll=1;
roll
<=
ROLLS;
roll++)
{
num1
=
die1.roll();还有这句
把DIE1实列化后调用的方法ROLL赋值给NUM1
num2
=
die2.roll();
一样
if
(num1
==
1
&&
num2
==
1)
count++;
}
System.out.println
("Number
of
rolls:
"
+
ROLLS);
System.out.println
("Number
of
snake
eyes:
"
+
count);
System.out.println
("Ratio:
"
+
(float)count
/
ROLLS);
}
}
虽不知这段代码做什么用的,但感觉代码写的不怎么。
相似回答