Results 1 to 3 of 3

Thread: Java Help

  1. #1
    Ryano is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default Java Help

    I am not sure if I am in the right forum but I am writing a code in Java to produce random multiplication. I wrote it in C++ but I am kinda new to Java. Can anyone help me translate this program that I wrote in c++ to Java.

    using std::cin;
    using std::endl;

    void multiplication();

    int main()
    {
    multiplication();

    return 0;
    }
    void multiplication()
    {
    int x;
    int y;
    int response = 0;

    while ( response != -1 ) {
    x = rand() % 10;
    y = rand() % 10;

    cout << " How much is " << x << " times " << y
    << " ( -1 to End ) ? : ";
    cin >> response;

    while ( response != -1 && response != x * y ) {
    cout << " No. Please try again: " ;

    cin >> response;

    } //End While

    if ( response != -1)
    cout << "Very Good!\n\n";

    } //End while

    } // End function multiplication

  2. #2
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    First off, a few pointers (see comments)

    Java Code:
    using std::cin;
    using std::endl; // #include changes to import, there are no namespaces
    
    void multiplication();  // this isn't needed no matter where your functions (or methods, as they're called in Java) are placed
    
    /* main changes to
    public static void main(String[] args) {} // notice that return type is void
    */
    int main()
    {
    multiplication();
    
    return 0;
    }
    void multiplication()
    {
    int x;
    int y;
    int response = 0;
    
    while ( response != -1 ) {
    x = rand() % 10; // I'm not sure what you tried to do here
    y = rand() % 10; // but these two would change
    
    cout << " How much is " << x << " times " << y << " ( -1 to End ) ? : ";  // cout changes to System.out.print(stuffToWrite) and << changes to a plus sign (+)
    cin >> response; // cin is trickier, too much to explain
    
    while ( response != -1 && response != x * y ) {
    cout << " No. Please try again: " ;
    
    cin >> response;
    
    } //End While
    
    if ( response != -1)
    cout << "Very Good!\n\n";
    
    } //End while
    
    } // End function multiplication
    I didn't know how new you were, so I just converted everything I know.

    I would convert the whole program to Java but I can't see what you are doing with "rand() % 10", if you tell me what rand() does then I should be able to convert it for you, unless you think you have it down.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class MultiplyTest {
        static Random seed = new Random();
        static Scanner scanner;
    
        public static void main(String[] args) {
            scanner = new Scanner(System.in);
            multiply();
            scanner.close();
        }
    
        private static void multiply() {
            int response = 0;
    
            while(response != -1) {
                int x = 1 + seed.nextInt(10);  // [ 1 <= x <= 10]
                int y = 1 + seed.nextInt(10);
                System.out.print("How much is " + x +
                                   " times " + y + " ? : ");
                try {
                    response = Integer.parseInt(scanner.nextLine());
                } catch(NumberFormatException e) {
                    System.out.println("number format: " + e.getMessage());
                }
                
                while(response != -1 && response != x*y) {
                    System.out.print("No. Please try again: ");
                    try {
                        response = Integer.parseInt(scanner.nextLine());
                    } catch(NumberFormatException e) {
                        System.out.println("number format: " + e.getMessage());
                    }
                }
    
                if(response != -1)
                    System.out.println("Very Good");
    
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •