【Java】练习题(1)

【Java】练习题(1)

01:A+B Problem

总时间限制: 1000ms 内存限制: 65536kB
描述
Calculate a + b
输入
Two integer a,b (0 ≤ a,b ≤ 10)
输出
Output a + b
样例输入

1 2

样例输出

3

代码

import java.util.Scanner;

public class Main {
   

	public static void main(String[] args) {
   
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		System.out.println(a + b);
	}

}

02:输出第二个整数

总时间限制: 1000ms 内存限制: 65536kB
描述
输入三个整数,把第二个输入的整数输出。
输入
只有一行,共三个整数,整数之间由一个空格分隔。整数是32位有符号整数。
输出
只有一行,一个整数,即输入的第二个整数。
样例输入

123 456 789

样例输出

456

代码

import java.util.Scanner;

public class Main {
   
	public static void main(String[] args) {
   
		Scanner sc = new Scanner(System.in);
		sc.nextInt();
		System.out.println(sc.nextInt());
		sc.nextInt();

	}
}

方法二:

import java.util.Scanner;

public class Main {
   
	public static void main(String[] args) {
   
		Scanner in=new Scanner(System.in);
		int a=in.nextInt();
		int b=in.nextInt();
		int c=in.nextInt();
		System.out.print(b);
	}
}

03:数字统计

总时间限制: 1000ms 内存限制: 65536kB
描述
对于给定的一个字符串,统计其中数字字符出现的次数
输入
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
输出
对于每个测试实例,输出该串中数值的个数,每个输出占一行。
样例输入

2

asdfasdf123123asdfasdf

asdf111111111asdfasdfasdf

样例输出

6

9

代码

import java.util.Scanner;

public class Main {
   
	public static void main(String[] args) {
   
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		for (int i = 0; i < n; i++) {
   
			String s = sc.nextLine();
			System.out.println(count(s));
		}
	}

	static int count(String s) {
   
		int count = 0;
		for (int i = 0; i < s.length(); i++) {
   
			if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
   
				count++;
			}
		}
		return count;
	}
}

方法二:

import java.util.Scanner;
public class Main {
   
	public static 
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值