1.
创建类
public class MobilPhone {
public String brand;
public MobilPhone ( ) {
this . brand= "诺基亚" ;
}
public MobilPhone ( String bra) {
this . brand= bra;
}
public String buy ( ) {
return "没发工资, 买一个" + brand+ "牌子的手机吧!" ;
}
public String buy ( String reason) {
return reason+ ", 快买一个" + brand+ "牌子的手机吧!" ;
}
}
引用类
public class MobilPhoneTest {
public static void main ( String[ ] args) {
MobilPhone mp = new MobilPhone ( ) ;
mp. brand= "苹果" ;
String detail= mp. buy ( "发工资啦" ) ;
System. out. println ( detail) ;
}
}
运行:
发工资啦, 快买一个苹果牌子的手机吧!
进程已结束,退出代码为 0
2.
类
public class One {
static int year;
static double a;
static double b;
static double c;
public double i ( double i, int yearchice) {
if ( yearchice== 1 ) {
a= 0.0603 ;
year= 36 ;
c= i* a;
} else if ( yearchice== 2 ) {
a= 0.0612 ;
year= 60 ;
c= i* a;
} else if ( yearchice== 3 ) {
a= 0.0639 ;
year= 240 ;
c= i* a;
}
b= ( i+ c) / year;
System. out. println ( "***月供为:" + b) ;
return b;
}
}
引用
public class Two {
public static void main ( String[ ] args) { One money= new One ( ) ; Scapublic class Two {
public static void main ( String[ ] args) {
One money= new One ( ) ;
Scanner input= new Scanner ( System. in) ;
System. out. println ( "请输入贷款金额:" ) ;
double i= input. nextDouble ( ) ;
System. out. print ( "\n请选择贷款年限:1.3年(36个月)\t2.5年(60个月)\t3.20年(240个月):" ) ;
int n= input. nextInt ( ) ;
money. i ( i, n) ;
}
}
运行
句请输入贷款金额:
100000
请选择贷款年限:1.3 年(36 个月) 2.5 年(60 个月) 3.20 年(240 个月): 3
* * * 月供为:443.2916666666667
进程已结束,退出代码为 0
3.
类
public class Triangle {
public boolean isTriangle ( int a, int b, int c) {
boolean flag= false ;
if ( ( a + b > c) && ( a + c > b) && ( b + c > a) ) {
flag= true ;
}
return flag;
}
public String shape ( int a, int b, int c) {
String shape= "" ;
if ( a+ b<= c|| a+ c<= b|| b+ c<= a) {
System. out. println ( "这不能构成三角形。" ) ;
} else if ( a== b&& b== c) {
shape= "等边三角形" ;
System. out. println ( "这是一个" + shape) ;
} else if ( a* a== b* b+ c* c || b* b== a* a+ c* c || c* c== a* a+ b* b) {
shape= "直角三角形" ;
System. out. println ( "这是一个" + shape) ;
} else if ( a* a< b* b+ c* c || b* b< a* a+ c* c || c* c< a* a+ b* b) {
shape= "锐角三角形" ;
System. out. println ( "这是一个" + shape) ;
}
else {
shape= "钝角三角形" ;
System. out. println ( "这是一个" + shape ) ;
}
return shape;
}
}
引用
public class Test {
public static void main ( String[ ] args) {
boolean i= true ;
Triangle t = new Triangle ( ) ;
String m= "" ;
Scanner input= new Scanner ( System. in) ;
do {
System. out. print ( "请输入第一个边:" ) ;
int a = input. nextInt ( ) ;
System. out. print ( "请输入第二个边:" ) ;
int b = input. nextInt ( ) ;
System. out. print ( "请输入第三个边:" ) ;
int c = input. nextInt ( ) ;
t. isTriangle ( a, b, c) ;
t. shape ( a, b, c) ;
System. out. print ( "继续吗?(y/n)" ) ;
m= input. next ( ) ;
if ( m. equals ( "n" ) ) {
System. out. println ( "谢谢使用!" ) ;
break ;
}
} while ( m. equals ( "y" ) ) ;
}
}
运行
请输入第一个边:1
请输入第二个边:1
请输入第三个边:3
这不能构成三角形。
继续吗?(y/ n)y
请输入第一个边:6
请输入第二个边:6
请输入第三个边:6
这是一个等边三角形
继续吗?(y/ n)y
请输入第一个边:6
请输入第二个边:4
请输入第三个边:3
这是一个锐角三角形
继续吗?(y/ n)n
谢谢使用!
进程已结束,退出代码为 0