import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数");
int a = sc.nextInt();
int sum = 0;
for (int x = 1;x <=a ;x++ ) {
sum += x;
}
System.out.println("sum = " + sum);
}
}
求偶数和
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数");
int a = sc.nextInt();
int sum = 0;
for (int x = 1;x <=a ;x++ ) {
if (x % 2 == 0) {
sum += x;
}
}
System.out.println("sum = " + sum);
}
}
求奇数和
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数");
int a = sc.nextInt();
int sum = 0;
for (int x = 1;x <=a ;x++ ) {
if (x % 2 == 1) {
sum += x;
}
}
System.out.println("sum = " + sum);
}
}
打印水仙花数
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int a,b,c,sum;
for (int x = 100;x <= 999 ;x++ ) {
a = x % 10;
b = x / 10 % 10;
c = x / 100;
sum = a*a*a + b*b*b + c*c*c;
if (x == sum) {
System.out.println(x);
}
}
}
}
统计水仙花数
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int a,b,c,sum,count = 0;
for (int x = 100;x <= 999 ;x++ ) {
a = x % 10;
b = x / 10 % 10;
c = x / 100;
sum = a*a*a + b*b*b + c*c*c;
if (x == sum) {
count++;
}
}
System.out.println("count = " + count);
}
}
九九乘法表
import java.util.Scanner;
class Test {
public static void main(String[] args) {
for (int x = 1;x <= 9 ;x++ ) {
for (int y = 1;y <= x ;y++ ) {
System.out.print(y + "*" + x + "=" + x*y + "\t");
}
System.out.println();
}
}
}
2:while循环的格式?要能看懂执行流程
用while循环完成如下案例
求和
纸张折叠成珠穆朗玛峰高度的次数
假设珠穆朗玛峰高度为8848米 一层纸代表0.01米
思路:
1,一张纸为1,对折一次为2,对折两次为4,对折三次为8*/
import java.util.Scanner;
class Test {
public static void main(String[] args) {
double i = 0.01;
int count = 0;
while (i <= 8848.0) {
i *= 2;
count++;
}
System.out.println("count = " + count);
}
}
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数");
int a = sc.nextInt();
System.out.println("请输入第二个整数");
int b = sc.nextInt();
int sum = he(a,b);
System.out.println("sum = " + sum);
}
public static int he(int a,int b) {
return a + b;
}
}
B:判断两个数据是否相等
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int a = 10;
int b = 11;
boolean c = isEquals(a,b);
System.out.println(c);
}
public static boolean isEquals(int a,int b) {
return (a == b);
}
}
C:获取两个数中较大的值
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
int max = getMax(a,b);
System.out.println(max);
}
public static int getMax(int a,int b) {
return (a > b) ? a : b;
}
}
D:打印m行n列的星形矩形
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入行数");
int row = sc.nextInt();
System.out.println("请输入列数");
int conlumn = sc.nextInt();
print(row,conlumn);
}
public static void print(int a,int b) {
for (int x = 1;x <= a ;x++ ) {
for (int y = 1;y <= b ;y++ ) {
System.out.print("*");
}
System.out.println();
}
}
}
E:打印nn乘法表
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数");
int x = sc.nextInt();
pro(x);
}
public static void pro(int a) {
for (int x = 1;x <= a ;x++ ) {
for (int y = 1;y <= x ;y++ ) {
System.out.print(y + "*" + x + "=" + x*y + "\t");
}
System.out.println();
}
}
}