package Endless;
import java.util.Random;
import java.util.Scanner;
public class Task {
public static void main(String[] args) {
Start();
}
@SuppressWarnings({ "resource" })
private static void Start() {
System.out.println("请选择要进入的程序:0,退出程序\n1,查询某月天数\n2,打印水仙花数\n3,猜数字\n4,数列求和1\n5,数列求和2"
+ "\n6,打印乘法口诀\n7,打印矩形\n8,打印平行四边形\n9,打印三角形");
Scanner in =new Scanner(System.in);
int x = 0;
try {
x=in.nextInt();
} catch (Exception e) {
Start();
}
switch (x) {
case 0:
System.exit(0);
break;
case 1:
month();
case 2:
narcissus();
break;
case 3:
bulls();
break;
case 4:
sum99();
break;
case 5:
Seq();
break;
case 6:
seq99();
case 7:
printjx();
break;
case 8:
printsbx();
break;
case 9:
printsjx();
break;
default:
System.out.println("没有此选项");
Start();
break;
}
}
private static void printsjx() {
int n=9;
for(int x=0;x<=n;x++){
for(int y =0;y<n-x;y++){
System.out.print(" ");
}
for(int z =1;z<=x;z++){
System.out.print("* ");
}
System.out.println();
}
Start();
}
private static void printsbx() {
for(int x=0;x<5;x++){
for(int y =1;y<9;y++){
System.out.print("*");
}
System.out.println();
}
Start();
}
private static void printjx() {
for(int x=0;x<5;x++){
for(int y =1;y<=x;y++){
System.out.print(" ");
}
for(int y =1;y<10;y++){
System.out.print("*");
}
System.out.println();
}
Start();
}
private static void seq99() {
for(int x=0;x<=9;x++){
for(int y=1;y<=x;y++){
System.out.print(y+"*"+x+"="+x*y+"\t");
}System.out.println();
}Start();
}
@SuppressWarnings("resource")
private static void Seq() {
System.out.println("计算数列1+1/2+1/3…+1/n(n>=2)的和");
System.out.println("请输入你要求此数列的前几项和:");
Scanner in = new Scanner(System.in);
double x=in.nextInt();
double sum=0;
for(double i=1;i<=x;i++){
sum=sum+(1/i);
System.out.println(1/i);
}
System.out.println("此数列的和为"+sum);
Start();
}
@SuppressWarnings("resource")
private static void sum99() {
Scanner in = new Scanner(System.in);
System.out.println("数列n,nnn,nnnn...求和");
System.out.println("请输入n:");
long n= in.nextInt();
System.out.println("请输入你要求此数列的前几项和:");
int x=in.nextInt();
long sum = 0;
long s = 0;
for(int i=1;i<=x;i++){
sum=sum*10+n;
s=(s+sum);
System.out.println(sum);
}
System.out.println("此数列的和为"+s);
Start();
}
@SuppressWarnings("resource")
private static void bulls() {
int x=new Random().nextInt(99)+1;
System.out.println("----猜数字----\n请输入:");
int n = 0;
int i = 0;
while (true) {
Scanner in = new Scanner(System.in);
n=in.nextInt();
i++;
if(n==111){
Start();
}else if (n>x) {
System.out.println("猜大了");
}else if(n==x) {
System.out.println("恭喜你在第"+i+"次猜对了!");
Start();
}else{
System.out.println("猜小了");
} System.out.println("请继续猜 ,输入111退出");
}
}
private static void narcissus() {
System.out.println("0-999之间的水仙花数:");
for(int i=100;i<=999;i++){
int x = i%10;
int y = (i/10)%10;
int z = (i/100)%10;
int num=(int) (Math.pow(x, 3)+Math.pow(y, 3)+Math.pow(z, 3));
if(i==num){
System.out.println(i);
}
}
System.out.println("---------------------");
Start();
}
@SuppressWarnings("resource")
private static void month() {
while(true){
int year = 0;
int month = 0;
int day = 0;
Scanner in =new Scanner(System.in);
try {
System.out.println("请输入年份:");
year=in.nextInt();
if(year==0){
Start();
}
System.out.println("请输入月份:");
month=in.nextInt();
if(month==0){
Start();
}
} catch (Exception e) {
System.out.println("请输入数字");
month();
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0){
day = 29;
}else {
day=28;
}
break;
default:
System.out.println("没有此月份(1-12)");
month();
break;
}
System.out.println(year+"年"+month+"月有"+day+"天\n输入0返回上级");
}
}
}
package Endless;
import java.util.Scanner;
/**
* @JAVA06.java1100sum.java
* @author 当前用户:Endless作者 :Endless
* @version 创建时间:2017年8月1日 上午10:19:49
* 实现计算1~100之间的和,要求分别使用while循环、do-while循环和for循环三种形式实现
*/
public class java100sum {
public static void main(String[] args) {
start();
}
@SuppressWarnings("resource")
private static void start() {
try {
System.out.println("选择计算方式:1,while 2,do-while 3,for");
Scanner in = new Scanner(System.in);
int x = in.nextInt();
if(x==1){
whiles();
}else if (x==2) {
dowhile();
}else if (x==3) {
For();
}else {
System.out.println("没有此选项");
start();
}
} catch (Exception e) {
System.out.println("请输入数字");
start();
}
}
private static void out(int sum){
System.out.println("1-100之和为"+sum);
}
private static void For() {
int sum=0;
for(int i=1;i<=100;i++){
sum=sum+i;
}
out(sum);
start();
}
private static void dowhile() {
int i=1;
int sum=0;
do{
sum=sum+i;
i++;
}
while(i<=100);
out(sum);
start();
}
private static void whiles() {
int i=1;
int sum=0;
while(i<=100){
sum=sum+i;
i++;
}
out(sum);
start();
}
}