Gyakorlati alapok
Chilipaprika zabáltatása algoritmikusan
Oldal Milán megoldása
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
System.out.println("Hello, it is the game called Candy-Pepper");
System.out.println("You MUST grab out 1, 2 or 3 candy(es).");
System.out.println("Let's play... good luck");
int numbersOfTheCandies = 17 ;
int myTurn;
int input = 0;
Scanner candy = new Scanner(System.in);
for(int i = 0; i < 4; i++){
System.out.println("How many candies
do you want to grab out? ");
input = candy.nextInt();
if((input != 1) && (input != 2) &&
(input != 3)){
do{
System.out.println("Wrong input... Please retype your number.");
input =
candy.nextInt();
}while((input != 1) && (input != 2)
&& (input != 3));
}
System.out.println("You grabbed out : "+input+" candy(es)");
numbersOfTheCandies = numbersOfTheCandies - input;
System.out.println("You have : " + numbersOfTheCandies);
myTurn = 4 - input;
System.out.println("I will grab " + myTurn + " candy(es)");
numbersOfTheCandies = numbersOfTheCandies - myTurn;
}
System.out.println("SORRY I WON!");
}
}
Végeredmény:
Hello, it is the game called Candy-Pepper
You MUST grab out 1, 2 or 3 candy(es).
Let's play... good luck
How many candies do you want to grab out?
0
Wrong input... Please retype your number.
1
You grabbed out : 1 candy(es)
You have : 16
I will grab 3 candy(es)
How many candies do you want to grab out?
2
You grabbed out : 2 candy(es)
You have : 11
I will grab 2 candy(es)
How many candies do you want to grab out?
4
Wrong input... Please retype your number.
5
Wrong input... Please retype your number.
76
Wrong input... Please retype your number.
3
You grabbed out : 3 candy(es)
You have : 6
I will grab 1 candy(es)
How many candies do you want to grab out?
0
Wrong input... Please retype your number.
2
You grabbed out : 2 candy(es)
You have : 3
I will grab 2 candy(es)
SORRY I WON!