Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 23, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokomokohitsuzi/c79a72cfc8fdf9e828322e2a69298e71 to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/c79a72cfc8fdf9e828322e2a69298e71 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習7-1
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