자바 텍스트 낚시 프로그램입니다.
글쓴이: lfs / 작성시간: 목, 2018/12/20 - 9:08오전
import java.lang.*;
import java.util.*;
import java.io.*;
import java.time.*;
public class fish{
public static void main(String args[]) throws Exception{
Angle angle=new Angle();
boolean mi;
Console console=System.console();
while(true){
angle.init();
angle.drop();
mi=angle.middle();
if(mi)
angle.up();
System.out.print("계속하시겠습니까?(y/n)>>");
String No=console.readLine();
if(No.equals("y")){
System.out.println("계속하겠습니다.");
continue;
}
else if(No.equals("n")){
System.out.println("그만두겠습니다.");
break;
}
}
angle.result();
}
}
class Angle{
Hook[] hook=Hook.values();
Rod[] rod=Rod.values();
int preHook;
int preRod;
//물고기 수확하는 갯수변수
int crucianfish;
int catfish;
int zzie=0; //낚시찌 길이
//낚시 초기화
public void init() throws Exception{
Scanner scanner=new Scanner(System.in);
while(true){
System.out.println(" ++낚시 채비++");
System.out.println("낚시바늘:0=1호 1=2호");
System.out.print("입력:");
this.preHook=scanner.nextInt();
if(this.preHook<0 || this.preHook>1){
System.out.println("잘못입력하셨습니다.");
continue;
}
System.out.println(" ++낚시 채비++");
System.out.println("낚시대:0=나무 1=쇠");
System.out.print("입력:");
this.preRod=scanner.nextInt();
if(this.preRod<0 || this.preRod>1){
System.out.println("잘못입력하셨습니다.");
continue;
}
break;
}
if(hook[this.preHook]==Hook.ONE){
System.out.println("낚시바늘 1호선택");
}
else if(hook[this.preHook]==Hook.TWO){
System.out.println("낚시바늘 2호선택");
}
if(rod[this.preRod]==Rod.TREE){
System.out.println("낚시대 나무선택");
}
else if(rod[this.preRod]==Rod.STAIN){
System.out.println("낚시대 쇠선택");
}
System.out.println("낚시준비가 되었습니다.");
}
//낚시를 드리울때
public void drop() throws Exception{
Scanner scanner=new Scanner(System.in);
System.out.print("낚시찌 길이 입력(1~10미터):>>");
this.zzie=scanner.nextInt(); //zzie 필드를 씁니다.
System.out.printf("낚시찌 길이가 %d--미터입니다.\n",this.zzie);
System.out.println("낚시를 물에 드리웁니다.");
System.out.println("낚시바늘이 물속에 있습니다.");
}
//낚시중일때
public boolean middle() throws Exception{
LocalDateTime now=LocalDateTime.now();
Random random=new Random((long)now.getSecond());
int watime=random.nextInt(10)+1;
boolean rope;
if(hook[this.preHook]==Hook.ONE){
if(rod[this.preRod]==Rod.TREE){
rope=random.nextBoolean();
if(rope){
System.out.println("낚시가 잘되가고 있습니다.");
Thread.sleep(watime*1000);
return true;
}
else{
System.out.println("낚시가 잘되가고 있습니다.");
Thread.sleep(watime*1000);
System.out.println("낚시줄이 끊어졌습니다.");
return false;
}
}
else if(rod[this.preRod]==Rod.STAIN){
rope=random.nextBoolean();
if(rope){
System.out.println("낚시가 잘되가고 있습니다.");
Thread.sleep(watime*1000);
return true;
}
else{
System.out.println("낚시가 잘되가고 있습니다.");
Thread.sleep(watime*1000);
System.out.println("낚시줄이 끊어졌습니다.");
return false;
}
}
}
else if(hook[this.preHook]==Hook.TWO){
if(rod[this.preRod]==Rod.TREE){
rope=random.nextBoolean();
if(rope){
System.out.println("낚시가 잘되가고 있습니다.");
Thread.sleep(watime*1000);
return true;
}
else{
System.out.println("낚시가 잘되가고 있습니다.");
Thread.sleep(watime*1000);
System.out.println("낚시줄이 끊어졌습니다.");
return false;
}
}
else if(rod[this.preRod]==Rod.STAIN){
rope=random.nextBoolean();
if(rope){
System.out.println("낚시가 잘되가고 있습니다.");
Thread.sleep(watime*1000);
return true;
}
else{
System.out.println("낚시가 잘되가고 있습니다.");
Thread.sleep(watime*1000);
System.out.println("낚시줄이 끊어졌습니다.");
return false;
}
}
}
return true;
}
//물고기가 밖으로 나올때
public void up() throws Exception{
Scanner scanner=new Scanner(System.in);
boolean run=true;
LocalDateTime now=LocalDateTime.now();
Random random=new Random((long)now.getSecond());
System.out.println("물고기가 물렸습니다.");
int zzimet=this.zzie;
int loop=random.nextInt(5); //낚시 잡아당기는 횟수
while(run){
System.out.printf("물고기가 %d--미터에 있습니다.\n",zzimet);
System.out.print("낚시찌 길이 입력(잡아당김)>>");
int capmet=scanner.nextInt();
zzimet-=capmet;
if(zzimet<0){
System.out.println("잘못입력하셨습니다.");
System.out.println("다시입력하십시오.");
zzimet=random.nextInt(9)+1;
continue;
}
loop--;
if(loop==0 && zzimet==0){
run=false;
System.out.println("물고기가 뜰채에 담을때가 되었습니다.");
boolean fishki=random.nextBoolean();
if(fishki){
(this.crucianfish)++;
System.out.println("붕어가 걸렸습니다.");
}
else{
(this.catfish)++;
System.out.println("메기가 걸렸습니다.");
}
}
else if(zzimet==0){
run=true;
zzimet=random.nextInt(9)+1;
}
else if(loop<0){
loop=random.nextInt(2)+1;
}
}
}
//낚시 결과를 나타낼때
public void result() throws Exception{
System.out.println("오늘 낚시결과입니다.");
System.out.println("====================");
System.out.printf("붕어----------%2d마리\n",this.crucianfish);
System.out.printf("메기----------%2d마리\n",this.catfish);
System.out.println("====================");
}
}
//낚시바늘
enum Hook{
ONE, //바늘 1호
TWO //바늘 2호
}
//낚시대
enum Rod{
TREE, //나무
STAIN //쇠
}저의 cpu가 i3-6100인데 낚시를 잡아당길때 무한 루프가 됩니다.
cpu버그일수도 있으나 다른 컴퓨터에서는 실험을 안했으므로
참고하시길 바랍니다.
Forums:


댓글 달기