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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yinzhenyu 初级黑马   /  2014-9-21 19:50  /  6145 人查看  /  29 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 yinzhenyu 于 2014-9-21 19:51 编辑
  1. <p><div class="blockcode"><blockquote>package com.itheima;


  2. import java.util.Scanner;

  3. public class test2 {

  4.         /**
  5.          * 2、 从键盘接受一个数字,列出该数字的中文表示格式,例如:键盘输入123,打印出一二三;键盘输入3103,打印出三一零三。
  6.          * 思路:
  7.          * 1.接收一个键盘录入的数字
  8.          * 2.将这个数字转换成中文格式,查表法就可以满足
  9.          *
  10.          * @param args
  11.          *  
  12.          */
  13.         public static void main(String[] args)  {
  14.                
  15.                    System.out.println("请输入数字:");
  16.                   String[] s={"零 " ,"一","二","三","四","五","六","七","八","九"};
  17.                   while(true){
  18.                           
  19.                     Scanner s1=new Scanner(System.in);

  20.                 String str=s1.nextLine();

  21.                      for (int i = 0; i < str.length(); i++) {

  22.                             //提取键盘输入字符串中的每一个字符再转换成阿拉伯数字
  23.                         int c= str.charAt(i)-'0';
  24.                        //如果阿拉伯数字在0~9就输出s中相应c位置的元素
  25.                          if(c>=0&& c<10){

  26.                            System.out.print(s[c]);
  27.                           

  28.                          }else{

  29.                                System.out.print("请重新输入数字:"+"\n");
  30.                                break;

  31.                          }
  32.                      

  33.                      }
  34.                      System.out.println("\n");
  35.                      
  36.                   }

  37.      }

  38. }
  39. </p><p>
  40. </p>
复制代码

29 个回复

倒序浏览
  1. package com.itheima;

  2. import java.util.ArrayList;
  3. import java.util.Iterator;

  4. public class test1 {

  5.         /**
  6.          * 1、 创建ArrayList对象,添加5个元素,使用Iterator遍历输出
  7.          * @param args
  8.          */
  9.         public static void main(String[] args) {
  10.                
  11.                 //创建一个ArrayList对象并使用泛型
  12.                 ArrayList<String> al = new ArrayList<String>();
  13.                 //添加5个元素,用add方法
  14.                 al.add("abc1");
  15.                 al.add("abc2");
  16.                 al.add("abc3");
  17.                 al.add("abc4");
  18.                 al.add("abc5");
  19.                
  20.                 //返回一个有序的迭代器
  21.                 Iterator<String> it = al.iterator();
  22.                 //循环迭代器,输出
  23.                 while(it.hasNext()){  //如果下一个不为空,循环
  24.                         //获取下一个元素
  25.                         String s = it.next();
  26.                         //打印下一个元素
  27.                         System.out.println(s);
  28.                        
  29.                 }
  30.                                

  31.         }

  32. }
复制代码


回复 使用道具 举报
  1. package com.itheima;

  2. public class test3 {

  3.         /**
  4.          * 3.定义一个二维int数组,编写代码获取最小元素
  5.          * @param args
  6.          */
  7.         public static void main(String[] args) {
  8.                
  9.                 int[][] arr = {{12,34},{75,9,78,35},{24,65,44,9,3},{43,56,21,8}};
  10.                
  11.                 int min = getmin(arr);
  12.                 System.out.println("最小元素为:"+min);

  13.         }

  14.         public static int getmin(int[][] arr) {
  15.                
  16.                 int min = arr[0][0];
  17.                 for (int i = 0; i < arr.length; i++) {
  18.                         for (int j = 0; j < arr[i].length; j++) {
  19.                                
  20.                                 if(arr[i][j]<min)
  21.                                         min = arr[i][j];
  22.                                
  23.                                
  24.                         }
  25.                 }
  26.                
  27.                 return min;
  28.         }

  29. }
复制代码


