🔒 Closed Pa help po sa java

Status
Not open for further replies.

Jmrie_

in memoriam 1995-2021
Legendary
Yung sir ko na si sir efren gusto niya ipagawa saamen yung sa notepad
Mag code dun tapos dapat lumabas sa out put yung
991
Ninehundred ninenty one
Ganyan daw dapat lumabas sa cmd.
Paano po magawa yung ganyan sa java?
 
Code:
import java.util.Scanner;

public class Test1 {

       public static void main(String[] args) {
           int number = 0;
           Scanner scanner = new Scanner(System.in);
           System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
           number = scanner.nextInt();
           while(number!=-1){
               if(number>=0 && number<=999){
                   if(number==0){
                       System.out.print("NUMBER AFTER CONVERSION:\tZERO");
                   } else {
                       System.out.print("NUMBER AFTER CONVERSION:\t");
                       numberToWord(((number / 100) % 10), " HUNDRED");
                       numberToWord((number % 100), " ");
                   }

               } else{
                   System.out.print("NUMBER OUT OF RANGE");
               }
               System.out.print("\nPlease type a number between 0 and 999 OR type -1 to exit:  ");
               number = scanner.nextInt();
           }
       }

       public static void numberToWord(int num, String val) {
           String ones[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"
           };
           String tens[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
           if (num > 19) {
               System.out.print(tens[num / 10] + " " + ones[num % 10]);
           } else {
               System.out.print(ones[num]);
           }
           if (num > 0) {
               System.out.print(val);
           }
       }
   }
Salamat po ng sobra pasensiya na po
 
Ok po nag aaral din po kasi ako ng java gusto ko kasi masterin din yung java nag simula din po kami dyan sa notepad saka sa cmd din namen po pinapa run.
Buti po nag reply ka nahihirapan ako mapa run sa java e di kasi lumalabas code ko sa cmd e file not found nakalagay e
 
Code:
import java.util.*;

public class NumberToWords
{
    static int number;
    static Scanner userInput;
    static String ones[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"};
    static String tens[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
    static int num;
    static String val;
   
    public static void main ( String [] args){
       
        initializeNumber();
       
        initializeScanner();
       
        whileNumber();
       
        logic(num,val);
       
    }
   
    static void initializeNumber(){
        number = 0;
    }
   
    static void initializeScanner(){
       
        userInput = new Scanner(System.in);
    }
   
    static void whileNumber(){
       
       
        System.out.print("Please type a number between 0 and 999  \n");
        number = userInput.nextInt();
           
        if(number>=0 && number<=999){
            if(number==0){
                System.out.print("NUMBER AFTER CONVERSION:\tZERO");
            } else {
                System.out.print("NUMBER AFTER CONVERSION:\t");
                logic(((number / 100) % 10), " HUNDRED");
                logic((number % 100), " ");
            }

        } else{
            System.out.print("NUMBER OUT OF RANGE");
        }
        System.out.print("\nPlease type a number between 0 and 999 OR  \n");   
   
}
   
    static void logic(int num, String val){
        if (num > 19) {
            System.out.print(tens[num / 10] + " " + ones[num % 10]);
        } else {
            System.out.print(ones[num]);
        }
        if (num > 0) {
            System.out.print(val);
        }
    }
}
 
May ginawa kaming ganyan nong senior high para sa advance java (android development) namin.

Lagay mo sa array mam.

String [] one = {"one", "two", ...};
String [] two = {"ten", "twenty", ...};
String [] twoSpecial = {"eleven", "twelve", ...};

String manipulation (str.charAt(index)) para kunin mo isa isa yong input.
 

Attachments

  • Screenshot_20190127-111109.webp
    Screenshot_20190127-111109.webp
    149.8 KB · Views: 20
view

You do not have permission to view the full content of this post. Log in or register now.

Logic side written in C# Console which is very similar to Java

This will convert from 0 to Millions. from negative to positive to integers to decimal point.

*Max is 2M input.

//Conversion for Decimals
private static string DecimalToWords(decimal number)
{
string inWords = "";

int firstNumber, secondNumber = 0;

string[] splitNumber = new string[2];

splitNumber = number.ToString().Split('.');

firstNumber = int.Parse(splitNumber[0]);
secondNumber = int.Parse(splitNumber[1]);

inWords += IntegerToWords(firstNumber) + " Point " + IntegerToWords(secondNumber);

return inWords;
}

//Convertion for Integers
private static string IntegerToWords(int number)
{
string inWords = "";
string[] Units = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
string[] Tens = new string[] { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

if (number == 0)
{
return "Zero";
}

if (number < 0)
{
return "Negative " + IntegerToWords(Math.Abs(number));
}

inWords = "";

if ((number / 1000000) > 0)
{
inWords += IntegerToWords(number / 1000000) + " Million ";
number %= 1000000;
}

if ((number / 1000) > 0)
{
inWords += IntegerToWords(number / 1000) + " Thousand ";
number %= 1000;
}

if ((number / 100) > 0)
{
inWords += IntegerToWords(number / 100) + " Hundred ";
number %= 100;
}

if (number > 0)
{
if (number < 20)
{
inWords += Units[number];
}
else
{
inWords += Tens[number / 10];
if ((number % 10) > 0)
{
inWords += "-" + Units[number % 10];
}
}
}

return inWords;
}

 
view

You do not have permission to view the full content of this post. Log in or register now.

Logic side written in C# Console which is very similar to Java

This will convert from 0 to Millions. from negative to positive to integers to decimal point.

*Max is 2M input.

//Conversion for Decimals
private static string DecimalToWords(decimal number)
{
string inWords = "";

int firstNumber, secondNumber = 0;

string[] splitNumber = new string[2];

splitNumber = number.ToString().Split('.');

firstNumber = int.Parse(splitNumber[0]);
secondNumber = int.Parse(splitNumber[1]);

inWords += IntegerToWords(firstNumber) + " Point " + IntegerToWords(secondNumber);

return inWords;
}

//Convertion for Integers
private static string IntegerToWords(int number)
{
string inWords = "";
string[] Units = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
string[] Tens = new string[] { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

if (number == 0)
{
return "Zero";
}

if (number < 0)
{
return "Negative " + IntegerToWords(Math.Abs(number));
}

inWords = "";

if ((number / 1000000) > 0)
{
inWords += IntegerToWords(number / 1000000) + " Million ";
number %= 1000000;
}

if ((number / 1000) > 0)
{
inWords += IntegerToWords(number / 1000) + " Thousand ";
number %= 1000;
}

if ((number / 100) > 0)
{
inWords += IntegerToWords(number / 100) + " Hundred ";
number %= 100;
}

if (number > 0)
{
if (number < 20)
{
inWords += Units[number];
}
else
{
inWords += Tens[number / 10];
if ((number % 10) > 0)
{
inWords += "-" + Units[number % 10];
}
}
}

return inWords;
}
Ang sakit sa ulo neto
 
Yan para mas madaling intindihin. hehe! may mga online java convertion naman e. convert mo na lang.

Code:
//Conversion for Decimals
        private static string DecimalToWords(decimal number)
        {
            string inWords = "";

            int firstNumber, secondNumber = 0;

            string[] splitNumber = new string[2];

            splitNumber = number.ToString().Split('.');

            firstNumber = int.Parse(splitNumber[0]);
            secondNumber = int.Parse(splitNumber[1]);

            inWords += IntegerToWords(firstNumber) + " Point " + IntegerToWords(secondNumber);

            return inWords;
        }

        //Convertion for Integers
        private static string IntegerToWords(int number)
        {
            string inWords = "";
            string[] Units = new string[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
            string[] Tens = new string[] { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

            if (number == 0)
            {
                return "Zero";
            }

            if (number < 0)
            {
                return "Negative " + IntegerToWords(Math.Abs(number));
            }

            inWords = "";

            if ((number / 1000000) > 0)
            {
                inWords += IntegerToWords(number / 1000000) + " Million ";
                number %= 1000000;
            }

            if ((number / 1000) > 0)
            {
                inWords += IntegerToWords(number / 1000) + " Thousand ";
                number %= 1000;
            }

            if ((number / 100) > 0)
            {
                inWords += IntegerToWords(number / 100) + " Hundred ";
                number %= 100;
            }

            if (number > 0)
            {
                if (number < 20)
                {
                    inWords += Units[number];
                }
                else
                {
                    inWords += Tens[number / 10];
                    if ((number % 10) > 0)
                    {
                        inWords += "-" + Units[number % 10];
                    }
                }
            }

            return inWords;
        }
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 21
    Replies
  • 1K
    Views
  • 6
    Participants
Last reply from:
Jmrie_

Trending Topics

Online now

Members online
439
Guests online
1,484
Total visitors
1,923

Forum statistics

Threads
2,279,849
Posts
28,993,576
Members
1,226,183
Latest member
gro
Back
Top