0
点赞
收藏
分享

微信扫一扫

腾讯云2核2G服务器优惠价格,61元一年

Mhhao 03-31 11:00 阅读 2

题目一 

代码

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner( System.in );
		String str = input.nextLine();
		int len = str.length();
		StringBuilder s = new StringBuilder(len);//StringBuilder类参考菜鸟教程
		for(int i = len-1;i >= 0;i--) {
			s.append(str.charAt(i));
		}
		System.out.print(s);
	}
}

输入输出 

A Nice World!123
321!dlroW eciN A

题目二

文本中查找字符串

从标准输入获取多行文本,行数小于100,具体行数自行指定,再输入要查找的字符串,输出要查找的字符串在文本中的位置,包含行号与每行中的下标位置,如无,则都显示-1

【输入形式】

采用交互式输入,
一个整数m表示m行文本

后m行文本内容

一个字符串表示要查找的内容
【输出形式】
每行两个整数,以空格分隔,第一个为行号,第二个为该行中的下标位置,以行数升序排列。如无,行号、列号均为-1

【输入形式】
Enter line numbers: 

4

Enter line contents: 

To you who knew me in my youth, 

Who spread sweet joy and spoke with truth; 

Before you passed to heaven’s skies, 

I saw an angel in your eyes. 
Enter search item:

you
【输出形式】
1 3

3 7

4 18

代码

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner( System.in );
		System.out.println("Enter line numbers: ");
		int LineNum = input.nextInt();
		String []str = new String[LineNum];
		System.out.println("Enter line contents: ");
		str[0] = input.nextLine();
		for( int i = 0;i < LineNum;i++ ) {
			str[i] = input.nextLine();
		}
		System.out.println("Enter search item: ");
		String SearchStr = input.nextLine();
		for( int i = 0;i < LineNum;i++ ) {
			if ( str[i].indexOf(SearchStr) != -1 ) {
				System.out.println((i+1)+" "+str[i].indexOf(SearchStr));
			}
		}
	}
}

输入输出

Enter line numbers:
4
Enter line contents:
To you who knew me in my youth,
Who spread sweet joy and spoke with truth;
Before you passed to heaven’s skies,
I saw an angel in your eyes.
Enter search item:
you
1 3
3 7
4 18

题目三

 1. 十六进制转化为十进制

代码

import java.util.*;
public class Main {
	//十六进制转化为十进制
	public static void main(String[] args) {
		Scanner input = new Scanner( System.in );
		String str = input.nextLine();
		int n=0;	//存储十进制数
		for ( int i = 0;i < str.length();i++ ){
			n=n*16+Getbit(str.charAt(i));
		}
		System.out.println(n);
	}
	
	public static int Getbit ( char c ) { 
		if ( '0' <= c && c <= '9' )
			return c - '0';//在ASCII表中,数字字符和它自己的真值正好相差一个字符‘0’
		if ( 'a' <= c && c <= 'f' )
			return c - 'a' + 10;//这种方法参考了CSDN
		if ( 'A' <= c && c <= 'F' )
			return c - 'A' + 10;//这种方法参考了CSDN
		return 0;
	}
}

测试:

5fFDb023
1610461219

OK没毛病!

 题目四

 

代码 

public class Main {
    public static void main(String[] args) {
        
        int num1 = Integer.parseInt(args[0]);
        char operator = args[1].charAt(0);
        int num2 = Integer.parseInt(args[2]);
        
        int result = 0;
        switch (operator) {
            case '+':
                result = num1 + num2;
				System.out.printf("%d + %d = %d",num1,num2,result);
                break;
            case '-':
                result = num1 - num2;
				System.out.printf("%d - %d = %d",num1,num2,result);
                break;
            case 'x':
                result = num1 * num2;
				System.out.printf("%d x %d = %d",num1,num2,result);
                break;
            case '/':
                if (num1 % num2 == 0) {
                    result = num1 / num2;
					System.out.printf("%d / %d = %d",num1,num2,result);
                } else {
                    double divisionResult = (double) num1 / num2;
                    System.out.printf("%d / %d = %.2f",num1,num2,divisionResult);
                }
                break;
        }
    }
}
举报

相关推荐

0 条评论