java基础练习题

1.给定一个字符串,判断该字符串是否是回文字符串

package Test;

public class Day06 {
    public static void main(String[] args) {
        //课堂案例:给定一个字符串,判断该字符串是否是回文字符串
        System.out.println(isHui("leel"));
        System.out.println(isHui("abcd"));
    }

    private static boolean isHui(String s) {
        for (int tou = 0,wei = s.length() - 1;tou < wei / 2 ; tou ++, wei--){
            if (s.charAt(tou) != s.charAt(wei)){
                return false;
            }
        }
        return true;
    }
}




/*
true
false

*/

2. 输入一个字符串,判断该字符串是否是回文字符串(面试题)
123454321

public class Day06 {
    public static void main(String[] args) {
//        输入一个字符串,判断该字符串是否是回文字符串(面试题)
//        123454321
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你想判断的字符串:");
        String s = scanner.nextLine();
        System.out.println(isHui1(s));
    }

    public static boolean isHui1(String s){
        int tou = 0, wei = s.length() - 1;
        while (tou < wei){
            if (s.charAt(tou) != s.charAt(wei)){
                return false;
            }
            tou ++;
            wei --;
        }
        return true;
    }


/*
请输入你想判断的字符串:12345654321
true
*/

3.去除字符串中所有的空格

public class Day06 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你想去除空格的字符串:");
        String s = scanner.nextLine();
        System.out.println(Qukong(s));
        //去除字符串中所有的空格
    }

    public static String Qukong(String s){
        String st = "";
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' '){
                continue;
            }else {
                st +=s.charAt(i);
            }
        }
        return st;
    }


/*
请输入你想去除空格的字符串:    1   2  3  5  44
123544
*/

4.将小写字母转换为大写字母

public class Day06 {
    public static void main(String[] args) {
        System.out.println("输入的字符串为:");
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        System.out.print(DAXiao(str));
    }
    
    public static String DAXiao(String s){
        //        将字母全部转换为大写或小写
        String str1="";
        for(int i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            if((ch>='a'&&ch<='z'))
            {   //每个大小写字符的ASCII码都相差32,减去32即可得到该字符的大写形式
                ch=(char)((int)ch-32);
            }
            str1+=ch;
        }
        return str1;
    }

/*
输入的字符串为:
abcddef
ABCDDEF
*/

5.接收用户输入的一句英文,将其中的单词以反序输出,
  “hello c sharp”→“sharp c hello”。

public class Day06 {
    public static void main(String[] args) {
        System.out.println("输入的字符串为:");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        System.out.print(Fan(str));
    }

    public static String Fan(String s){
        //    接收用户输入的一句英文,将其中的单词以反序输出,
        //    “hello c sharp”→“sharp c hello”。
        String temp = "";
        String[] st = s.split(" ");
        for (int i = st.length - 1; i >= 0; i--) {
            temp += st[i] + " ";
        }
        return temp;
    }


/*
输入的字符串为:
你 爱 我
我 爱 你 
*/

6.从请求地址中提取出用户名和域名

//http://www.163.com?userName=admin&pwd=123456

public class Day06 {
    public static void main(String[] args) {
        System.out.print("用户名为:"+TiQu());
    }

    public static String TiQu(){
        //从请求地址中提取出用户名和域名
        //http://www.163.com?userName=admin&pwd=123456
        String str = "http://www.163.com?userName=admin&pwd=123456";
        String st = "";
        String st1 = "";
        String st2 = "";
        st = str.substring(str.indexOf("=") +1,str.indexOf("pwd") -1);
        st1 = str.substring(str.lastIndexOf("=") +1);
        st2 = str.substring(str.indexOf("/") +2, str.indexOf("?"));
        System.out.println("域名为:" + st2);
        System.out.println("密码为:"+ st1);
        return st;
    }

/*
域名为:www.163.com
密码为:123456
用户名为:admin
*/

7.让用户输入一句话,找出所有"呵"的位置

public class Day06 {
    public static void main(String[] args) {
        System.out.println("输入的字符串为:");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        Zhao(str);
    }

