"Java Programming" Experimental Guidance-Project 2 Java Language Foundation

Project 2 Java language foundation

Experiment purpose: Familiar with the basic grammar of Java, basic data types, the use of various operators and expressions, master the priority of operators, and be familiar with the use of Java selection statements and loop statements.
Experimental nature: confirmatory experiment + design experiment.
Experimental content:
(1) Analyze and debug the examples in Chapter 2. (slightly)

(2) Use the if statement and switch statement to write the program to realize the function: according to the student's grades (may be 5, 4, 3, 2, 1), display the corresponding level (excellent, good, medium, pass, no Pass)

import java.util.Scanner;
public class Grade {
    
    

	public static void main(String []args) {
    
    

//if方法		
		System.out.print("请输入学生成绩:");
		Scanner input = new Scanner(System.in);
		int grade = input.nextInt();
		if(grade==5) System.out.println("优秀");
		if(grade==4) System.out.println("良好");
		if(grade==3) System.out.println("中等");
		if(grade==2) System.out.println("及格");
		if(grade==1) System.out.println("不及格");
		

//switch方法	
		System.out.print("请输入学生成绩:");
		int grade2 = input.nextInt();
		switch(grade2) {
    
    
		case 5:System.out.println("优秀");break;
		case 4:System.out.println("良好");break;
		case 3:System.out.println("中等");break;
		case 2:System.out.println("及格");break;
		case 1:System.out.println("不及格");break;
		}
		input.close();
	}
}

(3) Output data within 500 that can be divisible by 5 and 7 but not divisible by 3, and calculate their sum.

public class Divide {
    
    

	public static void main(String[] args) {
    
    
		int num = 0;
		for(int i=0;i<501;i++) {
    
    
			if(i%5==0&&i%7==0&&i%3!=0) {
    
    
				System.out.print(i+" ");
				num+=i;
			}
		}
		System.out.println("和为"+num);
	}
}

(4) Use the loop statement to output the following graphics:
Insert picture description here

public class Picture {
    
    

	public static void main(String []args) {
    
    
		for(int i = 0;i<5;i++) {
    
    
			for(int j=0;j<5;j++) {
    
    
				System.out.print("*");
				if(j==4) System.out.println();
				}
			for(int s=0;s<i+1;s++) {
    
    
				System.out.print(" ");
			}
		}
	}
}

(5) Man-machine boxing guessing: Write a small game of rock, paper, scissors and man-machine. After a round of boxing guessing, win or lose information will be displayed.
Prompt: Program flow:
[1] Generate a random number (range 1-3)
[2] The user enters an integer (range 1-3) (with good information prompt)
[3] According to the rules of rock, scissors, and cloth , Judge the relationship between user input data and random numbers, and give win or lose information.
Random number generation instructions
Method 1: Use Math.random() method
1. You need to import the Math class at the beginning of the program, as follows
import java.lang .;
It is not necessary to import it, because the java.lang package system uses
2. Generate [m, The expression of the integer (m<= x <=n) between n]:
int x= (int)(Math.random()
(n-m+1)+m) Method 2: Use Random class method
1 The Random class needs to be imported at the beginning of the program, as follows
import java.util.*;
2. A statement that generates an integer (m<= x <=n) between [m,n]:
Random obj=new Random();
int x=obj.nextInt(n-m+1)+m;

Input:
Add at the beginning of the file: import java.util.*;
Scanner read=new Scanner(System.in);
int x=read.nextInt();//Read an integer

import java.util.Scanner;
public class FingerGuessing {
    
    
	public static void main(String []args) {
    
    
		
		Scanner in = new Scanner(System.in);
		System.out.println("请输入您要出的手势  1.剪刀  2.石头  3.布");
		int x = in.nextInt();
		if(x==1) 
			System.out.println("我出剪刀");
		else if(x==2) 
			System.out.println("我出石头");
		else
			System.out.println("我出布");
		
		int  y= (int)(Math.random()*3+1);
		if(y==1) 
			System.out.println("对方出剪刀");
		else if(y==2) 
			System.out.println("对方石头");
		else
			System.out.println("对方布");
		
		if(x==y) 
			System.out.println("平局");
		else if((x==1&&y==2)||(x==2&&y==3)||(x==3&&y==1))
			System.out.println("很遗憾,您输了");
		else
			System.out.println("恭喜您,获得胜利");
		in.close();
		
	}
}

(6) Guess the number game: the computer randomly generates a number x; the user enters a data, compares it with the random number, and gives a prompt message:
if the input data> random number, the user is prompted that the data is too large, and the user is required to re-enter the data ;
If the input data is less than a random number, the user is prompted that the data is too small and the user is required to re-enter the data;
repeat this process until the user input data is the same as the random number.

import java.util.Random;
import java.util.Scanner;

public class GuessNumber {
    
    

	public static void main(String []args) {
    
    
		Random obj=new Random();
		int x = obj.nextInt(100)+1;
		Scanner price=new Scanner(System.in);
		
		do{
    
    
		System.out.println("猜测一下某某的价格(0~100元)");
		int y = price.nextInt();
	
		if(x>y) {
    
    
			System.out.println("没这么便宜!");
			System.out.print("再");}
		else if(x<y) {
    
    
			System.out.println("有这么贵吗?");
			System.out.print("再");}
		else
			{
    
    System.out.println("猜对了");price.close();break;}
		}while(true);
	}
}

(7) Enter the year, month, and day to show that the day is the first day of the year. It is required to check the reasonableness of the entered date.

import java.util.Scanner;
public class Date {
    
    
	
	public static void main(String []args) {
    
    

	int year;
	int month;
	int day;
	int days;
	int monthday;
	
	Scanner in = new Scanner(System.in);
	System.out.print("请输入年份");
	year=in.nextInt();

	while(true) {
    
    
	System.out.print("请输入月份(1~12)");
	month=in.nextInt();
	if(month>0&&month<13) 
		break;
	else
		System.out.print("输入错误,");
	}
	
	while(true) {
    
    
	System.out.print("请输入日");
	day=in.nextInt();
	if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)  //31天的月份
		if(day<32&&day>0)
			{
    
    monthday=31;break;}
		else
			System.out.print("输入错误,");
	else if(month==2&&year%4==0&&year%100!=0)                                    //普通闰年
		if(day<30&&day>0)
			{
    
    monthday=29;break;}
		else
			System.out.print("输入错误,");
	else if(month==2&&year%400==0&&year%100==0)                                   //特殊闰年
		if(day<30&&day>0)
			{
    
    monthday=29;break;}
		else
			System.out.print("输入错误,");
	else if((month==2&&year%4!=0)||(year%100==0&&year%400!=0))                     //非闰年
		if(day<29&&day>0)
		   {
    
    monthday=28;break;}
		else
			System.out.print("输入错误,");
	else
		if(day<31&&day>0)                                                          //30天的月份
			{
    
    monthday=30;break;}
		else
			System.out.print("输入错误,");
	}
	days=0;
	switch(month) {
    
    
	case 12:days+=31;
	case 11:days+=30;
	case 10:days+=31;
	case  9:days+=30;
	case  8:days+=31;
	case  7:days+=31;
	case  6:days+=30;
	case  5:days+=31;
	case  4:days+=30;
	case  3:days+=31;
	case  2:{
    
    
		if(year%4==0) days+=29;
		else days+=28;
	}
	case  1:days+=31;
	}
	days=days-(monthday-day);
	System.out.println(year+"年"+month+"月"+day+"日,是今年第"+days+"天");
	in.close();
	}
}

Guess you like

Origin blog.csdn.net/weixin_44652589/article/details/114269474