赞
踩
练习1:判断输入的值是否是偶数,另外,要处理输入错误
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个数字:"); if(input.hasNextInt()){ int num=input.nextInt(); if(num%2==0){ System.out.println("是偶数"); }else { System.out.println("是奇数"); } }else { System.out.println("输入无效!"); } } }
练习2:输入并输出姓名、年龄、身高
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入姓名:");
String name=input.next();
System.out.println("请输入年龄:");
String age=input.next();
System.out.println("请输入身高:");
double height=input.nextDouble();
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
System.out.println("身高:"+height);
}
}
练习3:从控制台获取Java、Oracle、HTML三门课程的成绩,计算总分和平均分(平均分保留2位小数,要求四舍五入)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入Java成绩: "); int javaScore=input.nextInt(); System.out.print("请输入Oracle成绩: "); int oracleScore=input.nextInt(); System.out.print("请输入HTML成绩: "); int htmlScore=input.nextInt(); System.out.println("-------------------------------- "); System.out.println("Java Oracle HTML"); System.out.println(); System.out.println(""+javaScore+"\t"+oracleScore+"\t"+htmlScore); System.out.println("-------------------------------- "); int sum=javaScore+oracleScore+htmlScore; double average=sum/3.0; String result = String.format("%.2f", average); System.out.println("总成绩是:"+sum); System.out.println("平均成绩是:"+result); } }
练习4:输入一个数字,反转输出,如:输入123,输出321
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("请输入一个整数: ");
String num=input.next();
for(int i = num.length()-1;i >= 0;i--)
{
System.out.print(num.charAt(i));
}
}
}
练习5:输入tom和jack的年龄,比较年龄并输出相差多少岁,要求使用条件运算符
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入tom的年龄: "); int tomAge=input.nextInt(); System.out.print("请输入jack的年龄: "); int jackAge=input.nextInt(); int num=tomAge-jackAge; if(num==0){ System.out.println("tom与jack一样大"); }else { if (num>0) { System.out.println("tom比jack大"+num+"岁"); }else { System.out.println("tom比jack小"+num+"岁"); } } } }
练习6:交换两个数值变量的值(至少两种方法)
public class Main { public static void main(String[] args) { // 方法一 int a=1,b=2; int c=a; a=b; b=c; System.out.println(a); System.out.println(b); // 方法二 int d=1,f=2; d=d+f; f=d-f; d=d-f; System.out.println(d); System.out.println(f); } }
练习7:下面结果分别是?为什么?
int a = 97;
char b = a;
System.out.println(b);
报错,int类型无法赋值char
char c = 97;
System.out.println(c);
*char为字符类型,97为a,最后输出为a;
int m = 3 ;
int n = m++ + ++m;
System.out.println( "m:" +m+ ", n:" +n);
++m:先自加1再进行运算
m++:先运算后再自加1
先计算(m++)中。前面m=3,(m++)就是先m=3参与计算,再自加1,原式等价于:n=3+(m++),同时m的值变成了5(自加1)。
m:5, n:8
练习1:提示用户输入三个整数,判断并输出最大值、最小值
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入第一个整数: "); int oneNum = input.nextInt(); System.out.print("请输入第二个整数: "); int twoNum = input.nextInt(); System.out.print("请输入第三个整数: "); int treeNum = input.nextInt(); int maxNum = 0; int mixNum=0; if(oneNum>twoNum){ if (oneNum>treeNum){ maxNum=oneNum; }else { maxNum=treeNum; } }else { if(twoNum>treeNum){ maxNum=twoNum; }else { maxNum=treeNum; } } if(oneNum>twoNum){ if (twoNum>treeNum){ mixNum=treeNum; }else { mixNum=treeNum; } }else { if(oneNum>treeNum){ mixNum=treeNum; }else { mixNum=oneNum; } } System.out.println("最大数为:"+maxNum); System.out.println("最小数为:"+mixNum); } }
练习2:如果今天是周日且晴天,就出去嗨
如果今天是周日但不是晴天,睡懒觉
如果今天不是周日,继续996
说明:天气、星期几的变量,自己定义即可;主要练习if嵌套
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入周几:"); int num = input.nextInt(); System.out.print("请输入天气: "); String weather = input.next(); if(num>0){ if (num<=5) { System.out.println("今天是工作日,天气" + weather + "继续996"); }else if(num>6&&num<8) { if (weather == "晴天") { System.out.println("如果今天是周日,天气" + weather + "出去嗨"); }else { System.out.println("如果今天是周日,天气" + weather + "睡懒觉"); } } }else { System.out.println("请输入一到七的日期数字"); } } }
练习3:由键盘输入三个整数分别存入变量num1、num2、num3,对它们进行排序(使用 if-else if-else),并且从小到大输出。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入第一个整数:"); int num1 = input.nextInt(); System.out.print("请输入第二个整数:"); int num2 = input.nextInt(); System.out.print("请输入第三个整数:"); int num3 = input.nextInt(); System.out.println("您输入的3个整数为:"+num1+","+num2+","+num3); if(num1 > num2) { if (num3 > num1) { System.out.println("从小到大的排序:" + num2 + "," + num1 + "," + num3); } else if (num3 < num2) { System.out.println("从小到大的排序:" + num3 + "," + num2 + "," + num1); } else { System.out.println("从小到大的排序:" + num2 + "," + num3 + "," + num1); } }else{ if(num3 > num2){ System.out.println("从小到大的排序:"+num1 + "," + num2 + "," + num3 ); }else if(num3 < num1){ System.out.println("从小到大的排序:"+num3 + "," + num1 + "," + num2 ); }else{ System.out.println("从小到大的排序:"+num1 + "," + num3 + "," + num2 ); } } } }
练习4:提示用户输入三角形的边a、b、c,范围必须在 [1,100) 之间,判断三角形的类型:1.非三角形;2.等边三角形;3.直角三角形;4.等腰三角形;5.普通三角形
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入第一个边:"); int num1 = input.nextInt(); System.out.print("请输入第二个边:"); int num2 = input.nextInt(); System.out.print("请输入第三个边:"); int num3 = input.nextInt(); if ((num1 >= 1 && num1 < 100) && (num2 >= 1 && num2 <= 100) && (num3 >= 1 && num3 < 100)) { if(num1+num2>num3||num1+num3>num2||num3+num2>num1){ if(num1==num2&&num2==num3&&num1==num3){ System.out.println("这是等边三角形!"); }else if (num1*num1+num2*num2==num3*num3||num3*num3+num2*num2==num1*num1||num1*num1+num3*num3==num2*num2) { System.out.println("这是直角三角形!"); }else if (num1==num1||num2==num3||num1==num3){ System.out.println("这是等腰三角形!"); }else { System.out.println("这是普通三角形!"); } }else { System.out.println("这不是三角形!"); } }else { System.out.println("请输入规定数值的数字"); } } }
练习1:周一跑步,周二、三、四打羽毛球,周五游泳,其它休息
import java.util.Scanner; public class Main { public static void main(String[] args) { int today=7; switch (today){ case 1: System.out.println("周一跑步"); break; case 2: System.out.println("周二、三、四打羽毛球"); break; case 3: System.out.println("周二、三、四打羽毛球"); break; case 4: System.out.println("周二、三、四打羽毛球"); break; default: System.out.println("其它休息"); } } }
练习2:输入 2022 年的某个月份和日期,例如 month=4,day=21,经过程序计算,打印出输入的月份和日期是 2022年的第几天:31+28+31+21
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入月:"); int monNum = input.nextInt(); System.out.print("请输入日:"); int dayNum = input.nextInt(); int num=0; if(dayNum>=0&dayNum<=31) { switch (monNum) { case 1: num = dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 2: if (dayNum >= 0 & dayNum <= 28) { num = 31 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; } else { System.out.println(+monNum + "月2022只有28天"); break; } case 3: num = 31 + 28 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 4: num = 31 + 28 + 31 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 5: num = 31 + 28 + 31 + 30 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 6: num = 31 + 28 + 31 + 30 + 31 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 7: num = 31 + 28 + 31 + 30 + 31 + 30 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 8: num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 9: num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 10: num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 11: num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; case 12: num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + dayNum; System.out.println(+monNum + "月" + dayNum + "日是2022的第" + num + "天"); break; default: System.out.println( "请输入0-12数字"); break; } } } }
练习1:输出比i(i=5)小的正整数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个数:");
int num=input.nextInt();
for (int i=num-1;i>0;i--){
System.out.println(i);
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。