I am stuck on a homework problem. I think I am on the right track but am getting stuck on the if/else statement. I could be totally wrong though.

Here is my assignment: Write a program that reads three integer inputs into variables. Display the input values in both the ordered entered and in sorted order. This program should sort the numbers so that value1 <= value2 <= value3.

I have only been learning Java for a couple weeks, any help would be appreciated. Hoping to make this a career one day.

Here is the code:

import java.util.Scanner;

public class OrderVariable {
public static void main (String [] args) {

Scanner input = new Scanner (System.in);

System.out.prntln("Enter first variable");
int VariableOne = input.nextInt();

System.out.prntln("Enter second variable");
int VariableTwo = input.nextInt();

System.out.prntln("Enter third variable");
int VariableThree = input.nextInt();

if (VariableOne > VariableTwo && VariableOne > VariableThree){
int Biggest = VariableOne;

else if (VariableTwo > VariableOne && VariableTwo > VariableThree);
int Biggest = VariableTwo;

} else (VariableThree > VariableOne && VariableThree > VariableTwo);
int Biggest = VariableThree;

if (VariableOne < VariableTwo && VariableOne < VariableThree){
int Smallest = VariableOne;

else if (VariableTwo < VariableOne && VariableTwo < VariableThree);
int Smallest = VariableTwo;

} else (VariableThree < VariableOne && VariableThree < VariableTwo);
int Smallest = VariableThree;


if (VariableOne < Biggest && VariableOne > Smallest){
int Mid = VariableOne;

else if (VariableTwo < Biggest && VariableTwo > Smallest);
int Mid = VariableTwo;

} else (VariableThree < Biggest && VariableThree > Smallest);
int Mid = VariableThree;

System.out.println(VariableOne + ", " + VariableTwo + ", " + VariableThree);
System.out.println(Biggest + ", " + Mid + ", " + Smallest);
}
}
;

Edited 4 Years Ago by cpole: n/a

Remove the semi-colons at the ends of those if/else statements.

if( fred < bill)
{
   DoSomething();
}
else if (bill < sam)
{
   DoSomethingElse();
}

Edited 4 Years Ago by thines01: clarity

Thanks, that is a good start but after correcting my code I have 29 errors. Any suggestions?

Always start with the first error and fix that one to the exclusion of all others (except maybe obvious stuff).

What's your first error?

Also, the variables holding the Biggest, Mid and Small should be declared outside of the if statements.

// Scanner stuff here
      int Biggest = 0;
      int Smallest = 0;
      int Mid = 0;
      // if statements here

You should have (no more than) 6 if statements (the last one being optional)

adding to what you need to fix would also be:

System.out.prntln

to

System.out.println

which you had correct at the bottom of your code but not the top

also i see your if else statements look like this:

if (VariableOne > VariableTwo && VariableOne > VariableThree){
int Biggest = VariableOne;
 
else if (VariableTwo > VariableOne && VariableTwo > VariableThree);
int Biggest = VariableTwo;
 
} else (VariableThree > VariableOne && VariableThree > VariableTwo);
int Biggest = VariableThree;

it should be as jamescherrill said but just to add:

if (VariableOne > VariableTwo && VariableOne > VariableThree){
Biggest = VariableOne;
 }
else if (VariableTwo > VariableOne && VariableTwo > VariableThree) {
Biggest = VariableTwo;
} else {
Biggest = VariableThree;
}

Edited 4 Years Ago by DavidKroukamp: n/a

one of the main problem with this coding is that else part should not contain any conditions if you need to add condition it should be if else

why r u complicating much? you can try this coding

import java.util.Scanner;

public class OrderVariable {
public static void main (String [] args) {

Scanner input = new Scanner (System.in);

System.out.println("Enter first variable");
int VariableOne = input.nextInt();

System.out.println("Enter second variable");
int VariableTwo = input.nextInt();

System.out.println("Enter third variable");
int VariableThree = input.nextInt();
 int Biggest, Smallest,Mid;
if (VariableOne > VariableTwo && VariableOne > VariableThree)
{
 Biggest = VariableOne;
 Mid=VariableTwo  > VariableThree? VariableTwo:VariableThree;
 Smallest=VariableTwo < VariableThree? VariableTwo:VariableThree;
}
else if (VariableTwo >  VariableThree)
{
 Biggest = VariableTwo;
 Mid=VariableOne  > VariableThree? VariableOne :VariableThree;
 Smallest=VariableOne  < VariableThree? VariableOne :VariableThree ;
}
else 
{
 Biggest = VariableThree;
 Mid=VariableOne  > VariableTwo? VariableOne :VariableTwo;
 Smallest=VariableOne  < VariableTwo? VariableOne :VariableTwo ;
}
 
System.out.println(VariableOne + ", " + VariableTwo + ", " + VariableThree);
System.out.println(Biggest + ", " + Mid + ", " + Smallest);
}
}

@karthi.k.s:
Please don't give people solutions to their homework. All it teaches them is how to cheat. Help them learn to develop their own solutions.

And if you do post some code, set a good example by following normal Java naming and indentation conventions.

Edited 4 Years Ago by JamesCherrill: n/a

@karthi.k.s:
Please don't give people solutions to their homework. All it teaches them is how to cheat. Help them learn to develop their own solutions.

