Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2008, 01:31 AM
Member
 
Join Date: Apr 2008
Posts: 1
Ryano is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-01-2008, 03:20 AM
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
First off, a few pointers (see comments)

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-01-2008, 03:29 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,015
hardwired is on a distinguished road
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"); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 05:23 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org