目录
一、简易计算器
二、购买电影票
三、计算平均分
四、厘米与英寸互转
使用静态方法模拟一个只能进行两个数加、减、乘、除的建议计算器。
package haha;
import java.util.Scanner;
public class Dog{
public static double jia(double a,double b)
{
double c;
c=a+b;
return c;
}
public static double jian(double a,double b)
{
double c;
c=a-b;
return c;
}
public static double cheng(double a,double b)
{
double c;
c=a*b;
return c;
}
public static double chu(double a,double b)
{
double c;
c=a/b;
return c;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("请输入两个数字,用空格隔开(a b):");
double a=sc.nextDouble();
double b=sc.nextDouble();
System.out.println(a+"加上"+b+"的结果:"+jia(a,b));
System.out.println(a+"减去"+b+"的结果:"+jian(a,b));
System.out.println(a+"乘上"+b+"的结果:"+cheng(a,b));
System.out.println(a+"除以"+b+"的结果:"+chu(a,b));
}
}
购买电影票有优惠:满十八周岁的付40元,未满十八周岁的享受半价。使用成员变量、成员方法、构造方法和this关键字,控制台输出如下所示的姓名、年龄票价等信息。
姓名 年龄 票价(元)
李明 20 40
钱丽 16 20
周刚 8 20
吴红 32 40
package haha;
public class Dog{
String name;
int age;
int price;
public Dog(String name,int age,int price) {
this.name=name;
this.age=age;
this.price=price;
}
public static int Price(int age) {
int price;
if(age<18)
{
price=20;
}
else
{
price=40;
}
return price;
}
public static void main(String[] args) {
System.out.println("姓名"+" "+"年龄"+" "+"票价(元)");
Dog s1=new Dog("李明",20,Price(20));
System.out.println(s1.name+" "+s1.age+" "+s1.price);
Dog s2=new Dog("钱丽",16,Price(16));
System.out.println(s2.name+" "+s2.age+" "+s2.price);
Dog s3=new Dog("周刚",8,Price(8));
System.out.println(s3.name+" "+s3.age+" "+s3.price);
Dog s4=new Dog("吴红",32,Price(32));
System.out.println(s4.name+" "+s4.age+" "+s4.price);
}
}
使用成员变量、成员方法、构造方法和this关键字,先记录4名学生的语文、数学、英语这3科成绩,再计算每个人的平均分。运行结果如下:
学号 姓名 语文 数学 英语 平均分
—————————————————————————————
1 张三 91.5 98.0 89.0 92.83333333333333
2 李四 96.0 98.5 93.0 95.83333333333333
3 王五 97.0 100.0 98.5 98.5
4 钱六 77.0 83.0 81.0 80.33333333333333
package haha;
public class Dog{
String name;
static int id=0;
double chinese;
double math;
double english;
public Dog(String name,double chinese,double math,double english) {
this.name=name;
this.chinese=chinese;
this.math=math;
this.english=english;
id++;
}
public static double Avg(double chinese,double math,double english) {
double avg=(chinese+math+english)/3;
return avg;
}
public static void main(String[] args) {
System.out.println("学号"+" "+"姓名"+" "+"语文"+" "+"数学"+" "+"英语"+" "+"平均分");
System.out.println("——————————————————————————————————————————————————————————————————————");
Dog s1=new Dog("张三",91.5,98,89);
System.out.println(s1.id+" "+s1.name+" "+s1.chinese+" "+s1.math+" "+s1.english+" "+Avg(s1.chinese,s1.math,s1.english));
Dog s2=new Dog("李四",96,98.5,93);
System.out.println(s2.id+" "+s2.name+" "+s2.chinese+" "+s2.math+" "+s2.english+" "+Avg(s2.chinese,s2.math,s2.english));
Dog s3=new Dog("王五",97,100,98.5);
System.out.println(s3.id+" "+s3.name+" "+s3.chinese+" "+s3.math+" "+s3.english+" "+Avg(s3.chinese,s3.math,s3.english));
Dog s4=new Dog("钱六",77,83,81);
System.out.println(s4.id+" "+s4.name+" "+s4.chinese+" "+s4.math+" "+s4.english+" "+Avg(s4.chinese,s4.math,s4.english));
}
}
编写工具类,提供厘米与英寸之间的相互转换的工具方法。
package haha;
import java.util.Scanner;
public class Dog{
public static double cmtoin(double a) {
double b=0.393700788*a;
return b;
}
public static double intocm(double a) {
double b=2.5399999961392*a;
return b;
}
public static void main(String[] args) {
System.out.println("厘米转换成英寸请输入1,英寸转换为厘米请输入2:");
Scanner sc=new Scanner(System.in);
double n=sc.nextDouble();
if(n==1)
{
System.out.println("请输入需要转换的厘米数:");
double a=sc.nextDouble();
System.out.println(a+"cm="+cmtoin(a)+"in");
}
else if(n==2)
{
System.out.println("请输入需要转换的英寸数:");
double a=sc.nextDouble();
System.out.println(a+"in="+intocm(a)+"cm");
}
else
{
System.out.println("输入无效!");
}
}
}