And if you do post some code, set a good example by following normal Java naming and indentation conventions.

k sir i won't produce code here after
i just don't want to make the program stranger so that i have used variables used by the creator of the thread that's it

Edited 4 Years Ago by karthi.k.s: n/a

k sir i won't produce code here after
i just don't to make the program stranger so that i have used variables used by the creator of the thread that's it

sir

.... i just don't want to make the program stranger so that i have used variables used by the creator of the thread that's it

That's a good point. You were right.

k sir i won't produce code here after

It's OK to post code if you want to show a particular technique or idea, but not the whole homework. You can also use pseudo-code to explain the logic - this is a good thing because the student has to understand the pseudo-code then write a version in Java, and it will help them learn and remember.

Anyway, welcome to DaniWeb!

Thank you guys for your help. I realized I was making some simple mistakes.

Here is the corrected code

import java.util.Scanner;

public class OrderVariable {
	public static void main (String [] args) {

		//creates scanner object
		Scanner input = new Scanner (System.in);

		//assigns variables from user input
		System.out.println("Enter first variable");
		int V1 = input.nextInt();

		System.out.println("Enter second variable");
		int V2 = input.nextInt();

		System.out.println("Enter third variable");
		int V3 = input.nextInt();

		//creates variables which will be used to sort
		int Max = V3;
		int Min = V3;
		int Mid = V3;

		//finds largest number
		if ((V1 > V2) && (V1> V3))
		{
		Max = V1 ;
		}
		else if ((V2 > V1) && (V2 > V3))
		{
		Max = V2;
		}
		else
		{
		Max = V3;
		}
		//finds lowest number
		if ((V1 < V2) && (V1 < V3))
		{
		Min = V1;
		}
		else if ((V2 < V1) && (V2 < V3))
		{
		Min = V2;
		}
		else
		{
		Min = V3;
		}
		//finds middle number
		if ((V1 < Max) && (V1 > Min))
		{
		Mid = V1;
		}
		else if ((V2 < Max) && (V2 < Min))
		{
		Mid = V2;
		}
		else
		{
		Mid = V3;
		}
		//displays numbers in the order entered
		System.out.println(V1 + ", " + V2 + ", " + V3);
		//displays numbers in sorted order smallest to largest
		System.out.println(Min + ", " + Mid + ", " + Max);
	}
}

I used everything I learned to try another program, a simple rock, paper, scissors game. It is not working. Below is the code.

If I assign the variable "Computer" a random number and the variable "Response" a number from user input can I not compare the two? Is that the problem or am I doing something else wrong?

I don't need the answer, just someone to point me in the right direction. It seems like the problems starts after line 27.

import java.util.scanner;

public class RockPaperScissor {
	public static void main (String [] args);

	Scanner input = new Scanner (system.in);

	int Computer= (int)(Math.random() * 3);


	System.out.print("Lets play Rock, Paper, Scissors. Enter 1 for rock, 2 for paper, 3 for scissors");

	int Response = input.nextInt();

	if (Response == Computer)
	{
	System.out.println("You draw!");
	}

	else if ((Response == 1) && (Computer == 2))
	{
	System.out.println("You lose! Paper beats rock!");
	}

	else if ((Response == 1) && (Computer == 3))
	{
	System.out.println("You win! Rock beats scissors!);
	}

	else if ((Response == 2) && (Computer == 1))

	{
	System.out.println("You win! Paper beats rock!");
	}

	else if ((Response == 2) && (Computer == 3))
	{
		System.out.println("You lose! Scissor beats paper!");
	}

	else if ((Response == 3) && (Computer == 1))
	{

	System.out.println("You lose! Rock beats scissors");
	}
	else
	{
	System out.println("You win! Scissors beat paper!")
	}
	}
}

I did not change anything in the flow or test the program for completeness, I just removed some typos and fixed the formatting:

import java.util.Scanner;

public class RockPaperScissor
{
   public static void main (String [] args)
   {
      Scanner input = new Scanner (System.in);

      int Computer= (int)(Math.random() * 3);


      System.out.print("Lets play Rock, Paper, Scissors. Enter 1 for rock, 2 for paper, 3 for scissors");

      int Response = input.nextInt();

      if (Response == Computer)
      {
         System.out.println("You draw!");
      }
      else if ((Response == 1) && (Computer == 2))
      {
         System.out.println("You lose! Paper beats rock!");
      }
      else if ((Response == 1) && (Computer == 3))
      {
         System.out.println("You win! Rock beats scissors!");
      }
      else if ((Response == 2) && (Computer == 1))
      {
         System.out.println("You win! Paper beats rock!");
      }
      else if ((Response == 2) && (Computer == 3))
      {
         System.out.println("You lose! Scissor beats paper!");
      }
      else if ((Response == 3) && (Computer == 1))
      {
         System.out.println("You lose! Rock beats scissors");
      }
      else
      {
         System.out.println("You win! Scissors beat paper!");
      }
   }
}

Check you random numbers (print them out at line 10). They are not what you expect.

ps: It's a java convention that your variables should begin with a lower-case letter. Class names start with an upper case.

This article has been dead for over six months. Start a new discussion instead.