1, 编程实现调和数列的累加
import java. util. Scanner; public class While {
public static void main ( String[ ] args) {
System. out. println ( "请输入想要计算的项数n:" ) ; Scanner sc = new Scanner ( System. in) ; int n = sc. nextInt ( ) ; double sum = 0.0 ; int i = 1 ; while ( i <= n) {
sum += ( 1.0 / i) ; i++ ; } System. out. println ( "前" + n + "项的和为:" + sum) ; }
}
2, 编程实现,出租车计费系统
import java. util. Scanner; public class TaxiPrice {
public static void main ( String[ ] args) {
System. out. println ( "请输入公里数和等候秒数:" ) ; Scanner sc = new Scanner ( System. in) ; int km = sc. nextInt ( ) ; int sec = sc. nextInt ( ) ; int kmprice, secprice; if ( km <= 3 ) {
kmprice = 13 ; } else if ( km > 3 && km <= 15 ) {
kmprice = ( km - 3 ) * 2 + 13 ; } else {
kmprice = ( km - 15 ) * 3 + 24 + 13 ; } secprice = sec / 150 ; System. out. println ( km + "公里且等候时间" + sec + "秒,总计车费为:" + ( secprice + kmprice) ) ; }
}
3, 编程实现,switch 实现字符界面
import java. util. Scanner; public class SwitchMenu {
public static void main ( String[ ] args) {
System. out. println ( " " ) ; System. out. println ( " 欢迎来到拉勾教育" ) ; System. out. println ( "-------------------------------------" ) ; System. out. print ( " [1]学员系统" ) ; System. out. println ( " [2]管理员系统" ) ; System. out. println ( " [0]退出系统" ) ; System. out. println ( "-------------------------------------" ) ; System. out. println ( "请选择要进入的系统:" ) ; Scanner sc = new Scanner ( System. in) ; int choose = sc. nextInt ( ) ; switch ( choose) {
case 1 : System. out. println ( "正在进入 [1]学员系统 ..." ) ; break ; case 2 : System. out. println ( "正在进入 [2]管理员系统 ..." ) ; break ; case 0 : System. out. println ( "谢谢使用,下次再见!" ) ; break ; default : System. out. println ( "输入有误,请重新输入" ) ; } }
}
4, 编程实现,使用switch语句判断等级
import java. util. Scanner; public class SwitchLvJudge {
public static void main ( String[ ] args) {
System. out. println ( "请输入考试成绩:" ) ; Scanner sc = new Scanner ( System. in) ; int score = sc. nextInt ( ) ; char Lv = 'F' ; switch ( score / 10 ) {
case 10 : / / Lv = 'A' ; case 9 : Lv = 'A' ; break ; case 8 : Lv = 'B' ; break ; case 7 : Lv = 'C' ; break ; case 6 : Lv = 'D' ; break ; default : Lv = 'E' ; } System. out. println ( "您所在的等级为:" + Lv) ; System. out. println ( "-----------------------------------------------------------------------" ) ; System. out. println ( "世界上最痴情的等待,就是我当case你当switch,或许永远都不会选到自己!" ) ; }
}
5, 编程实现,使用switch语句判断等级
import java. util. Scanner; public class ReverNum {
public static void main ( String[ ] args) {
System. out. println ( "请输入任意正整数" ) ; Scanner sc = new Scanner ( System. in) ; int num = sc. nextInt ( ) ; while ( num > 0 ) {
System. out. print ( num % 10 ) ; num /= 10 ; } }
}
6, 编程实现等级判断
import java. util. Scanner; public class LvJudge {
public static void main ( String[ ] args) {
System. out. println ( "请输入考试成绩:" ) ; Scanner sc = new Scanner ( System. in) ; int score = sc. nextInt ( ) ; char Lv = 'F' ; if ( score < 60 ) {
Lv = 'E' ; } else if ( score < 70 ) {
Lv = 'D' ; } else if ( score < 80 ) {
Lv = 'C' ; } else if ( score < 90 ) {
Lv = 'B' ; } else if ( score <= 100 ) {
Lv = 'A' ; } System. out. println ( "您所在的等级为:" + Lv) ; }
}
7, 编程使用if分支结构模拟网吧上网的过程
import java. util. Scanner; public class IfTest {
public static void main ( String[ ] args) {
System. out. println ( "请输入您的年龄:" ) ; Scanner sc = new Scanner ( System. in) ; int age = sc. nextInt ( ) ; if ( age >= 18 ) {
System. out. println ( "开心的浏览起了网页..." ) ; } System. out. println ( "美好的时光总是短暂的" ) ; } }
8, 编程实现,输入薪水,计算个人所得税并打印
import java. util. Scanner; public class Ifsalary {
public static void main ( String[ ] args) {
System. out. println ( "请输入本月薪水:" ) ; Scanner sc = new Scanner ( System. in) ; int salary = sc. nextInt ( ) ; double gainrate = 0.0 ; if ( salary <= 5000 ) {
gainrate = 0.0 ; } else if ( salary > 5000 && salary <= 8000 ) {
gainrate = ( salary - 5000 ) * 0.03 - 0 ; } else if ( salary > 8000 && salary <= 17000 ) {
gainrate = ( salary - 5000 ) * 0.1 - 210 ; } else if ( salary > 17000 && salary <= 30000 ) {
gainrate = ( salary - 5000 ) * 0.2 - 1410 ; } else if ( salary > 30000 && salary <= 40000 ) {
gainrate = ( salary - 5000 ) * 0.25 - 2660 ; } else if ( salary > 40000 && salary <= 60000 ) {
gainrate = ( salary - 5000 ) * 0.3 - 4410 ; } else if ( salary > 60000 && salary <= 85000 ) {
gainrate = ( salary - 5000 ) * 0.35 - 7160 ; } else {
gainrate = ( salary - 5000 ) * 0.45 - 15160 ; } System. out. println ( "本月纳税额为:" + gainrate) ; }
}
9, 编程实现,提示输入一个整数,使用if else判断正负并输出
import java. util. Scanner; public class IfMinus {
public static void main ( String[ ] args) {
System. out. println ( "请输入一个整数:" ) ; Scanner sc = new Scanner ( System. in) ; int num = sc. nextInt ( ) ; if ( num < 0 ) {
System. out. println ( num + "是一个负数" ) ; } else {
if ( 0 == num) {
System. out. println ( num + "是零" ) ; } else {
System. out. println ( num + "是一个正数" ) ; } } }
}
10, 编程实现,提示用户输入两个整数,使用if分支结构找到最大值并打印出来
import java. util. Scanner; public class IfMax {
public static void main ( String[ ] args) {
System. out. println ( "请输入两个整数:" ) ; Scanner sc = new Scanner ( System. in) ; int num1 = sc. nextInt ( ) ; int num2 = sc. nextInt ( ) ; int max = num1; if ( num2 >= max) {
max = num2; } System. out. println ( "最大值为:" + max) ; }
}
11, 编程实现 if else if else分支结构的使用,模拟购票
import java. util. Scanner; public class IfElseIfElse {
public static void main ( String[ ] args) {
System. out. println ( "请输入您的身份信息:" ) ; Scanner sc = new Scanner ( System. in) ; String str = sc. next ( ) ; if ( "军人" . equals ( str) ) {
System. out. println ( "免费乘坐" ) ; } else if ( "学生" . equals ( str) ) {
System. out. println ( "半价购票" ) ; } else {
System. out. println ( "请购买全票" ) ; } System. out. println ( "坐上了火车去拉萨,去看那美丽的布达拉 " ) ; }
}
12, 编程使用if else分支结构来模拟考试成绩查询过程
import java. util. Scanner; public class IfElse {
public static void main ( String[ ] args) {
System. out. println ( "请输入您的考试成绩:" ) ; Scanner sc = new Scanner ( System. in) ; int score = sc. nextInt ( ) ; if ( score >= 60 ) {
System. out. println ( "恭喜你考试通过了" ) ; } else {
System. out. println ( "下学期来补考吧" ) ; } System. out. println ( "世界上最遥远的距离不是生与死,而是你在if我在else,似乎一直相伴却又永远分离 " ) ; }
}
13, 编程实现for循环的使用,模拟玩游戏的过程
public class ForTest {
public static void main ( String[ ] args) throws Exception {
for ( int i = 1 ; i <= 10 ; i++ ) {
System. out. println ( "今晚吃鸡,大吉大利,正在进行第" + i + "场游戏..." ) ; Thread. sleep ( 5000 ) ; System. out. println ( "本场游戏结束!\n\n\n" ) ; } System. out. println ( "该休息了,否则明天就要迟到了" ) ; }
}
14, 编程实现,1到10000的累加并打印
public class ForSum {
public static void main ( String[ ] args) {
int sum = 0 ; for ( int i = 1 ; i <= 10000 ; i++ ) {
sum += i; } System. out. println ( "1到10000的和为:" + sum) ; }
}
15, 编程实现,打印1-100的所有奇数
public class ForNum {
public static void main ( String[ ] args) {
int count = 0 ; for ( int i = 1 ; i < 101 ; i++ ) {
if ( ( i % 2 ) != 0 ) {
System. out. print ( i + " " ) ; count++ ; if ( 10 == count) {
System. out. println ( "" ) ; count = 0 ; } } } System. out. println ( "------------------------------------" ) ; for ( int i = 1 ; i <= 100 ; i += 2 ) {
System. out. print ( i + " " ) ; count++ ; if ( 10 == count) {
System. out. println ( "" ) ; count = 0 ; } } System. out. println ( "------------------------------------" ) ; for ( int i = 1 ; i <= 50 ; i++ ) {
System. out. print ( i * 2 - 1 + " " ) ; count++ ; if ( 10 == count) {
System. out. println ( "" ) ; count = 0 ; } } }
}
16, 编程实现,使用for打印1~20之间所有整数,遇到5的倍数则跳过不打印
public class ForJump5 {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 20 ; i++ ) {
if ( 0 == i % 5 ) {
continue ; } System. out. println ( "i = " + i) ; } }
}
17, 编程使用for循环实现猜数字游戏
import java. util. Random;
import java. util. Scanner; public class ForGuess {
public static void main ( String[ ] args) {
Random ra = new Random ( ) ; int temp = ra. nextInt ( 100 ) + 1 ; int count = 0 ; System. out. println ( "\n请输入一个猜测数(1~100之间):" ) ; Scanner sc = new Scanner ( System. in) ; int num = sc. nextInt ( ) ; for ( ; ; ) {
if ( num > temp) {
System. out. println ( "太...太大了,再试一次吧!\n" ) ; num = sc. nextInt ( ) ; count++ ; } else if ( num < temp) {
System. out. println ( "太...太小了,再试一次吧!\n" ) ; num = sc. nextInt ( ) ; count++ ; } else {
count++ ; System. out. println ( "恭喜,大小刚刚好!您一共尝试了" + count + "次\n\n" ) ; break ; } } if ( 1 == count) {
System. out. println ( "你果然棒棒" ) ; } else if ( count <= 6 ) {
System. out. println ( "666,继续加油!" ) ; } else {
System. out. println ( "菜鸡,多来几次吧" ) ; } }
}
18, 编程使用双重for打印星星
public class ForForStar {
public static void main ( String[ ] args) {
System. out. println ( ) ; for ( int i = 1 ; i <= 5 ; i++ ) {
for ( int j = 1 ; j <= 5 ; j++ ) {
System. out. print ( "*" ) ; } System. out. println ( ) ; } System. out. println ( ) ; System. out. println ( "-------------------------------------" ) ; System. out. println ( ) ; for ( int i = 1 ; i <= 5 ; i++ ) {
for ( int j = 1 ; j <= i; j++ ) {
System. out. print ( "*" ) ; } System. out. println ( ) ; } System. out. println ( ) ; System. out. println ( "-------------------------------------" ) ; System. out. println ( ) ; for ( int i = 1 ; i <= 5 ; i++ ) {
for ( int j = 1 ; j <= 6 - i; j++ ) {
System. out. print ( "*" ) ; } System. out. println ( ) ; } System. out. println ( ) ; System. out. println ( "-------------------------------------" ) ; System. out. println ( ) ; for ( int i = 1 ; i <= 5 ; i++ ) {
for ( int j = 1 ; j <= 5 - i; j++ ) {
System. out. print ( " " ) ; } for ( int k = 1 ; k <= 2 * i - 1 ; k++ ) {
System. out. print ( "*" ) ; } System. out. println ( ) ; } }
}
19, 打印2~100之间所有的素数
public class ForForPrime {
public static void main ( String[ ] args) {
for ( int i = 2 ; i <= 100 ; i++ ) {
boolean flag = true ; for ( int j = 2 ; j <= Math. sqrt ( i) ; j++ ) {
if ( 0 == i % j) {
flag = false ; break ; } } if ( flag) {
System. out. println ( i + "是素数" ) ; } } System. out. println ( "------------------------------------------------------" ) ; }
}
20, 使用双重for打印99乘法表
public class ForFor99 {
public static void main ( String[ ] args) {
System. out. println ( "-----------------------------------------------------------------------" ) ; System. out. println ( ) ; outer: for ( int i = 1 ; i <= 9 ; i++ ) {
for ( int j = 1 ; j <= i; j++ ) {
System. out. print ( i + "*" + j + "=" + ( i * j) + " " ) ; if ( 6 == j) {
break outer; } } System. out. println ( ) ; } System. out. println ( ) ; System. out. println ( "-----------------------------------------------------------------------" ) ; System. out. println ( ) ; for ( int i = 1 ; i <= 9 ; i++ ) {
for ( int j = 1 ; j <= 10 - i; j++ ) {
System. out. print ( i + "*" + j + "=" + ( i * j) + " " ) ; } System. out. println ( ) ; } System. out. println ( "-------------------------------------" ) ; System. out. println ( ) ; for ( int i = 1 ; i <= 9 ; i++ ) {
for ( int k = 1 ; k <= 9 - i; k++ ) {
System. out. print ( " " ) ; } for ( int j = 1 ; j <= i; j++ ) {
System. out. print ( i + "*" + j + "=" + ( i * j) + " " ) ; } System. out. println ( ) ; } System. out. println ( "-------------------------------------" ) ; System. out. println ( ) ; for ( int i = 1 ; i <= 9 ; i++ ) {
for ( int k = 1 ; k <= i - 1 ; k++ ) {
System. out. print ( " " ) ; } for ( int j = i; j <= 9 ; j++ ) {
System. out. print ( i + "*" + j + "=" + ( i * j) + " " ) ; } System. out. println ( ) ; } }
}
21, 编程实现双重for循环的使用
public class ForFor {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 5 ; i++ ) {
for ( int j = 1 ; j <= 5 ; j++ ) {
System. out. print ( "厉害了我的哥 " ) ; } System. out. println ( "" ) ; } }
}
22, 编程,打印三位数中所有水仙花数
public class ForFlower {
public static void main ( String[ ] args) {
int count = 0 ; System. out. println ( "三位数中所有水仙花数是:" ) ; for ( int i = 100 ; i <= 999 ; i++ ) {
int i1 = i / 100 ; int i2 = ( i % 100 ) / 10 ; int i3 = i % 10 ; if ( i == i1 * i1 * i1 + i2 * i2 * i2 + i3 * i3 * i3) {
System. out. print ( " " + i) ; count++ ; if ( 5 == count) {
System. out. println ( "" ) ; count = 0 ; } } } }
}
23, 不断提示用户输入新内容,bye退出
import java. util. Scanner; public class ForBye {
public static void main ( String[ ] args) {
boolean flag = true ; for ( ; ; ) {
System. out. println ( "请" + ( flag? "张三" : "李四" ) + "输入新内容:" ) ; Scanner sc = new Scanner ( System. in) ; String str = sc. next ( ) ; if ( "bye" . equals ( str) ) {
System. out. println ( "已退出,谢谢使用!" ) ; break ; } System. out. println ( ( flag? "张三说:" : "李四说:" ) + str + "\n\n\n" ) ; flag = ! flag; } }
}
24, 编程使用dowhile来模拟学习效果的检查
import java. util. Scanner; public class DoWhileCheck {
public static void main ( String[ ] args) throws Exception{
String msg = null; do {
System. out. println ( "正在疯狂学习中..." ) ; Thread. sleep ( 5000 ) ; System. out. println ( "是否合格?(y/n)" ) ; Scanner sc = new Scanner ( System. in) ; msg = sc. next ( ) ; } while ( ! "y" . equals ( msg) ) ; System. out. println ( "恭喜任务合格!" ) ; System. out. println ( "-------------------------------------------------------" ) ; int = 1 ; while ( i <= 10000 ) {
System. out. println ( "I love You !" ) ; } }
}