Hello.java
import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = "yes"; int num; while(str.equals("yes")) { System.out.print("请输入学生编号:"); num = scanner.nextInt(); switch(num) { case 1: System.out.println("张三的成绩为90"); break; case 2: System.out.println("李四的成绩为80"); break; case 3: System.out.println("王五的成绩为70"); break; default: System.out.println("输入有误!"); break; } System.out.print("是否继续?yes/no"); str = scanner.next(); } System.out.println("查询结束"); } }
Test.java
import java.util.Scanner; public class Test { public static void main(String[] args) { // int num = 0; // do { // System.out.println("Hello World"); // num++; // }while(num < 0); String str; Scanner scanner = new Scanner(System.in); do { System.out.println("张三参加体能测试,跑1000米..."); System.out.print("是否合格?yes/no"); str = scanner.next(); }while(str.equals("no")); System.out.println("合格,通过测试"); } }
Test2.java
public class Test2 { public static void main(String[] args) { for(int i = 0;i < 10;i++) { System.out.println("Hello World"); } } }
Test3.java
public class Test3 { public static void main(String[] args) { //while循环 int num = 0; while(num <= 10) { if(num%2!=0) { System.out.print(num+","); } num++; } System.out.println(""); //do-while循环 num = 0; do { if(num%2!=0) { System.out.print(num+","); } num++; }while(num<=10); System.out.println(""); //for循环 for(int i = 0;i <= 10;i++) { if(i%2!=0) { System.out.print(i+","); } } } }
Test4.java
public class Test4 { public static void main(String[] args) { //打印1-6 // for(int i=1;i<=6;i++) { // System.out.print(i+" "); // } // System.out.println(""); // for(int i=1;i<=6;i++) { // System.out.print(i+" "); // } // System.out.println(""); // for(int i=1;i<=6;i++) { // System.out.print(i+" "); // for(int j=0;j<10;j++) { // for(int i=1;i<=6;i++) { // System.out.print(i+" "); // } // System.out.println(""); // } for(int i=0;i<4;i++) { for(int j=0;j<3-i;j++) { System.out.print(" "); } for(int k=i;k<2*i+1;k++) { System.out.print(k+" "); } System.out.println(""); } for(int i=0;i<3;i++) { for(int j=0;j<i+1;j++) { System.out.print(" "); } for(int k=2-i;k<5-2*i;k++) { System.out.print(k+" "); } System.out.println(""); } } }
Test5.java
public class Test5 { public static void main(String[] args) { // int sum = 0; // for(int i=1;i<=100;i++) { // sum += i; // if(sum>50) { // break; // } // } // System.out.println("1-100的总和是"+sum); int sum = 0; for(int i=1;i<=200;i++) { // if(i%2!=0) { // sum+=i; // } if(i%2==0) { continue; } sum+=i; } System.out.println("1-200的奇数之和是"+sum); } }
Test6.java
public class Test6 { public static void main(String[] args) { //声明数组:数据类型[] 数组名 int[] array1; String array2[]; //分配内存空间:创建数组必须指定长度,根据长度开辟一串连续的内存空间,长度不能修改 array1 = new int[6]; array2 = new String[7]; //给数组赋值 array1[0] = 50; array1[1] = 60; array2[1] = "Hello World"; //使用数组 System.out.println(array1[0]); } }
Test7.java
public class Test7 { public static void main(String[] args) { // int[] array; // array = new int[4]; int[] array = new int[4]; array[0] = 179; array[1] = 182; array[2] = 168; array[3] = 176; System.out.println(array[4]); // //计算平均值 // double sum = 0; // for(int i=0;i<4;i++) { // sum += array[i]; // } // System.out.println("平均身高是"+sum/4); // int[] array1 = {179,182,168,176}; // int[] array1 = new int[4]; // array1 = {179,182,168,176}; } }
原文地址:https://www.cnblogs.com/HiJackykun/p/11167095.html
时间: 2024-11-09 00:32:17