Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. // Declaring ints
  2. int x; // changing int for loop
  3.  
  4. // Getting user input
  5. // Name
  6. System.out.println("What is the name of your favorite pet?");
  7. String name=in.next(); // name--> name of pet user entered
  8. // How many times
  9. System.out.println("How many times would you like to see the name appear on the screen?");
  10. int times=in.nextInt(); // times--> amount of times user wants to see name
  11. // Vertically/Horizontally
  12. System.out.println("Would you like to see the names:\n1. Once per line\n2. Horizontally");
  13. int choice=in.nextInt(); // choice --> used for switch statement
  14.  
  15. // Switch statement
  16. switch(choice)
  17. { // Start of switch
  18. case 1:
  19. {
  20. if(times>0) // test for positive interger
  21. {
  22. for(x=0; x<times; x++)
  23. {
  24. System.out.println(name);
  25. } // End of switch
  26.  
  27. } // End of if
  28.  
  29. else
  30. {
  31. System.out.println("Invalid number for name printing, enter a positive interger.");
  32. }
  33.  
  34. } // End of case 1
  35.  
  36. case 2:
  37. {
  38. if(times>0) // test for positive interger
  39. {
  40. for(x=0; x<times; x++)
  41. {
  42. System.out.print(name+"\t");
  43. } // End of switch
  44.  
  45. } // End of if
  46. else
  47. {
  48. System.out.println("Invalid number for name printing, enter a positive interger.");
  49. }
  50. }
  51. default:
  52. {
  53. System.out.println("Invalid choice, enter one of the following options availible.");
  54. }
  55.  
  56. } // End of switch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement