一.例题
1.打印三角形
效果:

代码:
public class triangle {
public static void main(String[] args){
//public private
for (int i = 0; i < 5; i++){
for (int j = 0; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
结果:
*
**
***
****
*****
2.打印三角形pro
效果:

代码1:在第一个的基础上另外循环一次,补出来下面的部分
public class trianglepro {
public static void main(String[] args){
for (int i = 0; i < 5; i++){
for (int j = 0; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
for (int i = 4; i > 0; i--){
for (int j = 0; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
代码2:使用绝对值(有点难)
public class trianglepro {
public static void main(String[] args){
for (int i = 0; i < 5*2-1; i++){
//Math.abs() 绝对值
for (int j = 0; j < 5 - Math.abs(4-i); j++){
System.out.print("*");
}
System.out.println();
}
}
}
结果:
*
**
***
****
*****
****
***
**
*
3.打印三角形promax
效果:

代码:
public class trianglepromax {
public static void main(String[] args){
for (int i = 0; i < 5*2-1; i++) {
for (int j = 0; j < Math.abs(4-i); j++) {
System.out.print(" ");
}
for (int j = 0; j < (5-Math.abs(4-i))*2 - 1; j++) {
System.out.print(i%2);
}
System.out.println();
}
}
}
结果:
0
111
00000
1111111
000000000
1111111
00000
111
0
4.打印三角形promax远峰蓝(很离谱)
效果

代码:
import java.awt.*;
import javax.swing.JLabel ;
import javax.swing.JFrame ;
public class TriangleProMaxSierraBlue {
public static void main(String args[]){
JFrame frame = new JFrame("TriangleProMaxSierraBlue") ;
frame.setLayout(new FlowLayout());
String str="";
for(int i=0;i<5*2-1;i++){
for(int j=0;j<Math.abs(4-i);j++){
str+=" ";
}
for(int j=0;j<(5-Math.abs(4-i))*2-1;j++){
str+=i%2;
}
//这里补齐了后面的空格
for(int j=0;j<Math.abs(4-i);j++){
str+=" ";
}
JLabel lab = new JLabel(str,JLabel.CENTER) ; // 实例化标签对象
Font font = new Font("宋体", Font.PLAIN, 25);
lab.setFont(font);
lab.setForeground(new Color(173,198,220));;
frame.add(lab) ; // 将组件件入到面板之中
str="";
}
Dimension dim = new Dimension() ;
dim.setSize(200,350) ; //窗体的大小
frame.setSize(dim) ; //设置窗体大小
Point point = new Point(500,100) ; // 设置坐标
frame.setLocation(point) ; //设置窗体坐标
frame.setVisible(true) ; //设置窗体可见
}
}
结果:就是上面的图片,不再重复
5.打印三角形(输入版)
代码:
import java.util.Scanner;
public class triangle_input {
public static void main(String[] args){
//这里是输入
Scanner input = new Scanner(System.in);
int num = input.nextInt();
for (int i = 0; i < num; i++){
for (int j = 0; j < i + 1; j++){
System.out.print("*");
}
System.out.println();
}
}
}
结果:
4
*
**
***
****
2.面试题(看不懂,一脸懵逼)
题目:
/*
有一个方阵,方阵中有男生0和女生1。
一个女生上、下、左、右的女生与她本人同属于一朵"花"。
方阵中有多少"花"?
*/
public class flowerNumber {
public static void main(String[] args){
int[][] studentCube={{0,0,1,1,1},
{1,0,1,0,0},
{1,1,1,0,1},
{0,0,0,0,0},
{1,1,0,1,1}};
System.out.print("Number of flowers is "+solution(studentCube));
}
public static int solution(int[][] studentCube){
int flowerNum=0;
//输入内容
return flowerNum;
}
}
代码:(用到了迭代,看不懂啊)
public class flowerNumber {
public static void main(String[] args){
int[][] studentCube={{0,0,1,1,1},
{1,0,1,0,0},
{1,1,1,0,1},
{0,0,0,0,0},
{1,1,0,1,1}};
System.out.print("Number of flowers is "+solution(studentCube));
}
public static int solution(int[][] studentCube){
int flowerNum=0;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(studentCube[i][j]==0)
continue;
flowerNum++;
tagFemaleToMale(studentCube,i,j);
}
}
return flowerNum;
}
public static void tagFemaleToMale(int[][] studentCube,int i,int j){
studentCube[i][j]=0;
if(i<4){
if(studentCube[i+1][j]==1)
tagFemaleToMale(studentCube,i+1,j);
}
if(j<4){
if(studentCube[i][j+1]==1)
tagFemaleToMale(studentCube,i,j+1);
}
if(i>0){
if(studentCube[i-1][j]==1)
tagFemaleToMale(studentCube,i-1,j);
}
if(j>0){
if(studentCube[i][j-1]==1)
tagFemaleToMale(studentCube,i,j-1);
}
}
}
结果:
Number of flowers is 4
三.总结
- 要加快进度看视频啊,要来不及了
- 很多绝对值那里需要再看看
- 递归和迭代搞不懂,有机会就学
评论 (0)