簡単なif
ステートメントを使用します。
import java.util.Scanner;
public class CheckLuck {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);int strength,health,luck;
System.out.println("Enter strength (1-10): ");
strength = in.nextInt();
if(strength>10) /* chech for each value, should not be >10 */
strength=5;
System.out.println("Enter health (1-10): ");
health = in.nextInt();
if(health>10)
health=5;
System.out.println("Enter luck (1-10): ");
luck = in.nextInt();
if(luck>10)
luck=5;
if(strength+health+luck>15) /* If sum is >15 assign 5 to each value */
strength=health=luck=5;
}
}
更新:public voiddecide()の使用
(これがおかしいと思われる場合は申し訳ありません)
import java.util.Scanner;
public class TestDecide {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);int strength,health,luck;
System.out.println("Enter strength (1-10): ");
strength = in.nextInt();
if(strength>10)
strength=5;
System.out.println("Enter health (1-10): ");
health = in.nextInt();
if(health>10)
health=5;
System.out.println("Enter luck (1-10): ");
luck = in.nextInt();
if(luck>10)
luck=5;
if(strength+health+luck>15)
{
strength=health=luck=5;
decide();
}
else
{
System.out.println("Congrats!!!!!!!!!!!! for your scores");
}
}
public static void decide()
{
System.out.println("You have given your character too many points! Default values have been assigned: Character, strength: 5, health: 5, luck: 5");
}
}