본문 바로가기

JAVA를 잡아라!/Java

자동구문 생성..글자 무작위로 뽑아낸다..

public class PhraseOMatic {
 public static void main (String[] args){
  String[] wordListOne = {"24/7","multi-Tier","30,000 foot","B-to-B","win-win","front-end","web-based","pervasive","smart","six-sigma","critical-path","dynamic"};

  String[] wordListTwo = {"empowered", "sticky","valued-added","oriented", "centric","distributed","clustered","branded","outside-the-box","positioned","networked","focused","leveraged","aligned","targeted","shared","cooperative","accelerated"};

  String[] wordListThree ={"empowered", "sticky","valued-added","oriented"};
 // 각 단어 목록에 단어가 몇 개씩 들어가는지 확인하는 단계
 int oneLength = wordListOne.length; int oneLength = wordListOne.length; //길이를 확인한다는 말은.. 워드리스트원에 몇개인지를 다시 원랜스라는 변수값에 대입한다는 의미가 된다.!!
 int twoLength = wordListTwo.length;
 int threeLenght = wordListThree.length;

 // 난수(무작위숫자)를 발생시켜.. 무작위(랜덤)으로 단어를 출력시킨다..
 int rand1 = (int) (Math.random() * oneLength);
 int rand2 = (int) (Math.random() * oneLength);
 int rand3 = (int) (Math.random() * oneLength);

 //원,투,쓰리않에 문자값들을 랜덤으로 차례대로 불러와 출력시킨다~
 String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
 System.out.println("What we need is a " + phrase);
 }
}

첨부터 이해하려고 하니..너무나 힘들었다..
그래서무조건 예제를 쳐보기로 했다...

그리고 하나씩 치면서..주석을 달다보니...
아- 이해가 안될수가 없다!
저 글을 읽어보고 모르겠다면
무조건 쳐보길 바란다.. 실행을 해보고 난뒤 해석을 해보면 더 이해가 쉽다.

그리고 run을 여러번 수행할경우
출력부분에 무작위단어가 계속해서 바뀔것이다.
근데 계속 런을 하니.. 오류가 생기더랍...

정리 설명~~~
String[] food = {"apple","banana","orange"}; //스트링 배열 3가지를 만들었다.

int x = food.length; // 배열된값을 정수변수 x에 값을 넣는다..과일종류3가지가 들어갔으니..x값은 3이된다.

String s= food[0]; // s를 선언하는데 그 값은 apple이 될것이다. 말그대로 객체로 만들어진다는 뜻..

s = s+ " " +"는 맛있을껄요?"; //S = apple 이 된다!