1.
//***树
package d731s01;
import java.util.Scanner;
public class aster {
public static void main(String[] args){
System.out.print("请输入星树的高度");
Scanner scanner = new Scanner(System.in);
int h =scanner.nextInt();
for(int i =1;i<=h;i++){
for(int k=0;k<=h-i;k++){
System.out.print(" ");
}
for(int j =1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println("");
}
}
}
2.
//练习(平均联系)
package d731s02;
import java.util.Scanner;
public class agetest {
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int sum=0;
double avg =0;
boolean flag=true;
for(int i=0;i<5;i++){
System.out.printf("请输入第%s年纪:",i+1);
int x =sc.nextInt();
if(x<0||x>130){
System.out.println("您输入有误,无法计算");
flag =false;
break;
}
sum+=x;
}
avg =sum/5;
if(flag==true){System.out.println("平均年龄为:"+avg);}
}
}
3.
//平闰年练习
package d731s03;
import java.util.Scanner;
public class pr {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc =new Scanner(System.in);
int years =1900;
int num=0;
for(years=1900;years<=2050;years++){
int flag =0;
if(years%40){
flag =1;
if(years%1000){
flag =2;
if(years%400==0){flag =1;}
}
}
if(flag==1){
num++;
System.out.print(years+" ");
if(num%5==0){System.out.println("");}
}
}
}
}
4.//顾客登入系统
package d731s04;
import java.util.Scanner;
public class Admincust {
public static void main(String[] args) {
// TODO Auto-generated method stub
ShowMainpage();
Scanner scanner =new Scanner(System.in);
int x=scanner.nextInt();
do{
switch(x){
case 1:System.out.println("管理员登入中(登入过程省略)");
System.out.println("");
ShowMainpage();
x=scanner.nextInt();
break;
case 2:System.out.println("顾客登入中(登入过程省略)");
System.out.println("");
ShowMainpage();
x=scanner.nextInt();
break;
}
}while(x!=3);
System.out.println("系统退出");
}
//主显示函数
static void ShowMainpage(){
System.out.println("***********************");
System.out.println("1.管理员"+" "+"2.顾客"+" "+"3.退出");
System.out.println("***********************");
System.out.println("请输入数字选择登入:");
}
}
5.
//计算各种图形的面积
package d731s05;
import java.util.Scanner;
public class Calcarea {
static double π =3.1415926;
public static void main(String[] args) {
// TODO Auto-generated method stub
ShowMainPage();
Scanner scanner =new Scanner(System.in);
int x =scanner.nextInt();
do{
switch(x){
case 1:
System.out.println("");
ShowRectangle();
ShowMainPage();
x =scanner.nextInt();
break;
case 2:
System.out.println("");
ShowTriangle();
ShowMainPage();
x =scanner.nextInt();
break;
case 3:
System.out.println("");
ShowCir();
ShowMainPage();
x =scanner.nextInt();
break;
default:
System.out.println("输入有误");
System.out.println("请重新输入:");
x =scanner.nextInt();
break;
}
}while(x!=4);
System.out.println("系统退出");
}
static void ShowMainPage(){
System.out.println("计算面积");
System.out.println("1.矩形\t2.三角形 \t3.圆\t4.退出\t");
System.out.println("请选择数字对应的功能:");
}
static void ShowRectangle(){
System.out.println("请输入矩形的高");
Scanner scanner =new Scanner(System.in);
double w = scanner.nextDouble();
System.out.println("请输入矩形的宽");
double l = scanner.nextDouble();
double sum = l*w;
System.out.println("矩形的面积为:"+sum);
}
static void ShowCir(){
System.out.println("请输入圆的半径或直径");
System.out.println("1.半径\t\t2.直径");
Scanner scanner =new Scanner(System.in);
int flag =scanner.nextInt();
double cirsum=0;
double r=0.0;
if(flag==1){
System.out.println("请输入半径");
r = scanner.nextDouble();
cirsum = π*r*r;
}else{
System.out.println("请输入直径");
r = scanner.nextDouble();
cirsum = π*r*r/4;
}
System.out.println("圆的面积为:"+cirsum);
}
static void ShowTriangle(){
System.out.println("请输入三角形的高");
Scanner scanner =new Scanner(System.in);
double w = scanner.nextDouble();
System.out.println("请输入三角形的宽");
double l = scanner.nextDouble();
double sum = l*w/2;
System.out.println("三角形的面积为:"+sum);
}
}