标签:一个 ase 无限循环 nbsp ade code har rgs image
1、顺序结构
2、if条件语句
判断输入的字符是否为大写字母(if......else........):
package 选择结构; import java.util.Scanner; public class IfDemo { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s = sc.next();//返回 指定索引处的值。 char c = s.charAt(0); if(c>=65&&c<=90) System.out.println("大写字母"); else System.out.println("其他"); } }
3、switch选择语句
import java.util.*; public class SwitchDemo { public static void main(String [] args) { int num1,num2,num; String c; Scanner reader=new Scanner(System.in); System.out.println("输入正整数·"); num1=reader.nextInt(); System.out.println("输入运算符"); c=reader.next(); System.out.println("输入正整数"); num2=reader.nextInt(); switch(c) { case"+":System.out.println("num1+num2="+(num1+num2));break; case"-":System.out.println("num1-num2="+(num1+num2));break; case"/":System.out.println("num1/num2="+(num1/num2));break; case"*":System.out.println("num1*num2="+(num1*num2));break; } } }
4、while语句
package WhileDemo; import java.util.Scanner; public class WhileDemo { public static void main(String[] args) { int i=2,n; float fac=1; System.out.println("请输入一个正整数"); Scanner sc=new Scanner(System.in); n=sc.nextInt(); if(n==0||n==1){ System.out.println(1); } while(i<=n){ fac=fac*i; i++; } System.out.println(fac); } }
5、do......while()语句
package DOWhileDemo; import java.util.Scanner; public class DoWhileDemo { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int x=1,add=0; do { add=add+x; x++; // 将x的值自增 } while (x <= n); // 循环条件 System.out.println(add); } }
6、for循环语句
package 九九乘法表; public class MultiplicationTable { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { System.out.println("\n"); for (int j = 1; j <= i; j++) { System.out.print(i + "*" + j + "=" + i * j + " "); } } } }
7、无限循环(死循环)
while(true){}
和
for(;;){}
标签:一个 ase 无限循环 nbsp ade code har rgs image
原文地址:https://www.cnblogs.com/zhai1997/p/11258254.html