猜数字,输入一个数字判断是大于、小于、还是等于;
import java.util.Random;
import java.util.Scanner;
public class Test22 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = 0, count = 0;
//生成随机数0~100;
Random r = new Random();
int m = r.nextInt(100);
while (true) {
System.out.println("请输入数字:");
k = sc.nextInt();
if (m < k) {
System.out.println("你猜的数大了");
} else if (m > k) {
System.out.println("你猜的数小了");
} else {
System.out.println("你猜对了!");
System.out.println("你猜了" + ++count + "次");
break;
}
count++;
}
}
}
模拟输入密码三次机会,输入正确提示”登陆成功“,错误提示重新输入,输入密码长度不够提示应该输入位数,并不占用次数;三次后退出程序;
import java.util.Scanner;
public class Test18 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "";
String p = "123456";
int i = 0;
Scanner sc = new Scanner(System.in);
while (i++ < 3) {
System.out.println("请输入六位密码");
s = sc.nextLine();
if (s.length() == 6) {
if (s.equals(p)) {
System.out.println("密码正确,登录成功");
break;
}else{
System.out.println("密码错误");
}
if (i == 3) {
System.out.println("三次出错程序结束");
return;
}
} else {
i--;
System.out.println("密码是六位");
}
}
}
}
打印乘法表,要求没有重复例如2*3=6跟3*2=6只需一个;
public class Test19 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + i * j + "t");
}
System.out.println();
}
}
}
判断给定的三个数,是否能组成三角形,然后在判断其是(等边,等腰,直角,普通)三角形。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 3, b = 3, c = 3;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一边长");
while (true) {
a = sc.nextInt();
if (a >= 1 && a < 100) {
break;
} else {
System.out.println("请输入数据不合法");
}
}
System.out.println("请输入第二边长");
while (true) {
b = sc.nextInt();
if (b >= 1 && b < 100) {
break;
} else {
System.out.println("输入数据不合法");
}
}
System.out.println("请输入第三边");
while (true) {
c = sc.nextInt();
if (c >= 1 && c < 100) {
break;
} else {
System.out.println("输入数据不合法");
}
}
boolean i = (a + b > c) && (a + c > b) && (b + c > a);
if (i) {
if (a == b && b == c) {
System.out.print("等边");
} else if (a == b || b == c || a == c) {
System.out.print("等腰");
} else if (a * a + b * b == c * c || a * a + c * c == b * b) {
System.out.print("直角");
} else {
System.out.print("普通");
}
System.out.println("三角形");
}
}
}
输入行号打印菱形
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入行号:");
int row = sc.nextInt();
for (int i = 0; i < row; i++) {
for (int a = 0; a < (row - i - 1); a++) {
System.out.print(" ");
}
for (int j = 0; j < (2 * (i + 1) - 1); j++) {
if (j > 0 && j < 2 * i) {
System.out.print(" ");
} else
System.out.print("*");
}
System.out.println();
s = i;
// System.out.println(i);
}
// System.out.println(s);
for (int i = s; i > 0; i--) {
for (int a = row - i - 1; a >= 0; a--) {
System.out.print(" ");
}
for (int k = 2 * i - 1; k > 0; k--) {
if (i != 1 && k < 2 * i - 1 && k > 1) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}
}
输入数字判断其为几位数,并反向打印其各个位数值;
import java.util.Scanner;
public class Test16 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
String c = "";
c += i;
System.out.println(c.length() + "位数");
for (int j = c.length() - 1; j >= 0; j--) {
char s = c.charAt(j);
System.out.print(s + "t");
}
}
}
打印1~1000间的完数(完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数。它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。如果一个数恰好等于它的真因子之和,则称该数为“完全数”。)并将其真因子相加的表达式打印。
public class Test11 {
public static void main(String[] args) {
int res = 0;
String c = "";
for (int j = 4; j < 1000; j++) {
for (int k = 1; k <= j / 2; k++) {
if (j % k == 0) {
res += k;
c += k + "+";
}
}
if (res == j) {
System.out.println(res);
System.out.println(c.substring(0, c.length() - 1) + "=" + res);
res = 0;
c = "";
} else {
res = 0;
c = "";
}
}
}
}



