【大数据开发】Java语言基础——作业day03
1.图形打印(每一个图形都需要单独设计一个方法)(注:这是四个题)
class Demo12
{
public static void main(String[] args)
{
for (int i=1; i<=5; i++)
{
for (int j=0; j<i-1;j++)
{
System.out.print(" ");
}
System.out.println("*****");
}
}
}
class Demo13
{//0 2 4 6 8
//1 2 3 4 5
//9 7 5 3 1
//0 1 2 3 4
public static void main(String[] args)
{
for (int i=0; i<5; i++)
{
//打印空格
for (int j=0; j<i; j++)
{
System.out.print(" ");
}
//打印*
for (int k=9-2*i; k>=1; k--)
{
System.out.print("*");
}
//换行
System.out.println();
}
}
}
class Demo14
{
//每次打印5个
//每次打印后字母向后移动一位
//每五个换一行
//
//打印空格
public static void main(String[] args)
{
char ch1='A';
for (int i=0; i<5; i++)
{
//打印空格
for (int j=0; j<i; j++)
{
System.out.print(" ");
}
//打印字母
for (int k=0; k<5; k++)
{
char ch2=ch1;
System.out.print((char)(ch2+k));
}
ch1++;
System.out.println();
}
}
}
class Demo15
{
//每次打印5个
//每次打印后字母向前移动一位
//每五个换一行
//
//打印空格
public static void main(String[] args)
{
char ch1='E';
for (int i=0; i<5; i++)
{
//打印空格
for (int j=0; j<4-i; j++)
{
System.out.print(" ");
}
//打印字母
for (int k=0; k<5; k++)
{
char ch2=ch1;
System.out.print((char)(ch2+k));
}
ch1--;
System.out.println();
}
}
}
2.一个四位数字,恰巧等于去掉它首位数字之后所剩的三位数字的3倍,这个四位数字是多少
class Demo16
{
public static void main(String[] args)
{
for (int num=1000; num<10000; num++)
{
int a = num/1000;
int b = num - a*1000;
if (num == b*3)
{
System.out.println(num);
}
}
}
}
3. 求 2 + 22 + 222 + 2222 + 22222 + … + 2…2的和,数字的数量由控制台输入
import java.util.Scanner;
class Demo17
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//n表示数字的数量
int n = sc.nextInt();
int num=0, sum=0;
for (int i=0; i<n; i++)
{
num = num*10 + 2;
sum = sum + num;
}
System.out.print(sum);
}
}
4.输出斐波那契数列的前30位
class Demo18
{
public static void main(String[] args)
{
for(int i=1; i<=30; i++)
{
int sum = fac(i);
System.out.print(sum + "\t");
}
}
public static int fac(int n)
{
if (n==1 || n==2)
return 1;
return fac(n-1)+fac(n-2);
}
}
5.两个自然数X,Y相除,商3余10,被除数、除数、商、余数的和是163,求被除数、除数。
class Demo19
{
public static void main(String[] args)
{
for (int x=0; x<150; x++)
{
for (int y=1; y<150; y++)
{
if (x/y==3 && x%y==10 && x+y==150)
{
System.out.print("x=" + x + ",y=" + y);
}
}
}
}
}
6.某数学竞赛中,参赛人数大约在380~450人之间。比赛结果,全体考生的总平均分为76分,
男生的平均分为75分,女生的平均分为80.1分,求男女生各有多少人?
class Demo20
{
public static void main(String[] args)
{
for (int m=1; m<=450; m++)
{
for (int w=1; w<=150; w++)
{
if ((75*m+80.1*w)/(m+w)==76 && (m+w)>=380 && (m+w)<=450)
{
System.out.println("m=" + m + ",w=" +w);
}
}
}
}
}
7.从控制台输入两个英文字母,输出这两个英文字母之间的所有的字母
import java.util.Scanner;
class Demo21
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char c1 = sc.next().charAt(0);
char c2 = sc.next().charAt(0);
if (c1>c1)
{
char temp = c1;
c1 = c2;
c2 = temp;
}
for(char ch=c1; ch<=c2; ch++)
{
System.out.println(ch + "\t");
}
}
}