Advertisement
fosterbl

L9ForLoopsDay2.java COMPLETE

Oct 7th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //Scope
  4. //Nested for loops
  5. //For loops to while loops
  6. //Strings
  7. public class L9ForLoopsDay2{
  8.    public static void main(String[] args){
  9.       //Declaring a variable inside of an if-statement, loop, or any block of code { }
  10.       //limits that variable to existing inside of that block of code ONLY
  11.       //The examples below are illegal. Try uncommenting them and running them to see
  12.       // //Ex1
  13. //       int a = 4;
  14. //       if( a >= 3 ){
  15. //          int b = 5;
  16. //       }
  17. //       System.out.println( b );
  18. //       //Ex2
  19. //       for(int i = 0; i < 3; i++){
  20. //          System.out.println("hi");
  21. //       }
  22. //       System.out.println( i );
  23.  
  24. //       //Nested For Loop example
  25.       System.out.println( "i\tj" );
  26.       for( int i = 1; i <= 2; i++ ){
  27.          for( int j = 1; j <= 3; j++ ){//this loop is inside and happens more often
  28.             System.out.println( i + "\t" + j );//see that j changes more often and when j goes past 3, i gets incremented and inner loop starts again
  29.          }
  30.       }
  31.  
  32.       //Print a 10 x 10 box of *
  33.       //or any of the other options in #18 on http://www.beginwithjava.com/java/loops/questions.html
  34.       for(int b = 1; b <= 10; b++){
  35.          for(int a = 1; a <= 10; a++){
  36.             System.out.print("* ");
  37.          }
  38.          System.out.println();
  39.       }
  40. //       //For loop to While loop
  41. //       for( int k = 0; k < 9; k++ ){
  42. //          System.out.println( k );
  43. //       }
  44. //      
  45. //       int k = 0;//initialization expression goes before
  46. //       while( k < 9 ){//control expression goes inside
  47. //          System.out.println( k );
  48. //          k++;//step expression goes at bottom of loop
  49. //       }
  50.  
  51. //       //Loop over the characters in a String
  52.       Scanner kb = new Scanner(System.in);
  53.       System.out.print("Enter a word: ");
  54.       String word = kb.next();
  55.       for(int i = 0; i < word.length(); i++){//start loop @ index 0, go up until but not including the length, count by 1
  56.          String letter = word.substring(i, i+1);//substring(i,i+1) gets one letter at a time (first thing you want, first thing you don't want)
  57.          System.out.println( letter );
  58.       }
  59.  
  60.       //Write code to find how many vowels the user's name contains. The user inputs their name from the keyboard
  61.       System.out.print("Enter your name: ");
  62.       String name = kb.next();
  63.       int vowels = 0;
  64.       for(int i = 0; i < name.length(); i++ ){
  65.          String letter = name.substring(i, i+1).toLowerCase();
  66.          if( letter.equals("a") || letter.equals("e") || letter.equals("i")
  67.                || letter.equals("o") || letter.equals("u") ){
  68.             vowels++;  
  69.          }      
  70.       }
  71.       System.out.println( "Your name has " + vowels + " vowels" );
  72.    }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement