Java基础题目
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?
程序分析:兔子的规律为数列1,1,2,3,5,8,13,21….
public class Rabbit {
public static int f(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return f(n - 1) + f(n - 2);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println(f(n));
}
}
题目:判断101-200之间有多少个素数,并输出所有素数。
public class Primer {
public static boolean isPrimer(int n){
//判断一个数能否被小于sqrt(n)的数整除
int sqrt = (int)Math.sqrt(n);
for (int i = 2; i <= sqrt; i++) {
if(n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int count=0;
for (int i=101;i<=200;i++){
if(isPrimer(i)==true){
count++;
System.out.print(i);
System.out.print(" ");
}else {
continue;
}
}
System.out.print(count);
}
}
题目:打印出所有的”水仙花数”,所谓”水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个”水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
public class Flower {
public static boolean isFlower(int n) {
int i = n / 100;
int j = n / 10 - i * 10;
int k = n % 10;
if (n == Math.pow(i, 3) + Math.pow(j, 3) + Math.pow(k, 3)) {
return true;
}
return false;
}
public static void main(String[] args) {
for (int i = 100; i <= 999; i++) {
if (isFlower(i) == true) {
System.out.print(i);
System.out.print(" ");
} else {
continue;
}
}
}
}
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
public class DividePrimer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.print(n + "=");
for (int i = 2; i <= n; i++) {
while (n % i == 0 && n != i) {
n = n / i;
System.out.print(i + "*");
}
if (n == i) {
System.out.print(i);
break;
}
}
}
}
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:(a>b)?a:b这是条件运算符的基本例子。
public class Score {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
char level = (score >= 90) ? 'A' : (score < 60) ? 'C' : 'B';
System.out.println(level);
}
}
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。
public class BigDivider {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
if (m < n) {
int temp = m;
m = n;
n = temp;
}
int max = m % n;
int min = m * n;
while (max != 0) {
m = n;
n = max;
max = m % n;
}
max = n;
min = min / max;
System.out.println(max);
System.out.println(min);
}
}
题目:输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数。
public class CharClass {
public static void main(String[] args) {
int digit = 0;
int alpha = 0;
int space = 0;
int other = 0;
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= '0' && chars[i] <= '9') {//字符是数字
digit++;
} else if ((chars[i] >= 'A' && chars[i] <= 'Z') || (chars[i] >= 'a' && chars[i] <= 'z')) {
alpha++;
} else if (chars[i] == ' ') {
space++;
} else {
other++;
}
}
System.out.println("字母:" + alpha);
System.out.println("数字:" + digit);
System.out.println("空格:" + space);
System.out.println("其他:" + other);
}
}
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。输出结果的形式如:2+22+222=246;
public class Digital {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入参与运算的数1-9:");
int n = input.nextInt();
System.out.println("请输入运算的次数:");
int num = input.nextInt();
int nums = 0;
int sum = 0;
for (int i = 0; i < num; i++) {
nums += Math.pow(10, i) * n;
sum += nums;
System.out.print(nums);
if (i == num - 1) {
System.out.print("");
} else {
System.out.print("+");
}
}
System.out.print("=" + sum);
}
}
题目:一个数如果恰好等于它的因子之和,这个数就称为”完数”。例如6=1+2+3.编程找出1000以内的所有完数。
public class PreferDigit {
public static void main(String[] args) {
System.out.print("1000以内的所有完数为:");
for (int i = 1; i <= 1000; i++) {
int sum = 0;
for (int j = 1; j < i; j++) {
if (i % j == 0) {
sum += j;
}
}
if (i == sum) {
System.out.print(i);
System.out.print(" ");
}
}
}
}
题目:输出一行字符串,中间用空格隔开,统计最后一个单词的长度(好像是这么描述的)
public class WordLength {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = "";
while (input.hasNextLine()) {
s = input.nextLine();
System.out.println(s.length() - 1 - s.lastIndexOf(" "));
}
}
}
Java基础题目的更多相关文章
- java基础题目总结
有些基础题目由于工作中用的比较少但却又是不可少的,这样回答起来就会反应慢,不确定,不准确,特此开了文章记录遇到的不确定或者回答比较拗口的问题. 1.servlet是单例的吗,是安全的吗,是多线程吗 s ...
- java基础题目日常思考(持续更新)
public static void main(String[] args) { Integer a = 0; count(a); System.out.println(a); // 问题: a 输出 ...
- 50道JAVA基础编程练习题 - 题目
50道JAVA基础编程练习题[1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? [2]题目:判断 ...
- (Java基础--Spring阶段)常见面试题题目及解析整理(2021.03.12)
题目整理 Java基础进阶阶段 基础概念类 1.JDK1.8新特性? 2.面向对象和面向过程的区别? 3.什么是值传递和引用传递? 4.什么是不可变对象? 5.讲讲类的实例化顺序? 6.java 创建 ...
- java面试题目偏基础
一.JAVA基础篇-概念1.简述你所知道的Linux:Linux起源于1991年,1995年流行起来的免费操作系统,目前, Linux是主流的服务器操作系统, 广泛应用于互联网.云计算.智能手机(An ...
- [Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)
如若转载请注明出处: http://www.cnblogs.com/wang-meng/p/5898837.html 谢谢.上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大 ...
- 【JAVA面试题系列一】面试题总汇--JAVA基础部分
JAVA基础 基础部分的顺序: 基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法 线程的语法,集合的语法,io 的语法,虚拟机方面的语法 每天几道,持续更新!! 1.一个". ...
- 微冷的雨Java基础学习手记(一)
使用Java理解程序逻辑 之凌波微步 船舶停靠在港湾是很安全的,但这不是造船的目的 北大青鸟五道口原玉明老师出品 1.学习方法: 01.找一本好书 初始阶段不适合,可以放到第二个阶段,看到知识点时,要 ...
- Java基础&笔试题
这些题目是近期我参加过的笔试题和一些我在网上选的部分题,在这里做笔记,认真去学习,更好的应对后面的招聘.有错误欢迎指出. 一.Java基础部分 1.指针在任何情况下都可进行>,<,> ...
随机推荐
- 用python脚本测试接口
自己写一个脚本,统计调用200次接口的请求时长. # -*- coding=utf-8 -*-import osimport requestsimport time url = "http: ...
- centos源码安装git最新版
到 git官网下载git 源码安装包,git官网地址:https://www.git-scm.com/ 选择Tarballs系列的安装包,官网git下载:https://mirrors.edge.ke ...
- Django_视图
1. 视图 1.1 返回json数据 2. url配置 url组成 3. 获取 url参数 别名 4. url反向解析 接收参数 reverse 5. 视图总结 5.1 自定义错误页面 6. Http ...
- Led Night Light Factory: Traveler Led Night Light
Wake up in a strange hotel room in the evening and find the way to the bathroom, without stepping on ...
- 题解 P5587 【打字练习】
P5587 打字练习 想发一篇较为简洁易懂的题解,代码看起来长,实际上还是很好理解的,而且很多对称着写就行了 一道字符串签到题,比赛的时候小蒟蒻调了一个小时都没调出来一直RE,坑点还是不少的(主要是我 ...
- 【NS-3学习】ns3-模拟基础:关键概念,日志,命令行参数
前言 本篇博客先介绍在仿真过程中会使用到的一些关键概念,然后介绍便于调试仿真脚本的常用技术:日志.命令行参数. 关键概念 节点 在因特网术语中,主机(终端)是指任何一台连接到网络的计算设备.ns-3并 ...
- php对字符串的操作2之 处理字符串的内置函数
1,获取字串:substr($str,$start,$length) mb_substr($str,$start,$length,'utf-8'); 更换为utf8编码,能准确的截取中文 <?p ...
- 初识hadoop --- (分布式文件系统 + 分块计算)
[转载] + 整理 2016-11-18 使用范围: Hadoop典型应用有:搜索.日志处理.推荐系统.数据分析.视频图像分析.数据保存等. Hadoop历史 雏形开始于2002年的Apache的Nu ...
- 能使Oracle索引失效的七大限制条件
Oracle 索引的目标是避免全表扫描,提高查询效率,但有些时候却适得其反. 例如一张表中有上百万条数据,对某个字段加了索引,但是查询时性能并没有什么提高,这可能是 oracle 索引失效造成的.or ...
- AcWing 860. 染色法判定二分图
#include <cstring> #include <iostream> #include <algorithm> using namespace std; , ...