首页 > 代码库 > day1015

day1015

厘米与英寸的相互转换:

package com.lovo;import java.util.Scanner;public class Test1 {	public static void main(String[] agrs){		Scanner sc = new Scanner(System.in);		double Limi;		System.out.print("请输入:");
         //输入需要转换的数额 Limi = sc.nextDouble();
         //输入单位 String str = sc.next();
         //判断单位是否是cm,若是则进行运算 if (str.equals("cm")){ System.out.printf("%.2f厘米=%.2f英寸", Limi, Limi/2.54); }
         //判断单位是否是inch,若是则进行下面运算  else if (str.equals("inch")){ System.out.printf("%.2f英寸=%.2f厘米", Limi, Limi*2.54); }
         //输入不正确显示无法计算 else{ System.out.println("无法计算"); } }}

  

package com.lovo;import javax.swing.JOptionPane;public class Test1 {	private static final double changliang = 2.54;	public static void main(String[] agrs) {
         //在第一个指令框输入字符串 String li = JOptionPane.showInputDialog("请输入:");
         //在第二个指令框输入字符串 String str = JOptionPane.showInputDialog("请输入:");
         //将第一个字符串转化为Double型的数 double Limi = Double.parseDouble(li);         //判断第二个字符串是否为cm,如果是进行下面运算 if (str.equals("cm")) { JOptionPane.showMessageDialog(null,String.format("%.2f厘米=%.2f英寸", Limi, Limi / changliang)); }
         //判断第二个字符串是否为inch,如果是进行下面运算
          else if (str.equals("inch")) { JOptionPane.showMessageDialog(null,String.format("%.2f英寸=%.2f厘米", Limi, Limi * changliang)); }
          else { JOptionPane.showMessageDialog(null, "瓜西西的!"); } }}

  指令框的两个数之和package com.lovo;

import javax.swing.JOptionPane;public class Test2 {	public static void main(String[] args) {
String num1 = JOptionPane.showInputDialog("输入第一个数"); String num2 = JOptionPane.showInputDialog("输入第二个数"); String num3 = JOptionPane.showInputDialog("输入第三个数"); String num4 = JOptionPane.showInputDialog("输入第四个数"); int a = Integer.parseInt(num1); int b = Integer.parseInt(num2); double c = Double.parseDouble(num3); double d = Double.parseDouble(num4); JOptionPane.showMessageDialog(null, a+b); JOptionPane.showMessageDialog(null,String.format("%.2f", c+d )); }}

  判断是否为闰年

package com.lovo;import java.util.Scanner;public class Test3 {	public static void main(String[] args) {		System.out.print("请输入年:");		Scanner sc = new Scanner(System.in);		double year = sc.nextDouble();		if (year % 400 == 0 || (year % 4 == 0 && year % 40 != 0)){			System.out.println("是润年");		}		else{			System.out.println("不是润年");		}		sc.close();	}}

  

package com.lovo;import javax.swing.JOptionPane;public class Test4 {	public static void main(String[] args) {		String year = JOptionPane.showInputDialog("请输入年:");		int Year = Integer.parseInt(year);		if (Year % 400 == 0 || (Year % 4 == 0 && Year % 40 != 0)){			JOptionPane.showMessageDialog(null,year+"是润年");		}		else{			JOptionPane.showMessageDialog(null,year+"不是润年");		}	}}

  

day1015