Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- import java.util.ArrayList;
- public class question6 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner input = new Scanner (System.in);
- String secretword = "testing";
- // now we need to create an array that stores characters so that we can swap them
- // this is an array of characters that has the same length as the secret word
- char[] dashes =new char[secretword.length()];
- // now we need to add dashes to the array
- for (int i=0; i<secretword.length(); i++){
- dashes[i]= '_' ;
- }
- // now we need to start a loop that asks the user for input until all the dashes are swapped
- // declaring variables for loop counter and steps
- int counter = 0;
- int steps =0;
- // condition remains true until the counter is the same length as the secret word to make sure we swap everything
- while (counter<secretword.length()){
- // asking for input
- System.out.print("Key in one character or your guess word: ");
- String userinput = input.nextLine();
- // if it is a character
- if (userinput.length() == 1){
- for (int i = 0; i<secretword.length(); i++){
- // swapping
- if (userinput.charAt(0) == secretword.charAt(i)){
- dashes[i] = userinput.charAt(0);
- counter ++;
- }
- }
- }
- // swapping the whole word
- else if (userinput.equals(secretword)){
- for (int j=0; j<secretword.length(); j++){
- dashes[j]= userinput.charAt(j);
- counter = secretword.length();
- }
- }
- steps ++;
- System.out.println(dashes);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement