LoginSignup
3
3

More than 5 years have passed since last update.

JavaでFizzBuzzする環境を整える。

Last updated at Posted at 2014-03-07
  • javaは既に入ってた。
  • maven入れる。
brew install maven
  • プロジェクト作る
mvn archetype:generate

archetypeはプロジェクトの形式。
今回はとりあえず動かせればいいので maven-archetype-quickstart

groupIdはプロジェクトの識別子。公開しないならなんでもいい。
artifactIdはプロジェクトの名前。ディレクトリ名になる。

こんなかんじのディレクトリ構成になった。
[masato@mba] $ tree
.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── test
    │           └── App.java
    └── test
        └── java
            └── test
                └── AppTest.java

7 directories, 3 files
  • FizzBuzzを書く
src/main/java/test/FizzBuzz.java
package test;

public class FizzBuzz {
    public static void main(String[] args) {
        String val = null;
        for(int i=1; i<100; i++) {
            if (i%15 == 0) {
                val = "FizzBuzz";
            } else if (i%3 == 0) {
                val = "Fizz";
            } else if (i%5 == 0) {
                val = "Buzz";
            } else {
                val = String.valueOf(i);
            }

            System.out.print(val);
            System.out.print(" ");

        }
    }
}
  • コンパイルして実行
 mvn compile
 java -classpath target/classes test.FizzBuzz
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3