hi i am trying to passing arthematic expressions through command line arguments but i only get result of addition and multiplication cant get result of
subtraction and division:
//c:\jdk1.6.0\bin\javac mentalmath
//compilation is successful
c:\jdk1.6.0\bin\java mentalmath 12 6
**result** 72 18 // only give result of multiplication and addition of above numbers. but not give result of subtraction or divsion.
Code:
class mentalmath
{
public static void main(String args[])
{
int multiply= 1;
int sum=0;
// int minus=0;
for(int i=1;i<=args.length;i++)
{
multiply=multiply*Integer.parseInt(args[i]);
sum=sum+Integer.parseInt(args[i]);
// minus=minus-Integer.parseInt(args[i]);
}
//System.out.println("the minus is"+minus);
System.out.println("The product is " + multiply);
System.out.println("the sum is"+sum);
}
}
3 2009
hi i am trying to passing arthematic expressions through command line arguments but i only get result of addition and multiplication cant get result of
subtraction and division:
//c:\jdk1.6.0\bin\javac mentalmath
//compilation is successful
c:\jdk1.6.0\bin\java mentalmath 12 6
**result** 72 18 // only give result of multiplication and addition of above numbers. but not give result of subtraction or divsion.
Code:
class mentalmath
{
public static void main(String args[])
{
int multiply= 1;
int sum=0;
// int minus=0;
for(int i=1;i<=args.length;i++)
{
multiply=multiply*Integer.parseInt(args[i]);
sum=sum+Integer.parseInt(args[i]);
// minus=minus-Integer.parseInt(args[i]);
}
//System.out.println("the minus is"+minus);
System.out.println("The product is " + multiply);
System.out.println("the sum is"+sum);
}
}
1.) Code tags code tags code tags code tags code tags code tags code tags
2.) Where in your code did you instruct the program to subtract or divide?
hi i am trying to passing arthematic expressions through command line arguments but i only get result of addition and multiplication cant get result of
subtraction and division:
//c:\jdk1.6.0\bin\javac mentalmath
//compilation is successful
c:\jdk1.6.0\bin\java mentalmath 12 6
**result** 72 18 // only give result of multiplication and addition of above numbers. but not give result of subtraction or divsion.
Code:
class mentalmath
{
public static void main(String args[])
{
int multiply= 1;
int sum=0;
// int minus=0;
for(int i=1;i<=args.length;i++)
{
multiply=multiply*Integer.parseInt(args[i]);
sum=sum+Integer.parseInt(args[i]);
// minus=minus-Integer.parseInt(args[i]);
}
//System.out.println("the minus is"+minus);
System.out.println("The product is " + multiply);
System.out.println("the sum is"+sum);
}
}
You should start your loop at i= 0, not i= 1 and you should stop one loop pass
earlier; like this: -
for (int i= 0; i < args.length; i++) ...
-
kind regards,
Jos
Hi,
if you want to get the result for minus, u need to use below code:
minus=Integer.parseInt(args[0])-Integer.parseInt(args[1]);
For the division also u need to use as above.
cheers,
Sateesh
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paul Rubin |
last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd
like to use an assignment expression, like
for line in file:
if (g := re.match(pat, line)):
croggle(g.group(1))
Since...
|
by: MOvetsky |
last post by:
Is the following code ISO C++ standard compliant?
If yes, is it guaranteed that it will not crash on compliant platforms?
If yes, will it print "Pointers are equal" on any compliant platform?
Will...
|
by: Bill Reid |
last post by:
Bear with me, as I am not a "professional" programmer, but I was
working on part of program that reads parts of four text files into
a buffer which I re-allocate the size as I read each file. I...
|
by: jerry |
last post by:
I need to modify the code of a command-line tool. The source code
starts out like this:
int main(int argc, char *argv)
{
int ch, A, B ;
while ((ch = getopt(argc, argv, "AB")) != -1)...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
| |