Number Comparison Program in Java & C

 // This time I try to follow the lead from "Java Fundamental" video, part 1-3.

// Written a number comparison program in Java and C

Code:
  1. // Comparison in Java  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. public class Comparison  
  6. {  
  7.   
  8.     public static void main(String args[])  
  9.     {  
  10.   
  11.         Scanner input = new Scanner(System.in);         
  12.    // create a new Scanner call 'input' and using System.in method.'Yet to be comfirmed.'
  13.   
  14.   
  15.   
  16.         int n1, n2;  
  17.   
  18.         // Display tips and assign value from input to n1 and n2  
  19.   
  20.         System.out.print("First Number: ");  
  21.   
  22.         n1 = input.NextInt();  
  23.   
  24.   
  25.   
  26.         System.out.print("Second Number: ");  
  27.   
  28.         n2 = input.NextInt();  
  29.   
  30.   
  31.   
  32.         if (n1 > n2)  
  33.         {  
  34.             System.out.printf("%d is larger than %d. /n", n1, n2);  
  35.         } else if (n1 == n2)  
  36.         {  
  37.             System.out.printf("They are equal. /n");  
  38.         } else  
  39.         {  
  40.             System.out.printf("%d is less than %d. /n", n1, n2);  
  41.         }  
  42.   
  43.     }  
  44. }  
Code:
  1. // Number Comparision Program  
  2. // Written in C by R.Wong  
  3.   
  4. #include <stdio.h>  
  5.   
  6. int main(void)  
  7.   
  8. {  
  9.   
  10.     int n1, n2;  
  11.   
  12.     printf("First Nubmer: ");  
  13.   
  14.     scanf("%d", &n1);  
  15.   
  16.    
  17.   
  18.     printf("Second Number: ");  
  19.   
  20.     scanf("%d", &n2);  
  21.   
  22.    
  23.   
  24.     if (n1 > n2)  
  25.   
  26.     printf("%d is bigger than %d. /n", n1, n2);  
  27.   
  28.     else if (n1 == n2)  
  29.   
  30.         printf("They are equal. /n");  
  31.   
  32.         else  
  33.   
  34.         printf("%d is less than %d. /n", n1, n2);  
  35.   
  36.    
  37.     return 0;  
  38.   
  39. }  

/** (Quiz) 1.

* Expression:' Scanner input = new Scanner(System.in); '

* means what? Is it correct if I understand them this way: Create a 'new' 'Scanner' INSTANCE from 'Scanner' class

* and using 'System.in' METHOD as standard system input?

*/

 

/** (Quiz) 2.

* Expression: 'n1 = input.NextInt();'

* What is this means? I think it is: 'input' (a INSTANCE of Scanner CLASS) using NextInt() METHOD to aquire a value

* and pass it to n1??  What is the NextInt(), FUNCTION, METHOD, or INTERFACE??

*/

 

/** Any comments is welcomed.*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章