第1关
两整数的四则运算
package step1;
import java.util.Scanner;
public class Arithmetic2Num {
public static void main(String[] args) {
// ---------------------Begin------------------------
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.print("a+b=");
System.out.println(a+b);
System.out.print("a*b=");
System.out.println(a*b);
System.out.print("a-b=");
System.out.println(a-b);
System.out.print("a/b=");
System.out.println(a/b);
// ---------------------End------------------------
}
}
第2关 打印三角形
package step2;
import java.util.Scanner;
public class PosTriangle {
public static void prin(int n,int n2)
{
int num=n*2-1;
if(n==1){
for(int i=0;i<n2;i++)System.out.print(' ');
System.out.println('*');
return;
}
prin(n-1,n2+1);
for(int i=0;i<n2;i++)System.out.print(' ');
for(int i=0;i<num;i++)System.out.print('*');
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//定义输入的行数:
int n = scanner.nextInt();
// ---------------------Begin------------------------
prin(n,0);
// ---------------------End------------------------
}
}
第3关 循环之倒三角形
package step3;
import java.util.Scanner;
public class NegTriangle {
public static void prin(int n,int x)
{
int num=n*2-1;
if(n==x){
for(int i=0;i<num;i++)System.out.print('*');
System.out.println();
return;
}
prin(n+1,x);
for(int i=0;i<x-n;i++)System.out.print(' ');
for(int i=0;i<num;i++)System.out.print('*');
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//定义输入的行数:
int n = scanner.nextInt();
// ---------------------Begin------------------------
prin(1,n);
// ---------------------End------------------------
}
}
第4关 水仙花数
package step4;
import java.util.Scanner;
public class narcissisticNumber {
public static boolean dfs(int x){
int sum=0;
int num=x;
while(num!=0){
int m=num%10;
sum=sum+(m*m*m);
if(sum>x)return false;
num=num/10;
}
if(sum==x) return true;
else return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//获取起始范围
int i = scanner.nextInt();
//获取终止范围
int j = scanner.nextInt();
// ---------------------Begin------------------------
int l=0;
for(int k=i;k<=j;k++)
{
if(dfs(k))
System.out.println(k);
else l++;
}
if(l==(j-i+1)){
System.out.print(i);
System.out.print('~');
System.out.print(j);
System.out.println("范围内无水仙花数!");
}
// ---------------------End------------------------
}
}
第5关 a与b之间的素数
package step5;
import java.util.Scanner;
public class PrimeNumber {
public static boolean find(int x){
if(x==1)return false;
for(int i=2;i<=9;i++){
if(x%i==0&&i<x)return false;
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// ---------------------Begin------------------------
int i = scan.nextInt();
int j = scan.nextInt();
System.out.print(i);
System.out.print("到");
System.out.print(j);
System.out.println("之间的素数有:");
for(int k=i;k<=j;k++){
if(find(k)){
System.out.print(k);
System.out.print(' ');
}
}
}
// ---------------------End------------------------
}
第6关
余X的整数
package step6;
import java.util.Scanner;
public class LeftXnumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// ---------------------Begin------------------------
int a=scanner.nextInt();
int b=scanner.nextInt();
int c=scanner.nextInt();
for(int i=a;i<=b;i++){
if(i%c==0)System.out.println(i);
}
// ---------------------End------------------------
}
}
第7关 求解1元2次方程
package step7;
import java.util.Scanner;
public class SolveEquations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a=scanner.nextDouble();
double b=scanner.nextDouble();
double c=scanner.nextDouble();
double delta=b*b-4*a*c;
if(delta>0){
double x1=(-b+Math.sqrt(delta))/(2*a);
double x2=(-b-Math.sqrt(delta))/(2*a);
System.out.printf("方程有两个解:x1=%.2f x2=%.2f",x1,x2);
}else if(delta==0){
double x=-b/(2*a);
System.out.printf("方程有一个解:x1=x2=&.2f",x);
}else {
System.out.println("此方程无解");
}
// ---------------------End------------------------
// ---------------------End------------------------
}
}