我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*P2Q6
*Name:Wang Chaoran
*Description:6 (Using the console input)
Rewrite Listing 3.2, LeapYear.java, using the console input.
1 import javax.swing.JOptionPane;
2
3 public class LeapYear {
4 public static void main(String args[]) {
5 // Prompt the user to enter a year
6 String yearString = JOptionPane.showInputDialog("Enter a year");
7
8 // Convert the string into an int value
9 int year = Integer.parseInt(yearString);
10
11 // Check if the year is a leap year
12 boolean isLeapYear =
13 (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
14
15 // Display the result in a message dialog box
16 JOptionPane.showMessageDialog(null,
17 year + " is a leap year? " + isLeapYear);
18 }
19 }
*/
import java.util.Scanner;
public class P2Q6
{
public static void main(String[] args)
{
//Create a scanner
Scanner scan = new Scanner(System.in);
// Prompt the user to enter a year
String yearString = scan.next();
// Convert the string into an int value
int year = Integer.parseInt(yearString);
boolean isLeapYear =
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
// Display the result in a console
System.out.println(year + " is a leap year? " + isLeapYear);
}
}
导入论坛查看(30)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing