本文主要是介绍java入门经典例题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
经典例题
1.从键盘输入两个值并交换
public void test10() {
Scanner scanner = new Scanner(System.in);
System.out.println(
"请输入a的值:"
);
int a = scanner.nextInt();
System.out.println(
"请输入b的值:"
);
int
b = scanner.nextInt();
int temp = a;
a = b;
b = temp;
System.out.println(
"交换后的a:" + a
);
System.out.println(
"交换后的a:"
+ b);
}
2.编写input()方法,调用scnner()获取键盘输入值
public class Test01 {
public static void main(String[] arg) {
// char ch1 = 'a';
// char ch2 = '1';
// char ch3 = (char) (ch2 + 1);
// System.out.println(ch3);
Test01 t = new Test01();// 类的实例化
t.test11();// 交换,使用自己编写的input()方法实现***
}
//
从main()方法运行 实现输入交换 编写的input()方法***
public int input() {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
return Integer.parseInt(str);
}
public void test11() {
int a = input();
int b = input();
System.out.println(a);
System.out.println(b);
int temp = a;
a = b;
b = temp;
System.out.println("交换后的a:" + a);
System.out.println("交换后的a:" + b);
}
3.
查询月份的天数
public
void
test12() {
// 查询月份的天数
Scanner scanner =
new
Scanner(System.in);
System.out.println(
"请输入你要查询的月份:"
);
int
month = scanner.nextInt();
switch (month)
{
case
1
:
case
3
:
case
5
:
case
7
:
case
8
:
case
10
:
case
12
:
System.out.println(
"你要查询的月份有31天"
);
break
;
case 2:
System.out.println(
"请输入你要查询的年份:"
);
int
year = scanner.nextInt();
if
((year %
4
==
0
&& year %
100
!=
0
) || (year %
400
==
0
))
System.out.println(
"你要查询的月份有29天"
);
else
{
System.out.println(
"你要查询的月份有28天"
);
}
break
;
case
4
:
case
6
:
case
9
:
case
11
:
System.out.println(
"你要查询的月份有30天"
);
break
;
default
:
System.out.println(
"你输入的月份非法"
);
}
}
4.
猜数字
public
void
test14() {
int num = (int) (Math.random() * 100 + 1); // 随机生成一个1~100的整数
int
res = random.nextInt(
100
) +
1
;
//
随机生成一个
1~100
的整数
Scanner scanner =
new
Scanner(System.in);
while (true)
{
System.out.println(
"请输入一个1~100的整数:"
);
int
userInputNum = scanner.nextInt();
if
(userInputNum ==
0
) {
break
;
}
if
(userInputNum > num) {
System.out.println(
"大了!再猜~退出按0"
);
}
else if
(userInputNum < num) {
System.out.println(
"小了!再猜~退出按0"
);
}
else
{
System.out.println(
"恭喜你,猜对了!就是"
+ num);
break;
}
}
System.out.println(
"end"
);
}
5.输出正三角形
public
void
test19() {
Scanner scanner =
new
Scanner(System.in);
System.out.println(
"请输入一个整数得到正三角形:"
);
int
num = scanner.nextInt();
for
(
int
i =
1
; i <= num; i++) {
for
(
int
j =
1
; j <= (i + num -
1
); j++) {
if
(j <= (num - i)) {
System.out.print(
" "
);
}
else
{
System.out.print(
"*"
);
}
}
System.out.println();
}
}
6.打印7行的漏斗图形
public
void
test21() {
// 打印漏斗
要画坐标系找*和空格与i行的关系,列方程,移坐标轴(x左加右减y上加下减)x加绝对值就是关于轴对称的两个直线方程
for
(
int
i =
1
; i <=
7
; i++) {
for
(
int
j =
1
; j <= -
Math.abs(i - 4)
+
3
; j++) {
System.out.print(
" "
);
}
for
(
int
j =
1
; j <=
2
*
Math.abs(i - 4)
+
1
; j++) {
System.out.print(
"*"
);
}
System.out.println();
}
}
7.输出100-200素数
public
void
test26() {
// 使用循环打印100-200之间所有的素数(只能被1和自己整除的数叫素数)
for
(
int
i =
100
; i <=
200
; i++) {
for
(
int
j =
2
; j <=
Math.sqrt(i)
; j++) {
if
(i % j ==
0
) {
break
;
}
else
{
System.out.println(i);
break
;
}
}
}
}
@Test
public
void
test27() {
// 打印素数 方法二
for
(
int
i =
100
; i <=
200
; i++) {
boolean isPrime = true;
for
(
int
j =
2
; j <= Math.sqrt(i); j++) {
if
(i % j ==
0
) {
isPrime =
false
;
break
;
}
}
if
(isPrime ==
true
) {
System.out.println(i);
}
}
}
8.
求出数组中0-9分别出现的次数
public
void
test28() {
// Day3 作业1、 int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
// 求出上面数组中0-9分别出现的次数 双重for循环
int
[] count = {
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
};
int
[] scores = {
0
,
0
,
1
,
2
,
3
,
5
,
4
,
5
,
2
,
8
,
7
,
6
,
9
,
5
,
4
,
8
,
3
,
1
,
0
,
2
,
4
,
8
,
7
,
9
,
5
,
2
,
1
,
2
,
3
,
9
};
for
(
int
i =
0
; i <=
9
; i++) {
for
(
int
j =
0
; j <= scores.length -
1
; j++) {
if
(scores[j] == i) {
count[i]++;
}
}
}
for
(
int
i =
0
; i <=
9
; i++) {
System.out.println(i +
"出现的次数为:"
+ count[i]);
}
}
9.判断回文
public
void
test32() {
// 16、题目:一个5位数,判断它是不是回文数。
// 即12321是回文数,个位与万位相同,十位与千位相同。
char
[] array = {
'a'
,
'b'
,
'c'
,
'b'
,
'a'
};
boolean isPalindromes = true;
for
(
int
i =
0
; i <
array.length / 2
; i++) {
//
除二就可以,不用全遍历
if
(array[i] != array[array.length - i -
1
]) {
isPalindromes =
false
;
break
;
}
}
/** if (isPalindromes == true) {
System.out.println("是回文");
} else {
System.out.println("不是回文");
}
*/
System.out.println(isPalindromes?"是回文":"不是回文"); // 三目运算符
}
10.99乘法表(双重for循环)
public
void
test17() {
// 99乘法表
for
(
int
i =
1
; i <=
9
; i++) {
for
(
int
j =
1
; j <= i; j++) {
System.out.print(j +
"*"
+ i +
"="
+ (i * j) +
"\t"
);
}
System.out.println();
}
}
11.冒泡排序
public
void
testBubble() {
// 冒泡排序 双重for
int
[] array = {
40
,
17
,
21
,
1
};
for
(
int
i =
1
; i <= array.length -
1
; i++) {
for
(
int
j =
0
; j < array.length - i; j++) {
if
(array[j] > array[j +
1
]) {
int
temp = array[j];
array[j] = array[j +
1
];
array[j +
1
] = temp;
}
}
}
for (int temp : array)
{
//增强for循环遍历数组,把数组中的元素依次传给变量temp输出
System.out.println(temp);
}
}
12.学生类,从键盘输入学生数据,之后按属性查询学生信息
public
class
Student {
// 属性 (描述 静态)
int
id;
String name;
int
age;
String gender;
// 方法 (动作 动态)
public
void
study(){
//id是1的张三,年龄是23,正在学习
System.out.println(
"id是"
+id+
"年龄是"
+age+
"性别是"
+gender+
"的"
+name+
"正在学习"
);
}
@Override
// toString()作用 返回字符串打印用
// 快捷键 右键 generate 选 toString() 自动生成toString()方法
public
String toString() {
return
"Student{"
+
"id="
+ id +
", name='"
+ name + '\
''
+
", age="
+ age +
", gender='"
+ gender + '\
''
+
'}'
;
}
}
/** 输入1.按照id来搜索,找到就打印,没有找到就输出没有该信息的学生
输入2.按照name来搜索,找到就打印,没有找到就输出没有该信息的学生
输入3.按照age来搜索,找到就打印,没有找到就输出没有该信息的学生
输入
4.按照gender来搜索,找到就打印,没有找到就输出没有该信息的学生*/
@Test
public
void
test03() {
Scanner scanner =
new
Scanner(System.in);
System.out.println(
"请输入学生的个数"
);
int
count = scanner.nextInt();
Student[] students =
new
Student[count];
// 数组初始是count个null
for
(
int
i =
0
; i < students.length; i++) {
Student student =
new
Student();
System.out.println(
"请输入id:"
);
student.id = scanner.nextInt();
System.out.println(
"请输入名字:"
);
student.name = scanner.next();
System.out.println(
"请输入年龄:"
);
student.age = scanner.nextInt();
System.out.println(
"请输入性别:"
);
student.gender = scanner.next();
students[i] = student;
// 把输入填进去
}
for
(
int
i =
0
; i < students.length; i++) {
Student student = students[i];
// System.out.println(student.id);
// System.out.println(student.name);
// System.out.println(student.age);
// System.out.println(student.gender);
System.out.println(student.toString());
// System.out.println(student); /* 如果生成了toString()方法可以直接输出student */
}
while
(
true
) {
System.out.println(
"----查询学号请按1----"
);
System.out.println(
"----查询姓名请按2----"
);
System.out.println(
"----查询年龄请按3----"
);
System.out.println(
"----查询性别请按4----"
);
System.out.println(
"--退出查询请按其他键--"
);
int
type = scanner.nextInt();
boolean isFoundIt = false;
switch
(type) {
case
1
:
System.out.println(
"请输入你要查询的学号:"
);
int
id = scanner.nextInt();
for
(
int
i =
0
; i < students.length; i++) {
Student student = students[i];
if
(id == student.id) {
isFoundIt =
true
;
System.out.println(student);
break
;
}
}
// if (!isFoundIt) {
if
(isFoundIt ==
false
) {
System.out.println(
"没有该信息的学生!!!"
);
}
break
;
case
2
:
System.out.println(
"请输入你要查询的姓名:"
);
String
name = scanner.next();
for
(
int
i =
0
; i < students.length; i++) {
Student student = students[i];
if (name.
equals(student.name))
{
System.out.println(student);
isFoundIt =
true
;
}
}
if
(isFoundIt ==
false
) {
System.out.println(
"没有该信息的学生!!!"
);
}
break
;
case
3
:
System.out.println(
"请输入你要查询的年龄:"
);
int
age = scanner.nextInt();
for
(
int
i =
0
; i < students.length; i++) {
Student student = students[i];
if
(age == student.age) {
System.out.println(student);
isFoundIt =
true
;
}
}
if
(isFoundIt ==
false
) {
System.out.println(
"没有该信息的学生!!!"
);
}
break
;
case
4
:
System.out.println(
"请输入你要查询的性别:"
);
String gender = scanner.next();
for
(
int
i =
0
; i < students.length; i++) {
Student student = students[i];
if
(gender.equals(student.gender)){
System.out.println(student);
isFoundIt =
true
;
}
}
if
(isFoundIt ==
false
) {
System.out.println(
"没有该信息的学生!!!"
);
}
break
;
default:
return
; // return结束程序,如果是break只能跳出switch,
// 还会执行while语句会无限查询
}
}
}
这篇关于java入门经典例题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!