import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()) {
int n = sc.nextInt();
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < n ; j++) {
if(i == j || i + j == n-1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
}
public static void main(String[] args) {
double sum = 0;
int tmp = 1;
for (int i = 1; i <= 100; i++) {
sum += 1.0/i * tmp;
tmp = - tmp;
}
System.out.println(sum);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
while(num != 0) {
System.out.print(num%10+" ");
num /= 10;
}
}
要求:// 编写代码模拟三次密码输入的场景。 最多能输入三次密码,
// 密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序
Java中比较字符是.equals
public static void main(String[] args) {
int count = 3;
Scanner scanner = new Scanner(System.in);
while (count != 0) {
System.out.println("请输入你的密码:");
String passWord = scanner.nextLine();
if (passWord.equals( "123456")) {
System.out.println("登录成功");
return;
}else {
count--;
System.out.println("你还有 "+count+"次机会");
}
}
System.out.println("密码错误");
}
要求:创建方法求两个数的最大值max2,随后再写一个求3个数的最大值的函数max3
public static void main(String[] args) {
int a = 2;
int b = 4;
int c = 9;
System.out.println(max2(a, b));
System.out.println(max3(a, b, c));
}
public static int max2(int x,int y) {
return x > y ? x : y;
}
public static int max3(int i,int j,int k) {
int ret = max2(i,j);
return max2(ret,k);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
System.out.println(fib(num));
}
public static int fib(int n) {
if (n == 1 || n == 2) {
return 1;
}
int f1 = 1;
int f2 = 1;
int f3 = 2;
for (int i = 3; i <= n; i++) {
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
return f3;
}
要求:求两个整数的方法 和 三个小数之和
public static void main(String[] args) {
int x = 1;
int y = 2;
double d1 = 2.8;
double d2 = 5.2;
double d3 = 2.5;
System.out.println(add(x,y));
System.out.println(add(d1,d2,d3));
}
public static int add(int a,int b) {
return a+b;
}
public static double add(double c,double d,double e) {
return c+d+e;
}
在同一个类中定义多个方法:要求不仅可以求2个整数的最大值,还可以求3个小数的最大值?
public static void main(String[] args) {
int n1 = 2;
int n2 = 5;
double d1 = 2.5;
double d2 = 9.5;
double d3 = 7.5;
System.out.println(max(n1,n2));
System.out.println(max(d1,d2,d3));
}
public static int max(int a,int b) {
return Math.max(a, b);
}
public static double max(double n,double m, double l) {
double d = Math.max(n,m);
return Math.max(d,l);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int ret = fac(N);
System.out.println(ret);
}
public static int fac(int n) {
if (n == 1) {
return 1;
}
return n * fac(n-1);
}
public static void main(String[] args) {
System.out.println(sum(10));
}
public static int sum(int n) {
if (n == 1) {
return 1;
}
return n + sum(n-1);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
print(num);
}
public static void print(int n) {
if (n < 10) {
System.out.print(n);
return;
}
print(n / 10);
System.out.print(" " + (n % 10));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
System.out.println(sumEvery(num));
}
public static int sumEvery(int n) {
if (n < 10) {
return n;
}
return sumEvery(n / 10) + n % 10;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
System.out.println(fib1(num));
}
public static int fib1(int n) {
if (n == 1 || n == 2) {
return 1;
}
return fib1(n-1) + fib1(n-2);
}
public static void main(String[] args) {
hanoi(3,'A','B','C');
}
public static void move(char pos1,char pos2) {
System.out.print(pos1 + "-> " + pos2 + " ");
}
public static void hanoi(int n,char pos1,char pos2,char pos3) {
if (n == 1) {
move(pos1,pos3);
return;
}
hanoi(n-1,pos1,pos3,pos2);
move(pos1,pos3);
hanoi(n-1,pos2,pos1,pos3);
}