#eclipse自动补全
#添加(表示所有字母都可以自动补全)
.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
#倒立正三角形
public class test {
public static void main(String[] args) {
int i,j;
for (i = 0; i <=4; i++) {
for ( j = 0; j <= 4 - i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
#数组大小排序
import java.util.Arrays;
public class arrtest {
public static void main(String[] args) {
System.out.println("请输入数组个数:");
int c = getint();
int[] arr = new int[c];
for (int i = 0; i < c; i++) {
System.out.println("请输入第"+(i+1)+"整数");
arr[i]=getint();
}
System.out.println("排序前:"+Arrays.toString(arr));
paixu(arr);
}
private static void paixu(int[] arr) {
int b;
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if (arr[i] < arr[j]) {
b = arr[i];
arr[i] = arr[j];
arr[j] = b;
}
}
}
System.out.println("排序后:"+Arrays.toString(arr));
}
private static int getint() {
Scanner sc = new Scanner(System.in);
return sc.nextInt();
}
}
#水仙花数:是指一个三位数,其各位数字立方和等于该数本身
package test;
import java.util.Scanner;
public class shuixianhua {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int q = i % 10;
int w = (i / 10) % 10;
int e = i /100;
if (i == (q*q*q)+(w*w*w)+(e*e*e)) {
System.out.println(i);
}
}
}
}
求s=a+aa+aaa+aaaa+aa...a
package test;
import java.util.Scanner;
public class testtest {
public static void main(String[] args) {
System.out.println("请输入相加次数:");
int a =getint();
System.out.println("请输入数值:");
int b =getint1();
int first =b;
int s = 0;
for (int i = 0; i < a; i++) {
s += b;
b = b * 10 + first;
}
System.out.println("相加后的值为:"+s);
}
private static int getint1() {
Scanner sc = new Scanner(System.in);
return sc.nextInt();
}
private static int getint() {
Scanner sc = new Scanner(System.in);
return sc.nextInt();
}
}
#青蛙跳一只青蛙一次可以跳1级或2级台阶,N级的台阶总共有多少种跳法
分析:
/**
1层:1种
2层: 2种
3层: 3种
4层: 5种
5层: 8种
6层: 13种
7层 21种
总结规律
2层=1层+1层
3层=1层+2层
4层=2层+3层
5层=3层+4层
6层=4层+5层
7层=5层+6层
**/
第一种写法
public static void main(String[] args){
System.out.println("请输入台阶数:");
try (Scanner input = new Scanner(System.in)) {
int n=input.nextInt();
//frogjump方法
long result = frogjump(n);
// long result = (int)Math.pow(n-3, 2)+1;
System.out.println("共有"+result+"种方法");
}
}
//frogjump计算方法
static int frogjump(int n ){
if(n==1){
return 1;
}else if(n==2){
return 2;
}else{
int a = 1;
int b = 2;
int tmp = 0;
for(int i=3;i<=n;i++){
tmp = a+b;
a = b;
b = tmp;
}
return tmp;
}
}
第二种写法
public static void main(String[] args){
System.out.println("请输入台阶数:");
try (Scanner input = new Scanner(System.in)) {
int n=input.nextInt();
//调用jump方法
long result = jump(n);
System.out.println("共有"+result+"种方法");
}
}
//jump计算方法
public static int jump(int n){
if(n==0){
return 0;
}
else{
int[] frog=new int[3];
frog[0]=1;
frog[1]=2;
for(int i=2;i<n;i++){
frog[i%3]=frog[(i-1)%3]+frog[(i-2)%3];
}
//返回跳的次数
return frog[(n-1)%3];
}
}
第N总解法---,掌握数学规律后,想咋写就咋写
#这天是一年的第几天?
public static void main(String[] args){
System.out.println("----这一天是这一年的第几天-----");
//设置日期格式
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM");
Scanner sc =new Scanner(System.in);
System.out.println("请输入年份");
String dyear=sc.nextLine();
System.out.println("请输入月份");
String dmouth=sc.nextLine();
System.out.println("请输入日");
int dayint =sc.nextInt();
Calendar rightNow = Calendar.getInstance();
int daysum=0;
try{
int a =Integer.parseInt(dmouth);
for (int i = 1; i < a; i++) {
rightNow.setTime(simpleDate.parse(dyear+"/"+i));
int days=0;
days =rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.print(i+"月:"+days+"天"+" ");
daysum +=days;
}
}catch(ParseException e){e.printStackTrace();}
System.out.println("\n"+"年第:"+(daysum+dayint)+"天");
//getActualMaximum获取天数最大值
int days = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);//根据年月 获取月份天数
System.out.println(days);
}