实验一 命令行参数数组一
public class Test {
public static void main(String[] args) {
if(args.length==0)
System.out.println("没有命令行参数。");
else
{System.out.println("已获得命令行参数。");}
for (int i=0;i < args.length;i++)
System.out.println(args[i]);
}
}
实验二 水仙花数
public class DaffodilsNum {
static int number1;
static int number2;
static int number3;
//分别对应个位,十位,百位
public static void main(String[] args) {
shui();
}
private static void shui(){
for(int i = 100;i<1001;i++){
number1 = i%10;
number2 = i/10%10;
number3 = i/100; if(number1*number1*number1+number2*number2*number2+number3*number3*number3 == i){
System.out.println(i);
}
}
}
}
实验三 数值排序
import java.util.Random;
public class ListNum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
int random[]=new int[10];
Random r;
for(int i=0;i<10;i++){
r= new Random();
n =r.nextInt(99)+1;
System.out.print(n+" ");
random[i]=n;
}
System.out.print("\n");
for(int a=0;a<random.length;a++){
for(int b=a+1;b<random.length;b++){
if(random[a]>random[b]){
int tem= random[a];
random[a]=random[b];
random[b]=tem;
}
}
}
for(int q=0;q<random.length;q++){
System.out.print(random[q]+" ");
}
}
}
实验四 字符串算法
import java.util.Scanner;
public class AppearTime {
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int j=0;
for(int i=0;i<str.length()-1;i++){
String str1= str.substring(i, i+2);
if(str1.equals("sh")){
j++;
}
}
System.out.println("sh 在字符中一共出现了"+j+"次");
}
}