package box;
public class Cell {
int cow,rol;
public void left(){
cow--;
}
public void right(){
cow++;
}
public void down(){
rol++;
}
}
/*
1,首先需要一个类,建立一个对象来控制方块的移动,建立cell类。
2这个对象中需要拥有的功能,可以左移一格,右移一格,下移一格。需要定义变量行(row),列(col)。
3gamecell类放主方法,需要实现输出格子,采用两层for循环。
4还需要处理用户输入0(表示退出),1(表示下落),2(表示左移),3(表示右移)。解决方法,需要一个while结构,来判断输入是否为零,循环里面嵌套一个if,else
语句来判断输入的1,2,3.
5main 方法中需要有一个函数来输出表格printBox(),只需要两层for循环
*/
package box;
import java.util.Scanner;
public class Gamecell {
public static void main(String[] args){
int fristCow=(int)(Math.random()*20);
int fristRol=(int)(Math.random()*10);
Scanner sc=new Scanner(System.in);
Cell cell=new Cell();
cell.cow=fristCow;
cell.rol=fristRol;
System.out.println("cell的当前位置为:("+cell.cow+","+cell.rol+")");
printBox(cell);
System.out.println("1——下落,2——左移,3——右移,0——退出");
while(sc.hasNextInt())
{
int number=sc.nextInt();
if(number==0)
break;
if(number==1)
cell.down();
else if(number==2)
cell.left();
else if(number==3)
cell.right();
System.out.println("cell的当前位置为:("+cell.cow+","+cell.rol+")");
printBox(cell);
System.out.println("1——下落,2——左移,3——右移,0——退出");
}
}
public static void printBox(Cell cell){
int totalCow=20,totalRol=10;
for(int i=0;i<totalCow;i++){
for(int j=0;j<totalRol;j++){
if(i==cell.cow&&j==cell.rol)
{
System.out.print("*");
}
else
{
System.out.print("-");
}
}
System.out.println();
}
}
}
java基础俄罗斯方块
最新推荐文章于 2022-03-27 13:25:17 发布