1.分析以下需求,并用代码实现:
(1)打印1到100之内的整数,但数字中包含9的要跳过
(2)每行输出5个满足条件的数,之间用空格分隔
(3)如:1 2 3 4 5
class Noname1 {
public static void main(String[] args) {
int j = 0;
for (int i = 1; i <= 100; i++ )
{
if (i/10==9||i%10==9) {
continue;//遇到数字包含9的就跳出循环
}
System.out.print(i+"\t");
j++;
if (j%5==0) {//每行输出5个满足条件的数,之间用空格分隔
System.out.println();
}
}
}
}
2.分析以下需求,并用代码实现:
(1)按照从大到小的顺序输出四位数中的个位+百位=十位+千位(3553,2332,1166,8228,3773)的数字及个数
(2)每行输出5个满足条件的数,之间用空格分隔
(3)如:9999 9988 9977 9966 9955
class Noname1 {
public static void main(String[] args) {
int count = 0;
int qian,bai,shi,ge;
for (int i = 9999; i >= 1000; i-- )
{
//选出个,十,百,千号位
qian = i/10/10/10;
bai = i/10/10%10;
shi = i/10%10;
ge = i%10;
//判断该数字是否满足条件
if (bai + ge == shi + qian) {
System.out.print(i+" ");
}else{continue;}
//是否应该换行
count++;
if (count%5==0) {
System.out.println();
}
}
System.out.print(count);
}
}
3.分析以下需求,并用代码实现:
(1)珠穆朗玛峰高度为8848米,有一张足够大的纸,厚度为0.001米。
(2)请问,我折叠多少次,可以折成珠穆朗玛峰的高度。
class Noname1 {
public static void main(String[] args) {
double height = 0.001;
int count = 0;
while (height < 8848.0) {
height*=2;
count++;
}
System.out.print(count);
}
}
24次
4.分析以下需求,并用代码实现:
(1)倒着打印九九乘法表
class Noname1 {
public static void main(String[] args) {
for (int a = 9;a >= 1 ; a--)
{
for (int b=1;b <=a ; b++)
{
System.out.print(a+"X"+b+"="+(a*b)+"\t");
}
System.out.println();
}
}
}
5.分析以下需求,并用代码实现:
(1) 计算15+25+35+....+1005的和//为51000;
class Noname1 {
public static void main(String[] args) {
int sum = 0;
for (int i = 15; i <= 1005; i+=10 )
{
sum += i;
System.out.println(i+" ");
}
System.out.println(sum);
}
}
6.分析以下需求,并用代码实现:
(1)打印倒直角三角形
*****
****
***
**
*
class Noname1 {
public static void main(String[] args) {
for (int a = 5;a >= 1 ; a--)
{
for (int b=1;b <=a ; b++)
{
System.out.print("*");
}
System.out.println();
}
}
}
7.分析以下需求,并用代码实现(每个小需求都要封装成一个方法):
(1)求两个数据之和(两个整数或者两个小数)
(2)判断两个数据是否相等(两个整数或者两个小数)
(3)获取两个数中较大的值(两个整数或者两个小数)
(4)打印m行n列的星形矩形
(5)打印nn乘法表
import java.util.Scanner;
class Noname1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请问需要什么服务?" + "\n" + "1.求两个数据之和(两个整数或者两个小数)" + "\n"
+ "2.判断两个数据是否相等(两个整数或者两个小数)" + "\n"
+ "3.获取两个数中较大的值(两个整数或者两个小数)" + "\n" + "4.打印m行n列的星形矩形" + "\n"
+ "5.打印nn乘法表");
String function = sc.next();
switch (function) {
case "1":
System.out.println("输入整数请按1,小数请按2");
int selector1 = sc.nextInt();
if (selector1 == 1) {
System.out.println("请输入第一个整数:");
int a1 = sc.nextInt();
System.out.println("请输入第二个整数:");
int b1 = sc.nextInt();
System.out.println("两数之和为:" + plus(a1,b1));
} else if (selector1 == 2) {
System.out.println("请输入第一个小数:");
double a1 = sc.nextDouble();
System.out.println("请输入第二个小数:");
double b1 = sc.nextDouble();
System.out.println("两数之和为:" + plus(a1,b1));
} else {
System.out.println("乱敲啥");
}
break;
case "2":
System.out.println("输入整数请按1,小数请按2");
int selector2 = sc.nextInt();
if (selector2 == 1) {
System.out.println("请输入第一个整数:");
int a2 = sc.nextInt();
System.out.println("请输入第二个整数:");
int b2 = sc.nextInt();
if (equals(a2, b2)) {
System.out.println("两数相等");
} else {
System.out.println("两数不相等");
}
} else if (selector2 == 2) {
System.out.println("请输入第一个小数:");
double a2 = sc.nextDouble();
System.out.println("请输入第二个小数:");
double b2 = sc.nextDouble();
if (equals(a2, b2)) {
System.out.println("两数相等");
} else {
System.out.println("两数不相等");
}
} else {
System.out.println("乱敲啥");
}
break;
case "3":
System.out.println("输入整数请按1,小数请按2");
int selector = sc.nextInt();
if (selector == 1) {
System.out.println("请输入第一个整数:");
int a3 = sc.nextInt();
System.out.println("请输入第二个整数:");
int b3 = sc.nextInt();
System.out.println("最大值为:" + getMax(a3, b3));
} else if (selector == 2) {
System.out.println("请输入第一个小数:");
double a3 = sc.nextDouble();
System.out.println("请输入第二个小数:");
double b3 = sc.nextDouble();
System.out.println("最大值为:" + getMax(a3, b3));
} else {
System.out.println("乱敲啥");
}
// double max = getMax(a3,b3);
break;
case "4":
System.out.println("请输入?行(必须要整数)");
int n = sc.nextInt();
System.out.println("请输入?列(必须要整数)");
int m = sc.nextInt();
printTheStar(n, m);
break;
case "5":
System.out.println("请输入?行(必须要整数)");
int n1 = sc.nextInt();
printTheMN(n1);
break;
default:
System.out.println("无此服务,谢谢惠顾!");
break;
}
}
public static int plus(int a, int b) {
return a + b;
}
public static double plus(double a, double b) {
return a + b;
}
public static int getMax(int a, int b) {
int max = (a > b) ? a : b;
return max;
}
public static double getMax(double a, double b) {
double max = (a > b) ? a : b;
return max;
}
public static boolean equals(int a, int b) {
return a == b;
}
public static boolean equals(double a, double b) {
return a == b;
}
public static void printTheStar(int n, int m) {
for (int a = 1; a <= n; a++) {
for (int b = 1; b <= m; b++) {
System.out.print("*");
}
System.out.println();
}
}
public static void printTheMN(int n) {
for (int a = 1; a <= n; a++) {
for (int b = 1; b <= a; b++) {
System.out.print(a + "X" + b + "=" + (a * b) + "\t");
}
System.out.println();
}
}
}
8.把今天讲过的其他案例再练习一遍
|
|