vendredi 23 octobre 2015

I'm trying to implement variables in if statements but it does not recognize them while compiling [duplicate]

This question already has an answer here:

This is my whole program.

/**
* Auto Generated Java Class.
*/
import java.util.Scanner;
public class lopezRace {


    public static void main(String[] args) { 
        Scanner scan = new Scanner(System.in);
        String name1;
        double time1;
        String name2;
        double time2;
        String name3;
        double time3;


        System.out.println("Welcome to Race Tracker/n");
        System.out.println("Please enter the name of the runner: ");
        name1 = scan.nextLine();
        System.out.println("Please enter the time in minutes the runner took to finish the race");
        time1 = scan.nextDouble();
        scan.nextLine();
        System.out.println("Please enter the name of the runner: ");
        name2 = scan.nextLine();
        System.out.println("Please enter the time in minutes the runner took to finish the race");
        time2 = scan.nextDouble();
        scan.nextLine();
        System.out.println("Please enter the name of the runner: ");
        name3 = scan.nextLine();
        System.out.println("Please enter the time in minutes the runner took to finish the race");
        time3 = scan.nextDouble();

        if (time1 > time2 && time1 > time3){
            System.out.println("The runner " + name1 + " is #1 with the following time: " + time1 );
            final int var1 = 1;
        }
        if (time2 > time1 && time2 > time3){
            System.out.println("The runner " + name2 + " is #1 with the following time: " + time2 );
            final int var2 = 2;
        }
        if (time3 > time1 && time3 > time2){
            System.out.println("The runner " + name3 + " is #1 with the following time: " + time3 );}


            if (1 == var1){
                if (time2 > time3){
                    System.out.println("The runner " + name2 + " is #2 with the following time: " + time2);
                    System.out.println("The runner " + name3 + " is #3 with the following time: " + time3 );
                }else{
                     System.out.println("The runner " + name3 + " is #2 with the following time: " + time3 );
                     System.out.println("The runner " + name2 + " is #3 with the following time: " + time2 );
                }
            }

            if (2 == var2){
                if (time1 > time3){
                    System.out.println("The runner " + name1 + " is #2 with the following time: " + time1);
                    System.out.println("The runner " + name3 + " is #3 with the following time: " + time3 );
                }else{
                     System.out.println("The runner " + name1 + " is #2 with the following time: " + time1 );
                     System.out.println("The runner " + name2 + " is #3 with the following time: " + time2 );
                }
          }
    }
}

Here are the errors I'm getting:

2 errors found:
File: P:\AP Computers Erik Lopez Files\lopezRace.java  [line: 43]
Error: var1 cannot be resolved to a variable
File: P:\AP Computers Erik Lopez Files\lopezRace.java  [line: 53]
Error: var2 cannot be resolved to a variable

Aucun commentaire:

Enregistrer un commentaire