A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 寒星在天 中级黑马   /  2015-8-25 00:20  /  1241 人查看  /  20 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.Scanner;
class TestOne {
        public static void main(String[] args) { //这个是main函数,是程序的主入口,代码的执行,是从这里开始的。
                /*
                        需求:综合小案例
                                        首先进入到游戏选择界面,根据用户输入的数字玩儿对应的游戏                                               
                                                游戏1:让用户输入一个数字,你来打印对应的乘法表
                                                游戏2:让用户输入两个数字,你来计算出这两个数字间的所有整数和
                                                游戏3:让用户输入两个数字,你来交换着两个数字的值
                                                游戏4:让用户输入两个数字,你根据这两个数字来打印一个对应行列的正三角形*图
                                                游戏5:让用户输入两个数字,你来计算着两个数字间的奇数有多少个。
                                                游戏6:我说咱俩心有灵犀你信吗?不信你输入下你最喜欢吃的水果
                               
                */
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入你要玩儿的游戏的编号,游戏如下:");
                show();
                int num = sc.nextInt();
                System.out.println("你选择玩儿的是游戏是Game" + num);
                switch (num) {
                        case 1:
                                game1();
                                break;
                        case 2:
                                game2();
                                break;
                        case 3:
                                game3();
                                break;
                        case 4:
                                game4();
                                break;
                        case 5:
                                game5();
                                break;
                        case 6:
                                game6();
                                break;
               
                }
               
        }

        public static void show(){
                System.out.println("\tGame1:输入一个数字,我给你打印对应的乘法表");
                System.out.println("\tGame2:输入两个数字,我给你计算出这两个数字间的所有整数和");
                System.out.println("\tGame3:输入两个数字,有意想不到的惊喜哟!!"); //你来交换着两个数字的值
                System.out.println("\tGame4:输入两个数字,我能猜到你心里在想什么!");   //正三角形*图
                System.out.println("\tGame5:输入两个数字,你来计算着两个数字间的奇数有多少个");
                System.out.println("\tGame6:我说咱俩心有灵犀你信吗?不信你输入下你最喜欢吃的水果"); //
        }
        public static void game1(){
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个数字,范围是1-9:");
                int x=sc.nextInt();
                print(x);
                }
public static void print(int x){
                        for (int a=1;a<=x ; a++) {
                                for (int b=1;b<=a ;b++ ) {
                                        System.out.print(a+"*"+b+"="+b*a+"\t");
                                }
                                System.out.println();
                        }

        }
        public static void game2(){
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入第一个数字:");
                int x=sc.nextInt();
                System.out.println("请输入第二个数字,大于第一个数字:");
        int y=sc.nextInt();
                int sum= getsum(x,y);
        System.out.println(sum);
        }
        public static int getsum(int x,int y){
                int sum=0;
                for (int a= x;a<=y ; a++) {
                        sum+=a;
                }
                return sum;
        }
        public static void game3(){
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入第一个数字a:");
                int x=sc.nextInt();
                System.out.println("请输入第二个数字b:");
        int y=sc.nextInt();
        exchange(x,y);
        }
                public static void exchange(int x,int y){
                                int a=x;
                                int b=y;
                                a=a^b;
                                b=a^b;
                                a=a^b;
           System.out.println("a="+a+",b="+b);
                }

        public static void game4(){
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入第一个数字:");
                int x=sc.nextInt();
                System.out.println("请输入第二个数字:");
        int y=sc.nextInt();
        printxing(x,y);
        }
    public static void   printxing(int x,int y){
                for (int a=1;a<=x;a++ ) {
           for (int c=1;c<a ; c++) {
                System.out.print(" ");
           }
                        for (int b=1;b<=x-a ; b++) {
                  System.out.print("* ");
                        }
                        System.out.println();
                }
        }
        public static void game5(){
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入第一个数字:");
                int x=sc.nextInt();
                System.out.println("请输入第二个数字大于第一个数字:");
        int y=sc.nextInt();
        int count=getcount(x,y);
        System.out.println(count);
        }
                public static int getcount(int x,int y){
                        int count=0;
                for (int a=x;a<=y ;a++ ) {
                        if (a%2!=0) {
                                count++;
                        }
                }
                return count;
                }
        public static void game6(){
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入你最喜欢吃的水果");
                String fruit = sc.next();
                System.out.println("美女,这么巧啊,我也喜欢吃" + fruit );
        }
}

20 个回复

倒序浏览
小顶一下
回复 使用道具 举报
有意思,有一点意思吗
回复 使用道具 举报
楼主自己写的么?学了多久?好厉害的感觉
回复 使用道具 举报
城里人真会玩。
回复 使用道具 举报
楼主幽默啊,代码写的不错,呵呵
回复 使用道具 举报
cc3441251 来自手机 中级黑马 2015-8-25 13:18:06
7#
城里人果然会玩
回复 使用道具 举报
不会啊啊
回复 使用道具 举报

呵呵
回复 使用道具 举报
路过,看看,膜拜。。。
回复 使用道具 举报
这些都是java基础前几天的学习内容,楼主配合使用得很有意思呀。新手前来学习,膜拜~膜拜~
回复 使用道具 举报
进来看看。。。。。
回复 使用道具 举报
进来看看。。。。。
回复 使用道具 举报
小胡子721 来自手机 中级黑马 2015-8-25 15:30:09
14#
把基础玩得真扭,赞一个
回复 使用道具 举报
jeska 中级黑马 2015-8-25 18:02:07
15#
夯哥的徒弟,,,,,,,
回复 使用道具 举报
Wqi 高级黑马 2015-8-25 18:08:47
16#
夯哥布置作业......
回复 使用道具 举报
Wqi 高级黑马 2015-8-25 18:16:33
17#
  1. for(int x = 0;x < 5;x++) {
  2.                         for(int y = x+1;y < 5;y++) {
  3.                                 System.out.print(" ");
  4.                        
  5.                         }
  6.                         for(int z = 0;z <= x;z++) {
  7.                                 if(x != 4) {
  8.                                         if ((z == 0)||(z == x)) {
  9.                                                                                                        
  10.                                                 System.out.print("* ");
  11.                                         }
  12.                                         else {
  13.                                                 System.out.print("  ");
  14.                                         }
  15.                                 }else {
  16.                                         System.out.print("* ");
  17.                                 }
  18.                                        
  19.                         }
  20.                         System.out.println();
  21.                 }
复制代码

空心的正三角{:3_57:}
回复 使用道具 举报
666666666666
回复 使用道具 举报
天气预报 发表于 2015-8-25 09:40
楼主自己写的么?学了多久?好厉害的感觉

学了几天了自己写的啊
回复 使用道具 举报

哥也是农村的没见过大世面
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马