Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. import java.util.ArrayList;
  4.  
  5. public class question6 {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9.  
  10. Scanner input = new Scanner (System.in);
  11.  
  12. String secretword = "testing";
  13.  
  14. // now we need to create an array that stores characters so that we can swap them
  15. // this is an array of characters that has the same length as the secret word
  16.  
  17. char[] dashes =new char[secretword.length()];
  18.  
  19.  
  20. // now we need to add dashes to the array
  21.  
  22. for (int i=0; i<secretword.length(); i++){
  23. dashes[i]= '_' ;
  24. }
  25.  
  26.  
  27. // now we need to start a loop that asks the user for input until all the dashes are swapped
  28.  
  29. // declaring variables for loop counter and steps
  30. int counter = 0;
  31. int steps =0;
  32.  
  33. // condition remains true until the counter is the same length as the secret word to make sure we swap everything
  34. while (counter<secretword.length()){
  35.  
  36.  
  37. // asking for input
  38. System.out.print("Key in one character or your guess word: ");
  39. String userinput = input.nextLine();
  40.  
  41. // if it is a character
  42. if (userinput.length() == 1){
  43. for (int i = 0; i<secretword.length(); i++){
  44.  
  45. // swapping
  46. if (userinput.charAt(0) == secretword.charAt(i)){
  47.  
  48. dashes[i] = userinput.charAt(0);
  49. counter ++;
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56. // swapping the whole word
  57. else if (userinput.equals(secretword)){
  58. for (int j=0; j<secretword.length(); j++){
  59. dashes[j]= userinput.charAt(j);
  60. counter = secretword.length();
  61. }
  62. }
  63.  
  64.  
  65.  
  66.  
  67. steps ++;
  68. System.out.println(dashes);
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement