Sunteți pe pagina 1din 3

Stages:

import java.util.Scanner; /** * Josh Trout * JAVA P. 5 * * This program will display the age group name when you type in an age. */ public class stages { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final int Adult = 18; final int Toddler = 5; final int Child = 10; final int PreTeen = 12; int ent; Scanner input = new Scanner(System.in); System.out.print("Enter an age: "); ent = input.nextInt(); if (ent > Adult) { System.out.println("Adult"); } else { if (ent <= Toddler) { System.out.println("Toddler"); } else if (ent <= Child) { System.out.println("Child"); } else if (ent <= PreTeen) { System.out.println("PreTeen"); } else { System.out.println("Teen"); } } } }

Console:
Enter an age: 4 Toddler

Rock Paper Scissors:


import java.util.Random; import java.util.Scanner;

/** * Josh Trout * JAVA P. 5 * * This program plays rock paper scissors with a player. It asks for a throw (1. Rock 2. Paper 3. Scissors) then it generated * a random throw for itself. Finally, it will calculate who won and will display the results. */

public class rps_2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final int ROCK = 1, PAPER = 2, SCISSORS = 3; int playerThrow, computerThrow; Scanner input = new Scanner(System.in); Random r = new Random(); /* prompt player for throw and read typed number */ System.out.print("Enter your throw (1=Rock, 2=Paper, 3=Scissors) "); playerThrow = input.nextInt(); input.close(); /* Generate Computer Throw */ computerThrow = r.nextInt(3) + 1; /* Inform player of throws */ System.out.print("Player throws: "); switch(playerThrow) { case ROCK: System.out.println("ROCK"); break; case PAPER: System.out.println("PAPER"); break; case SCISSORS: System.out.println("SCISSORS"); break; } System.out.print("Computer throws: "); switch(computerThrow) { case ROCK: System.out.println("ROCK"); break; case PAPER: System.out.println("PAPER"); break; case SCISSORS: System.out.println("SCISSORS"); break; } /* Determine Winner */ switch(playerThrow) { case ROCK: if (computerThrow == ROCK) { System.out.println("It's a draw!"); } else if (computerThrow == PAPER) { System.out.println("Computer Wins!"); } else if (computerThrow == SCISSORS) {

System.out.println("Player Wins!"); } break; case PAPER: if (computerThrow == ROCK) { System.out.println("Player Wins!"); } else if (computerThrow == PAPER) { System.out.println("It's a draw!"); } else if (computerThrow == SCISSORS) { System.out.println("Computer Wins!"); } break; case SCISSORS: if (computerThrow == ROCK) { System.out.println("Computer Wins!"); } else if (computerThrow == PAPER) { System.out.println("Player Wins!"); } else if (computerThrow == SCISSORS) { System.out.println("It's a draw!"); } break; } } }

Console:
Enter your throw (1=Rock, 2=Paper, 3=Scissors) 3 Player throws: SCISSORS Computer throws: PAPER Player Wins!

S-ar putea să vă placă și