| | |
Why??
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 9
Reputation:
Solved Threads: 0
import java.util.Scanner;
public class stack {
/**
* @param args
*/
public static void main(String[] args) {
int result ;
int output ;
Scanner num = new Scanner(System.in);
int input = num.nextInt();
for(int j = 0;j<input;j++)
{
int output1 = 0;
output = num.nextInt();
for(int i = 0;i<output;i++)
{
if(output == 1 - 3000){
output = output1;
return;
}
if(output!=1-3000){
if(output ==('+')){
result = (output1 + output1);
result++ ;
System.out.println(result);
}
else if(output==('-')){
result = (output - output);
result++;
System.out.println(result);
}
else if(output == ('*')){
result = (output * output);
result++;
System.out.println(result);
}
}
}
}
}
}
Please Tell me
Why error.
input
9
7 2 – 5 2 * 36 - *
output
-130
public class stack {
/**
* @param args
*/
public static void main(String[] args) {
int result ;
int output ;
Scanner num = new Scanner(System.in);
int input = num.nextInt();
for(int j = 0;j<input;j++)
{
int output1 = 0;
output = num.nextInt();
for(int i = 0;i<output;i++)
{
if(output == 1 - 3000){
output = output1;
return;
}
if(output!=1-3000){
if(output ==('+')){
result = (output1 + output1);
result++ ;
System.out.println(result);
}
else if(output==('-')){
result = (output - output);
result++;
System.out.println(result);
}
else if(output == ('*')){
result = (output * output);
result++;
System.out.println(result);
}
}
}
}
}
}
Please Tell me
Why error.
input
9
7 2 – 5 2 * 36 - *
output
-130
I understood what you are trying to do but I think you should write it again. because it wont work. ok here how you should write it.
1 ) make a loop that turns until there is no token left on scanner.
and this is not it. input variable stores first interger token. which is 7 so it will loop 7 times
2) if next token is number put next token in stack if it is not number it shuld be + - * or / then pop 2 number from stack and do the calculation and put result back in stack.at the and you should have only one. number and it will be result.
note) if at the end ,there is more than one number in stack given string is not valid. when you got an operator from scanner and there is less than two number in stack string is not valid. and if string you got is not number nor operator string is not valid. good luck.
(
I dont know what this is and you probably dont know what this is either.this is same thing as
1 ) make a loop that turns until there is no token left on scanner.
java Syntax (Toggle Plain Text)
for(int j = 0;j<input;j++)
2) if next token is number put next token in stack if it is not number it shuld be + - * or / then pop 2 number from stack and do the calculation and put result back in stack.at the and you should have only one. number and it will be result.
note) if at the end ,there is more than one number in stack given string is not valid. when you got an operator from scanner and there is less than two number in stack string is not valid. and if string you got is not number nor operator string is not valid. good luck.
(
java Syntax (Toggle Plain Text)
if(output == 1 - 3000)
java Syntax (Toggle Plain Text)
if(output ==-2999)
![]() |
Other Threads in the Java Forum
- Previous Thread: how to modify the code to Creating multiple instances of a Class <<Very basic>>
- Next Thread: nested Arraylists
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






