相关文章推荐
冷静的火车  ·  mysql ...·  10 月前    · 
public static void main ( String [] args) System. out .println ( " Give at least 3 numbers: " ); Scanner xd = new Scanner (System. in ); double num1 = xd.nextDouble(); double num2 = xd.nextDouble(); double num3 = xd.nextDouble(); double largest,smallest,middle; System. out .println ( " 1st number: " + num1); System. out .println ( " 2nd number: " + num2); System. out .println ( " 3rd number: " + num3); if (num1 > num2){ if (num1 > num2) largest = num1; if (num2 > num3) middle = num2; smallest = num3; middle = num3; smallest = num2; largest = num3; middle = num1; smallest = num2; else { if (num2 > num1) if (num2 > num3) largest = num2; if (num1 > num3); middle = num1; smallest = num3; middle = num3; smallest = num1; largest = num3; middle= num2; smallest= num1; What I have tried:
I've tried re-coding but it still has the same error.
The three integers that the user will input should be placed in ascending and descending order.
import java.util.Scanner;
public class seatwork4
{
public static void main (String [] args)
{
System.out.println ("Give at least 3 numbers: ");
Scanner xd = new Scanner (System.in);

double num1 = xd.nextDouble();
double num2 = xd.nextDouble();
double num3 = xd.nextDouble();
double largest,smallest,middle;

System.out.println ("1st number: " + num1);
System.out.println ("2nd number: " + num2);
System.out.println ("3rd number: " + num3);

if (num1 > num2)
{
if (num1 > num2)
{
largest = num1;
if (num2 > num3)
{
middle = num2;
smallest = num3;
}
else
{
middle = num3;
smallest = num2;
}

{
largest = num3;
middle = num1;
smallest = num2;
}
}
}
else
{
if (num2 > num1)
{
if (num2 > num3)
{
largest = num2;
}
if (num1 > num3);
{
middle = num1;
smallest = num3;
}

{
middle = num3;
smallest = num1;
}

{
largest = num3;
middle = num2;
smallest = num1;
}
}
}

}

}

here's what I did
You've just removed the second else keyword from each block. The code within the braces will always execute, overwriting the variables you've just set in your if..else block.

Stop making random changes to your code. Get a piece of paper and a pen, and write down exactly what you want the code to do, step by step. Then write the code to do that.
You cannot have more than one else clause for an if statement. If you need to test for multiple conditions then you need to use else if or a switch block.
And in your code you have:
if (num1 > num2){ if (num1 > num2) // why are you repeating this if statement? largest = num1; if (num2 > num3) middle = num2; smallest = num3; middle = num3; smallest = num2; else // you cannot have a second else clause largest = num3; middle = num1; smallest = num2; I haven't studied your code beyond that, to see what you're trying to do, but it could be that you need to add an if after the first else . The following makes sense:
if (condition) else if (condition) You need to start sorting these syntax errors for yourself - you really didn't spend any significant time trying to fix this, given the time different between this and your previous syntax error ridden question / answers.
Syntax errors are part of life: they mean you typed it wrong and are simple to fix.
Just look at the error message and it will show you the file name, the line and column it found an error on, and give you a message describing the error.
So look at that line and consider what the message is trying to tell you: "'else' without 'if'"
That means it finds an else clause that can't be "tied up" with an if clause - and the syntax of an if statement in Java is simple:
if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false So all you have to do is look at the code above the line it found the problem on and see where the if that you think it matches is, and why it isn't "lining up" correctly.
And there are two reasons why in that code: a spurious semicolon you haven't noticed yet, and an else that is all alone.
Fix that one error, and compile again - the chances are many errors will go away at the same time!
Give it a try: you are going to be fixing syntax errors for a long time, and it's much, much quicker to be able to solve them yourself that it is to post for help on trivial matters like this and have to wait for a reply! -----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
import java.util.Scanner; public class seatwork4 public static void main ( String [] args) System.out.println ( " Give at least 3 numbers: " ); Scanner xd = new Scanner (System.in); double num1 = xd.nextDouble(); double num2 = xd.nextDouble(); double num3 = xd.nextDouble(); double largest,smallest,middle; System.out.println ( " 1st number: " + num1); System.out.println ( " 2nd number: " + num2); System.out.println ( " 3rd number: " + num3); if (num1 > num2){ if (num1 > num2) largest = num1; if (num2 > num3) middle = num2; smallest = num3; middle = num3; smallest = num2; largest = num3; middle = num1; smallest = num2; else { if (num2 > num1) if (num2 > num3) largest = num2; if (num1 > num3); middle = num1; smallest = num3; middle = num3; smallest = num1; largest = num3; middle= num2; smallest= num1; Indentation style - Wikipedia [ ^ ]
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home [ ^ ]
ultraedit [ ^ ]
Enabling Open Innovation & Collaboration | The Eclipse Foundation [ ^ ]
-----
Your code is over complicated for what you want to do, try this:
if (num1 > num2){ largest = num1; middle = num2; else { largest = num2; middle = num1; if (middle > num3){ smallest = num3; else { smallest = middle; if (largest > num3){ middle = largest; largest = num3; else { middle = num3;
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •