|
|
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.
|
|

01-09-2008, 06:33 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 2
|
|
|
Finding largest and smallest integer
This code shows malicious behavior.Actually I dont know what is wrong.I can solve using max and min method but I have to do using if else so Pls help me.Here is my code:
import javax.swing.*;
public class Max_min {
public static void main(String[]args){
int a=0,b=0,c=0;
String numA,numB,numC;
numA=JOptionPane.showInputDialog( "Enter number A");
a=Integer.parseInt(numA);
numB=JOptionPane.showInputDialog( "Enter number B");
b=Integer.parseInt(numB);
numC=JOptionPane.showInputDialog( "Enter number C");
c=Integer.parseInt(numC);
if(a>b)
{
if(b>c){
JOptionPane.showMessageDialog(null," a is greatest and c is smallest");
}
else JOptionPane.showMessageDialog(null," a is greatest and b is smallest");
}
else if(b>a)
{
if(a>c){
JOptionPane.showMessageDialog(null," b is greatest and c is smallest");
}
else JOptionPane.showMessageDialog(null," b is greatest and a is smallest");
}
else if(c>b)
{
if(b>a){
JOptionPane.showMessageDialog(null," c is greatest and a is smallest");
}
else JOptionPane.showMessageDialog(null," c is greatest and b is smallest");
}
}
}
Last edited by mlhazan : 01-09-2008 at 06:35 AM.
|
|

01-09-2008, 08:34 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
import javax.swing.*;
public class MaxTest {
public static void main(String[] args) {
int[][] vals = {
{ 7, 9, 3 }, { 9, 6, 3 }, { 6, 2, 9 },
{ 2, 3, 8 }, { 1, 3, 2 }, { 6, 1, 4 }
};
System.out.println(" a b c greatest smallest");
System.out.println("----------------------------");
for(int j = 0; j < vals.length; j++) {
int a = vals[j][0];
int b = vals[j][1];
int c = vals[j][2];
System.out.printf(" %d %d %d ", a, b, c);
if(a>b) {
if(a > c) {
if(b > c) {
System.out.println(" a c");
} else {
System.out.println(" a b");
}
} else { // (c > a)
System.out.println(" c b");
}
} else { // (b > a)
if(b > c) {
if(c > a) {
System.out.println(" b a");
} else {
System.out.println(" b c");
}
} else { // (c > b)
System.out.println(" c a");
}
}
}
}
}
|
|

01-13-2008, 12:30 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 2
|
|
|
thank you
Thank you very much for your reply.It works.
Originally Posted by hardwired
import javax.swing.*;
public class MaxTest {
public static void main(String[] args) {
int[][] vals = {
{ 7, 9, 3 }, { 9, 6, 3 }, { 6, 2, 9 },
{ 2, 3, 8 }, { 1, 3, 2 }, { 6, 1, 4 }
};
System.out.println(" a b c greatest smallest");
System.out.println("----------------------------");
for(int j = 0; j < vals.length; j++) {
int a = vals[j][0];
int b = vals[j][1];
int c = vals[j][2];
System.out.printf(" %d %d %d ", a, b, c);
if(a>b) {
if(a > c) {
if(b > c) {
System.out.println(" a c");
} else {
System.out.println(" a b");
}
} else { // (c > a)
System.out.println(" c b");
}
} else { // (b > a)
if(b > c) {
if(c > a) {
System.out.println(" b a");
} else {
System.out.println(" b c");
}
} else { // (c > b)
System.out.println(" c a");
}
}
}
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|