Advertisement
The_Newt

[Guide & Examples] Java

Oct 6th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. /*
  2. coded by newt
  3. only some of the codes are compatible with cmd; for educational purposes only
  4. all codes below (except /* codes) are original and thought of in the brightest way possible
  5. they are all examples, so feel free to modify them in any manner if possible
  6. */
  7.  
  8. /* example of declaring multiple variables */
  9. int a,b,c;
  10. float marks;
  11. char name;
  12.  
  13. /* example of initializing multiple variables */
  14. a=3, b=5;
  15. marks=45.5;
  16. name='y';
  17.  
  18. /* example of declaring and initializing variables at the same time */
  19. int a=2, b=4;
  20. float marks=34.5
  21. double do=20.22d;
  22.  
  23. /* accepting two numbers and printing their sum */
  24. import java.util.*;
  25. class total
  26. {
  27.     public static void main(String[]args)
  28.     {
  29.         Scanner sc - new Scanner(System.in);
  30.         int a,b,tot;
  31.        
  32.         System.out.print("\n Please enter the first number: ");
  33.         a=sc.nextInt(); /* inputs statement in java & inputs a number in a */
  34.        
  35.         System.out.print("\n Please enter the second number: ");
  36.         b=sc.nextInt();
  37.        
  38.         tot=a+b;
  39.         System.out.println(" \ The sum is " +tot);
  40.     }
  41. }
  42.  
  43. /* IF statement syntax */
  44. if (condition)
  45. {
  46.     True statements
  47. }
  48. else
  49. {
  50.     False statement
  51. }
  52.  
  53. /* to accept two numbers and print the largest of the two numbers */
  54. import java.util.*;
  55. class diff
  56. {
  57.     public static void main(String[]args)
  58.     {
  59.         Scanner sc - new Scanner(System.in);
  60.         int a,b;
  61.        
  62.         System.out.print("n\ Please enter the first number: ");
  63.         a=sc.nextInt(); /* inputs statement in java & inputs a number in a */
  64.        
  65.         System.out.print("n\ Please enter the second number: ");
  66.         b=sc.nextInt();
  67.        
  68.         if (a>b)
  69.         {
  70.             System.out.println("The first number is bigger");
  71.         }
  72.         else
  73.         {
  74.             System.out.println("The second number is bigger");
  75.         }
  76.     }
  77. }
  78.  
  79. /* to print natural numbers from 1 to 5 */
  80. class natural
  81. {
  82.     public static void main(String[]args)
  83.     {
  84.         int i=1;
  85.         while(i<=5)
  86.         {
  87.             System.out.println("The value of i is " +i);
  88.             i=i+1;
  89.         }
  90.     }
  91. }
  92.  
  93. /* syntax when using DO, WHILE LOOP */
  94. do
  95. {
  96.     block of statements
  97. }
  98. while(test condition)
  99.  
  100. /* to print even numbers from 2 to 10 */
  101. class evennumber
  102. {
  103.     public static void main(String[]args)
  104.     {
  105.         int i=2;
  106.         System.out.println("The even numbers from 1 to 5 are:");
  107.         while(i<=10)
  108.         {
  109.             System.out.println(i);
  110.             i=i+2;
  111.         }
  112.     }
  113. }
  114.  
  115. /* syntax when using FOR LOOP */
  116. for (start ; condition ; step value)
  117.  
  118. /* to print numbers from 1 to 10 in reverse order */
  119. class reverse
  120. {
  121.     public static void main(String[]args)
  122.     {
  123.         int=i;
  124.         System.out.println("The numbers from 1 to 10 in reverse order are:");
  125.         for(i=10;i>=1;i--)
  126.         {
  127.             System.out.println(i);
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133. /*
  134. congratulations! you have finished newt's java example guide!
  135. if you read through everything and used some of it - GREAT!
  136. if you just scrolled to the end - LOSER!
  137.  
  138. copyright 2014 newt drost
  139. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement