Você está na página 1de 4

Account Setup:

import java.util.Scanner; /** * This program asks you for a user name. Next, it will ask you for a password. It will keep on asking you for a password if the * entered password is not * 8 characters or longer. It then displays your user name and password. */ public class accountSetup { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String user, pass; Scanner input = new Scanner(System.in); System.out.print("Enter a user name: "); user = input.nextLine(); do { System.out.print("Enter a password that is at least 8 characters: "); pass = input.nextLine(); } while (pass.length() < 8); System.out.println("Your user name is " + user + " and your password is " + pass); } }

Console:
Enter a user name: JoshuaTrout Enter a password that is at least 8 characters: shsRobotics Your user name is JoshuaTrout and your password is shsRobotics

Word Guess:
import java.util.Scanner; /** * Josh Trout * JAVA P. 5 * * This program is a word guess game like hangman. The secret word is brains. Your score starts at 100 and every time you *miss a leter you lose ten points. * When you get to 0 points you lose. */ public class wordGuess {

/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final String SECRET_WORD = "BRAINS"; final String FLAG = "!"; final int LOSING_SCORE = 0; String wordSoFar = "", updatedWord = ""; String letterGuess, wordGuess = ""; int numGuesses = 0; int score = 100; Scanner input = new Scanner(System.in); /* begin game */ System.out.println("WordGuess Game.\n"); for (int i = 0; i < SECRET_WORD.length(); i++) { wordSoFar += "-"; } System.out.println(wordSoFar + "\n"); /* allow player to make guesses */ do { System.out.println("Current Score: " + score); System.out.print("Enter a letter (" + FLAG + " to guess entire word): "); letterGuess = input.nextLine(); letterGuess = letterGuess.toUpperCase(); /* Allow player to make guesses */ numGuesses += 1; /* player correctly guessed a letter--extract string in wordSoFar * up to the letter guessed and then append guessed letter to that * string Next, extract rest of wordSoFar and append after the guessed * letter */ if (SECRET_WORD.indexOf(letterGuess) >= 0) { updatedWord = wordSoFar.substring(0, SECRET_WORD.indexOf(letterGuess)); updatedWord += letterGuess; updatedWord += wordSoFar.substring(SECRET_WORD.indexOf(letterGuess)+1, wordSoFar.length()); wordSoFar = updatedWord; } else { score -= 10; } if (score <= LOSING_SCORE) { break; } /* display guessed letter instead of dash */ System.out.println(wordSoFar + "\n"); } while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD)); /* finish game and display message and number of guesses */ if (letterGuess.equals(FLAG)) { System.out.println("What is your guess? "); wordGuess = input.nextLine();

wordGuess = wordGuess.toUpperCase(); } if (wordGuess.equals(SECRET_WORD) || wordSoFar.equals(SECRET_WORD)) { System.out.println("You Won!"); } else { System.out.println("Sorry. You lose."); } System.out.println("The secret word is " + SECRET_WORD); System.out.println("You made " + numGuesses + " guesses."); } }

Console:
WordGuess Game. -----Current Score: 100 Enter a letter (! to guess entire word): a --A--Current Score: 100 Enter a letter (! to guess entire word): e --A--Current Score: 90 Enter a letter (! to guess entire word): i --AI-Current Score: 90 Enter a letter (! to guess entire word): o --AI-Current Score: 80 Enter a letter (! to guess entire word): u --AI-Current Score: 70 Enter a letter (! to guess entire word): b B-AI-Current Score: 70 Enter a letter (! to guess entire word): r BRAI-Current Score: 70 Enter a letter (! to guess entire word): s BRAI-S Current Score: 70 Enter a letter (! to guess entire word): n BRAINS You Won! The secret word is BRAINS You made 9 guesses.

Você também pode gostar