    public static void Zhao(String s){
//        让用户输入一句话,找出所有"呵"的位置。
        for (int i = 0; i < s.length(); i++) {
              if (s.charAt(i) == '呵') {
                  System.out.println("呵位置在:" + i);
              }
        }
    }


/*
输入的字符串为:
你好啊呵呵哒
呵位置在:3
呵位置在:4
*/

8.让用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变 成”老牛很**”;

public class Day06 {
    public static void main(String[] args) {
        System.out.println("输入的字符串为:");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        System.out.println(TiHuan(str));
    }

    public static String TiHuan(String s){
        //让用户输入一句话,判断这句话中有没有邪恶,如果有邪
        //恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变
        //成”老牛很**”;
        String str = "";
        str = s.replace("邪恶","**");
        return str;
    }


/*
输入的字符串为:
你这个人怎么这么邪恶呢
你这个人怎么这么**呢
*/

9.猜字游戏,计算机随机一个0~100的整数,每次用户输出字猜,提示如下:
   猜大了,重新猜
    猜小了,重新猜
    猜对了

public class Day06 {
    public static void main(String[] args) {
        CaiShu();
    }

    public static void CaiShu(){
        int zhen = (int) (Math.random() * 100 + 1);
        int count = 1;
        while (count <= 3){
            System.out.print("请输入您第"+ count + "次猜的数字:");
            Scanner sc=new Scanner(System.in);
            int cai=sc.nextInt();
            if ( cai == zhen){
                System.out.println("恭喜您猜对了!");
                break;
            }
            else if (cai < zhen){
                System.out.println("很遗憾您猜小了!");
            }
            else {
                System.out.println("很遗憾您猜大了!");
            }
            count ++;
        }
        System.out.println("很遗憾您没有猜出来,正确答案是:"+ zhen);
    }


/*
请输入您第1次猜的数字:50
很遗憾您猜大了!
请输入您第2次猜的数字:25
很遗憾您猜小了!
请输入您第3次猜的数字:38
很遗憾您猜小了!
很遗憾您没有猜出来,正确答案是:42
*/

10.猜拳游戏,石头剪刀布。
    随机数生成石头剪刀布(0:石头  1:剪刀  2:布)

public class Day06 {
    public static void main(String[] args) {
        Shi();
    }

    public  static void Shi(){
        //猜拳游戏,石头剪刀布。
        //随机数生成石头剪刀布(0:石头  1:剪刀  2:布)
        Random random = new Random();
        int sui = random.nextInt(3);
        System.out.print("请输入您的选项(0:石头  1:剪刀  2:布):");
        Scanner scanner = new Scanner(System.in);
        int chu = scanner.nextInt();
        if (sui == chu){
            System.out.println("您和我的想法一样,平局!!!");
        }else if ((sui == 0 && chu == 1) || (sui ==1 && chu ==2) ||(sui ==2 && chu ==0)){
            System.out.println("抱歉您输了!");
        }
        else if ((sui == 1 && chu == 0) || (sui ==0 && chu ==2) ||(sui ==2 && chu ==1)){
            System.out.println("恭喜您赢了!");
        }
        else {
            System.out.println("对不起,您的输入有误!!!");
        }
    }


/*
请输入您的选项(0:石头  1:剪刀  2:布):2
恭喜您赢了!
*/

11.求 2/1+3/2+5/3+8/5+13/8.....前20项之和?

public class Day06 {
    public static void main(String[] args) {
        QiHe();
    }

    public static void QiHe(){
        //求 2/1 + 3/2 + 5/3 + 8/5 + 13/8.....前20项之和?
        double f = 2.0;
        double s = 1.0;
        double f1 = 0.0;
        double s1 = 1.0;
        double num;
        double sum = 0.0;
        for (int i = 0; i < 20; i++) {

            num = f / s;
            sum += num;
            f = f + s;
            s = f - s;
        }
        System.out.println(sum);
    }


/*
32.66026079864164
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值