Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java で 数当てゲーム
import java.io.*;
public class Main {
static int small = 1;
static int big = 100;
private static int guessMyNumber() {
return (small + big) / 2;
}
private static int smaller() {
big = guessMyNumber() - 1;
return guessMyNumber();
}
private static int bigger() {
small = guessMyNumber() + 1;
return guessMyNumber();
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean isFinish = false;
String mes;
while (!isFinish) {
mes = in.readLine();
switch (mes) {
case "smaller":
System.out.println(smaller());
break;
case "bigger":
System.out.println(bigger());
break;
case "yes":
System.out.println("Yeah! Thank you playing!");
isFinish = true;
break;
default:
System.out.println(guessMyNumber());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.