Java day_03
程序员文章站
2024-02-10 09:20:10
...
import java.net.SocketOption;
import java.util.Scanner;
public class day_3 {
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
}
public static void test1(){
int[] array=new int[4];
array[0]=23;
array[1]=45;
array[2]=3;
array[3]=17;
System.out.println(array[0]);
System.out.println(array);
System.out.println(array[array.length-1]);
System.out.println("--------------");
for (int i=0;i<array.length;i++){
System.out.println(array[i]);
}
System.out.println("---------------");
for (int j=array.length-1;j>=0;j--){
System.out.println(array[j]);
}
}
public static void test2(){
int[] array=new int [4];
array[0]=23;
array[1]=45;
array[2]=3;
array[3]=17;
int sum=0;
for (int i=0;i<array.length;i++){
sum+=array[i];
}
System.out.println(sum);
}
public static void test3() {
int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
int[] countArray=new int[10];
for (int i=0;i< scores.length;i++){
switch(scores[i]){
case 0:
countArray[0]++;
break;
case 1:
countArray[1]++;
break;
case 2:
countArray[2]++;
break;
case 3:
countArray[3]++;
break;
case 4:
countArray[4]++;
break;
case 5:
countArray[5]++;
break;
case 6:
countArray[6]++;
break;
case 7:
countArray[7]++;
break;
case 8:
countArray[8]++;
break;
case 9:
countArray[9]++;
break;
}
}
for (int j=0;j<countArray.length;j++){
System.out.println(j+"出现的次数:"+countArray[j]);
}
}
public static void test4(){
int[] score = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
int oddCount=0;
int evenCount=0;
for (int i=0;i<score.length;i++){
if(score[i]%2==0){
evenCount++;
}else{
oddCount++;
}
}
System.out.println("odd:"+oddCount);
System.out.println("even:"+evenCount);
}
public static void test5(){
Scanner scanner=new Scanner(System.in);
System.out.println("Please Enter Student's num:");
int num=scanner.nextInt();
double[] array=new double[num];
int sum=0;
for (int i=0;i<array.length;i++){
System.out.println("Please Enter "+(i+1)+"Student's score:");
array[i]=scanner.nextDouble();
sum+=array[i];
}
System.out.println("average:"+sum/array.length);
}
public static void test6(){
Scanner scanner=new Scanner(System.in);
System.out.println("Please Enter a str:");
String str=scanner.next();
char[] array=str.toCharArray();
boolean ishuiwen=true;
for (int i=0;i<array.length;i++){
if(array[i]!=array[array.length-i-1]){
ishuiwen=false;
break;
}
}
if (ishuiwen){
System.out.println("is huiwen.");
}else{
System.out.println("not huiwen.");
}
}
public static void test7(){
Scanner scanner=new Scanner(System.in);
System.out.println("Please Enter a str:");
String str=scanner.nextLine();
char[] array=str.toCharArray();
int digitalCount=0;
int charCount=0;
int blankCount=0;
int otherCount=0;
for (int i=0;i<array.length;i++){
if (array[i]>='0'&&array[i]<='9'){
digitalCount++;
}else if (array[i]>='a'&&array[i]<='z'||array[i]>='A'&&array[i]<='Z'){
charCount++;
}else if (array[i]==' '){
blankCount++;
}else{
otherCount++;
}
}
System.out.println("digitaolcount:"+digitalCount);
System.out.println("charcount:"+charCount);
System.out.println("blankcount:"+blankCount);
System.out.println("othercount:"+otherCount);
}
public static void test8(){
Scanner scanner=new Scanner(System.in);
System.out.println("Please Enter x:");
int x=scanner.nextInt();
System.out.println("Please Enter y:");
int y=scanner.nextInt();
System.out.println("Please Enter z:");
int z=scanner.nextInt();
if (x>y){
int temp=x;
x=y;
y=temp;
}
if (x>z){
int temp=x;;
x=z;
z=temp;
}
if (y>z){
int temp=y;
y=z;
z=temp;
}
System.out.println("x:"+x+"\n"+"y:"+y+"\n"+"z:"+z);
}
}