java编程题50道(4)

程序16

题目:输出9*9口诀。

题目思路:

以两个for循环执行输出,第一个for循环分行,控制前一个乘数,第二个for循环,控制后乘数与乘积并输出

 1 package test;
 2 
 3 import java.util.Scanner;
 4 public class hello {
 5 
 6     public static void main(String[] args) 
 7     {
 8         Scanner in=new Scanner(System.in);
 9         System.out.print("输入");
10         int a=0;
11         for (int i = 1; i <= 9; i++) {
12             System.out.println();//每次大循环时分行
13             for (int j = 1; j <=i; j++) {
14                 a=i*j;
15                 System.out.print(i+"*"+j+"="+a+" ");//按x*y=xy格式输出
16             }
17         }
18     }
19 }    

程序17

题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

题目思路:

由第十天反推到第一天,每一天均为前一天的二倍加一,即x*2+1

package test;

import java.util.Scanner;
public class hello {

    public static void main(String[] args) 
    {
        Scanner in=new Scanner(System.in);
        System.out.print("输入");
        int a=1;
        for (int i = 1; i <= 9; i++) {
                a=a*2+1;//每一天均为前一天的一半零一个
                System.out.println(""+i+"天有"+a+"");
            }
        }
    
}    

 

程序18

题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

题目思路:

将两队人员以列表定义,通过for循环匹配,用if来决定条件,最后输出

 1 package test;
 2 
 3 import java.util.Scanner;
 4 public class hello {
 5 
 6     public static void main(String[] args) 
 7     {
 8         Scanner in=new Scanner(System.in);
 9         System.out.print("输入");
10         char [] list = {'a','b','c'};//使用数组定义两队人员
11         char [] list1 = {'x','y','z'};    
12         System.out.print("比赛名单为:");
13         for (int i = 0; i < 3; i++) {
14             System.out.println();
15             for (int j = 0; j < 3; j++) {
16                 if (list[i]=='a'&& list1[j]=='x') {
17                     continue;//注意使用continue,不要使用break,否则会直接跳出
18                 }
19                 else if (list[i]=='c'&& list1[j]=='x') {
20                     continue;
21                 }
22                 else if (list[i]=='c'&& list1[j]=='z') {
23                     continue;
24                 }
25                 else {
26                     System.out.print(list[i]+"vs"+list1[j]+" ");//输出比赛情况
27                 }
28             }
29         }
30         }
31     
32 }    

程序19

题目:打印出如下图案(菱形)。

题目思路:

输出分为两部分,分别输出,注意for循环中空格与*的输出,结束后要换行

 1 package test;
 2 
 3 import java.util.Scanner;
 4 public class hello {
 5 
 6     public static void main(String[] args) 
 7     {
 8         Scanner in=new Scanner(System.in);
 9         int a=7;
10         for (int x = 0; x <4; x++) {//输出上半部分
11             for (int i = 0; i <a/2 - x ; i++) {
12             System.out.print(" ");
13             }
14             for (int j = 0; j <x*2+1; j++) {
15                 System.out.print("*");
16             }
17             System.out.println();
18         }
19         for (int x = 1; x <=3; x++) {//输出下半部分
20             for (int i = 1; i <=x ; i++) {
21             System.out.print(" ");
22             }
23             for (int j = 1; j <=7-2*x; j++) {
24             System.out.print("*");
25             }
26             System.out.println();
27         }
28     }
29 }    

程序20

题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

题目思路:

第三项开始,不论除数与被除数均为前两项相加之后,通过for循环求出第三项并替换

 1 package test;
 2 
 3 import java.util.Scanner;
 4 public class hello {
 5 
 6     public static void main(String[] args) 
 7     {
 8         Scanner in=new Scanner(System.in);
 9         int a=2;//先定义
10         int a1=3;
11         int b=1;
12         int b1=2;
13         int a2=0;
14         int b2=0;
15         double sum=2/1+3/2;//先将前置项写出
16         for (int i = 2; i <20; i++) {
17             a2=a+a1;//求出新项
18             b2=b+b1;
19             sum+=a2/b2;
20             a=a1;//替换
21             a1=a2;
22             b=b1;
23             b1=b2;
24             System.out.println(sum);
25         }
26         System.out.print("总和为:"+sum);
27     }
28 }    

 

posted @ 2021-07-14 16:31  诶楼勾勾  阅读(59)  评论(0)    收藏  举报