public class Snake {
// panel width and height
static int pW;
static int pH;
static int x = 10;
static int y = 10;
static int k;
static JFrame frame = new JFrame("SNAKE");
// getters for panel class
public int getPW() { return pW; }
public int getPH() { return pH; }
public int getX() { return x; }
public int getY() { return y; }
public static void main(String[] args) {
// get screen dimensions
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int sH = (int) screenSize.getHeight();
int sW = (int) screenSize.getWidth();
pW = (int) sW/2;
pH = (int) sH/2;
// initialize frame
frame.setSize (pW/1,pH/1);
frame.setLocation(pW/2,pH/2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent e) {
k = e.getKeyCode();
switch(k) {
case 38: /* y -= square size */ break; // up
case 40: /* y += square size */ break; // down
case 37: /* x -= square size */ break; // left
case 39: /* x += square size */ break; // right
case 27: System.exit(0);
}
}
});
Panel panel = new Panel();
frame.add(panel);
frame.setVisible(true);
}
}
class Panel extends JPanel {
Snake snake = new Snake();
//square size and separation between squares
int sep = 0;
int size = 50;
// initial location of square on the panel/frame
int x = sep + size;
int y = sep + size;
// holding values to check if x or y have changed
int xH = x;
int yH = x;
public void paint(Graphics g) {
int pW = snake.getPW();
int pH = snake.getPH();
int i; int o;
Color on = Color.BLACK;
Color off = Color.GRAY;
// gray background
g.setColor(Color.GRAY);
g.fillRect(0,0,pW,pH);
// black square initialization
g.setColor(Color.BLACK);
g.fillRect(x, y, size, size);
/* this loop is supposed to check if the black
* rectangle has moved by repeatedly grabbing x & y
* values from the Snake class. When a key is pressed
* and the values change, a gray rectangle is placed at the old location
* and a black one is placed at the new location.
*
* When I run the program, I get stuck in this while loop.
* If I had the while loop in the same class I check for keys,
* I don't think I would have this problem
*/
while(true) {
x = snake.getX();
y = snake.getY();
if(x != xH || y != yH) {
g.setColor(off);
g.fillRect(xH, yH, size, size);
g.setColor(on);
g.fillRect(snake.getX(), snake.getY(), size, size);
xH = x;
yH = y;
}}
}
}
目前我收到RunTime错误。我的目标是使用DateTime.Now.Year()测试当前年份是否为闰年。我认为问题是我没有正确地将年份转换为int?请指教。
答案 0 :(得分:12)
您可以使用DateTime.IsLeapYear():
yargs
答案 1 :(得分:1)
对于那些遵循规则的人:
根据wikipedia,这些额外的日子每年发生
因此这将导致以下功能:
// PRE: no year < 1 or > 9999
// POST: true if year is a leap year, or false if not.
public static bool IsLeapYear(int year)
{
if (year < 1 || year > 9999)
{
throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_Year"));
}
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
现在我想知道如果我在卑诗省使用减年-会发生什么?-实际上,问题是哪一个日历甚至甚至适用于1753年(对于英国)? ;)
答案 2 :(得分:0)
我使用 C# 和这段代码来表示闰年。 干杯
if ((jahr % 4 == 0 && jahr % 400 == 0) || (jahr % 4 == 0 && !(jahr % 100 == 0)))
{
Console.WriteLine(jahr + " ist ein Schaltjahr");
}
else
Console.WriteLine(jahr + " ist kein Schaltjahr");
答案 3 :(得分:-1)
static int GeveDays()
{
int days;
if ((DateTime.Now).Year / 4 != 1 || (DateTime.Now).Year / 400 != 1) {
Console.WriteLine("it is a common year");
days = 365;
return days;
}
else if ((DateTime.Now).Year / 100 != 1) {
Console.WriteLine("it is a leap year");
days = 366;
return days;
}
else {
Console.WriteLine("it is a leap year");
days = 366;
return days;
}
}
答案 4 :(得分:-2)
年财产所以不需要括号......
答案 5 :(得分:-2)
javapackager -deploy -native -Bcategory=Education -BjvmOptions=-Xmx128m
-BjvmOptions=-Xms128m -outdir packages -outfile BrickBreaker -srcdir dist
-srcfiles BrickBreaker.jar -appclass brickbreaker.Main -name BrickBreaker
-title "BrickBreaker demo"
Thanks,
Gurnur Singh