Created
December 23, 2016 13:27
-
-
Save mokomokohitsuzi/c79a72cfc8fdf9e828322e2a69298e71 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習7-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class En07_01 { | |
public static void main(String[] args) { | |
Scanner stdIn = new Scanner(System.in); | |
System.out.println("入力した整数の値によって以下のように表示します。"); | |
System.out.println("負:-1 0:0 正:1"); | |
System.out.print("整数を入力:"); | |
int n = stdIn.nextInt(); | |
// メソッド実行 | |
int hantei = signOf(n); | |
// 表示 | |
System.out.println(hantei); | |
} | |
// メソッド | |
static int signOf(int n) { | |
int t = 0; | |
if (n == 0) { | |
t = 0; | |
} else if (n < 0) { | |
t = -1; | |
} else if (n > 0) { | |
t = 1; | |
} | |
return t; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment