控制语句
控制语句
1、打印九九乘法表
public class work01 {
public static void main(String[] args){
for (int i = 1;i < 10;i++){
for (int j = 1;j <= i;j++){
if((i == 3 && j == 3) || (i == 4 && j == 3)){
System.out.print(" ");
}
System.out.print(j + "x" + i + "=" + (j*i) + " ");
}
System.out.println();
}
}
}
代码实现:
2、打印三角形
public class work02 {
public static void main(String[] args){
for(int i = 1;i<= 4;i++){
for(int j = 1;j<=(4-i);j++){
System.out.print(" ");
}
for (int j = 1;j<=(i*2-1);j++){
System.out.print("*");
}
System.out.println();
}
}
}
代码实现:
3、打印菱形
public class work03 {
public static void main(String[] args) {
for(int i = 1;i<= 4;i++){
for (int j = 1;j<= 4-i;j++){
System.out.print(" ");
}
for (int j = 1;j<=(i*2-1);j++){
System.out.print("*");
}
System.out.println();
}
for(int i = 1;i<= 3;i++){
for (int j = 3;j>(3-i);j--){
System.out.print(" ");
}
for (int j = 5;j>=(i*2-1);j--){
System.out.print("*");
}
System.out.println();
}
}
}
代码实现:
4、打印空心菱形
public class work04 {
public static void main(String[] args) {
for(int i = 1;i<= 4;i++){
for (int j = 1;j<= 4-i;j++){
System.out.print(" ");
}
for (int j = 1;j<=(i*2-1);j++){
if(j==1|| j==(i*2-1) ) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
System.out.println();
}
for(int i = 1;i<= 3;i++){
for (int j = 3;j>(3-i);j--){
System.out.print(" ");
}
for (int j = 5;j>=(i*2-1);j--){
if(j==5 || j==(i*2-1)) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
代码实现:
5、已知账号admin,密码123456,使用键盘录入账号密码完成登录验证流程,允许输错三次,三次后提示账号锁定
方法一:
import java.util.Scanner;
public class work05 {
public static void main(String[] args) {
final String STR = "admin";
final int RESULT = 123456;
Scanner scanner = new Scanner(System.in);
String str;
int result;
int num = 2;
for(;;){
System.out.print("请输入账号:");
str = scanner.next();
System.out.print("请输入密码:");
result = scanner.nextInt();
if(str.equals(STR) && RESULT == result){
System.out.println("登录成功,欢迎回来!!!");
break ;
}else{
if (num == 0){
System.out.println("因错误次数过多,您的账户已经被锁定!");
break;
}
System.out.println("您的输入有误,请重新输入:你还有" + num + "次机会!!!");
num--;
}
}
}
}
方法二:
import java.util.Scanner;
public class work05 {
public static void main(String[] args) {
int RESULT = 123456;
String STR = "admin";
Scanner scanner = new Scanner(System.in);
System.out.print("请输入账号:");
String str = scanner.next();
System.out.print("请输入密码:");
int result = scanner.nextInt();
for (int i = 2;i> 0;i--){
if((!STR.equals(str)) || (!(RESULT == result))){
System.out.println("您输入的信息有误,请重新输入,你还有" + i + "次机会" );
}else {
System.out.println("登录成功,欢迎回来!!!");
break;
}
System.out.print("请输入账号:");
str = scanner.next();
System.out.print("请输入密码:");
result = scanner.nextInt();
}
}
}
代码实现:
6、随机生成一个1~100整数,然后输入一个整数和生成的数字进行比对,猜大小并给予提示,猜对为止。
import java.util.Random;
import java.util.Scanner;
public class work06 {
public static void main(String[] args) {
Random random = new Random();
int result = (random.nextInt(99) + 1);
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个 0 -- 100 的数字:");
int sum = scanner.nextInt();
for(;;){
if(sum > result){
System.out.println("猜错了,不妨往小一点试试");
System.out.print("请重新输入一个 0 -- 100 的数字:");
sum = scanner.nextInt();
}else if(sum < result){
System.out.println("猜错了,不妨往大一点试试");
System.out.print("请重新输入一个 0 -- 100 的数字:");
sum = scanner.nextInt();
}else if(sum == result){
System.out.println("恭喜你,猜对了");
break;
}
}
System.out.println("您的幸运数字为: " + result);
}
}
代码实现:
7、由键盘输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321
import java.util.Scanner;
public class work07 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个四位数的整数:");
int num = scanner.nextInt();
int temp = 0;
System.out.print("反转:");
for (;;){
if(num == 0){
break;
}
temp = num%10;
System.out.print(temp);
num /= 10;
}
}
}
代码实现:
8、int a = 2; int b = 5;如何不使用第三个变量交换两个数?(不可以使用按位运算)
public class work08 {
public static void main(String[] args) {
int a = 2;
int b = 5;
// a *=b;
// b = a/b;
// a /=b;
a = a+b;
b = a-b;
a = a-b;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
代码实现:
9、输入一个整数,计算该数有多少位,例如123有3位
import java.util.Scanner;
public class work09 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个正整数:");
int result = scanner.nextInt();
int temp = 1;
for (;;){
if(result/10 == 0 ){
System.out.println("您输入的是一个" + temp + "位数!");
break;
}
result /= 10;
temp++;
}
}
}
代码实现:
|