回复 使用道具 举报
  1. package com.itheima;

  2. public class test4 {

  3.         /**
  4.          * 4.请列举您了解的一些排序算法,并用Java语言实现一个效率较高的
  5.          *
  6.          * 我了解的一些排序算法:
  7.          * 直接选择排序,冒泡排序,折半排序,快速排序,直接插入排序,希尔排序,归并排序
  8.          *
  9.          * 这里实现一个效率较高的冒泡排序
  10.          * @param args
  11.          */
  12.         public static void main(String[] args) {
  13.                
  14.                 int[] str = {2,45,6,87,3,56,1,56};
  15.                 //打印数组元素
  16.                 printArray(str);
  17.                 //对数组进行排序
  18.                 bubbleSort(str);
  19.                 printArray(str);
  20.                

  21.         }
  22.     //数组打印
  23.         public static void printArray(int[] str) {
  24.                
  25.                 System.out.print("[");
  26.                 for (int i = 0; i < str.length; i++) {
  27.                         if(i!=str.length-1)
  28.                                 System.out.print(str[i]+",");
  29.                         else
  30.                                 System.out.println(str[i]+"]");
  31.                        
  32.                 }
  33.         }
  34.     //冒泡排序
  35.         public static void bubbleSort(int[] str) {
  36.                  for ( int i = 0; i < str.length; i++ )
  37.                 {
  38.                     for ( int j = i + 1; j < str.length; j++ )
  39.                     {
  40.                         if (str[i] > str[j])
  41.                         {
  42.                             swap(str, i, j);
  43.                         }
  44.                     }
  45.                 }
  46.                
  47.                
  48.         }
  49.     //元素交换位置
  50.         public static void swap(int[] str, int i, int j) {
  51.                 int temp = str[i];
  52.                 str[i] = str[j];
  53.                 str[j] = temp;
  54.         }

  55. }
复制代码


点评

冒泡写成选择排序了  发表于 2015-8-21 18:48
回复 使用道具 举报
  1. package com.itheima;

  2. import java.util.Scanner;

  3. public class test5 {

  4.         /**
  5.          *  5.编写程序,从键盘接收一个字符串,对字符串中的字母进行大小写互转(大写字母转成小写,小写字母转成大写)
  6.          * @param args
  7.          */
  8.         public static void main(String[] args) {
  9.                
  10.                 while(true){
  11.                 System.out.println("请输入一串字母:");
  12.         //从键盘读入数据
  13.                 Scanner sc = new Scanner(System.in);
  14.                 //读取整行
  15.                 String inputStr = sc.nextLine();
  16.                 //大小写转换
  17.             String outputStr = convert(inputStr);
  18.                 System.out.println(inputStr+"     大小写转换后:      "+outputStr+"\n");
  19.                 }
  20.         }

  21.         public static String convert(String inputStr) {
  22.                 //定义一个数组容器
  23.                 char[] arr = new char[inputStr.length()];

  24.                 for(int i=0;i<inputStr.length();i++){
  25.                         //得到字母对应的ascii码
  26.                     int ascii = (int)inputStr.charAt(i);
  27.                            //小写
  28.                        if(ascii>=97){
  29.                       arr[i] = (char)(ascii-32);
  30.                     //大写
  31.                            }else if(ascii>=65){

  32.                              arr[i] = (char)(ascii+32);

  33.                             }

  34.                 }

  35.             return String.valueOf(arr);

  36.         }

  37.        

  38. }
复制代码


回复 使用道具 举报
你写的冒泡排序代码是选择排序的吧,最后那个大小写互换直接用Arrays不就行了

点评

求教,用Arrays的什么?  发表于 2015-8-21 16:42
回复 使用道具 举报
看看。。
回复 使用道具 举报
进击的大鹏 发表于 2014-9-21 22:52
你写的冒泡排序代码是选择排序的吧,最后那个大小写互换直接用Arrays不就行了 ...

恩,一开始写的,不太成熟
回复 使用道具 举报
值得学习,我写基础测试时,也要这样。
回复 使用道具 举报
感谢楼主分享!
回复 使用道具 举报
谢谢了。。。。
回复 使用道具 举报
不管怎么样,很不错
回复 使用道具 举报
向楼主学习啊
回复 使用道具 举报
xplcc 中级黑马 2014-9-21 23:55:10
14#
谢谢分享
回复 使用道具 举报
谢谢分享哦
回复 使用道具 举报
恩,挺不错的
回复 使用道具 举报
我只能说佩服..........
回复 使用道具 举报
先留个脚印
回复 使用道具 举报
不错,挺好的,加油
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马