简体   繁体   中英

java symbol out of scope out of scope

i have to modify my program to continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. So what am trying to do is to put the prompts for the input between a for loop, however every time i do it. my program tells me cannot find symbol for even though its global and am using it in the for loop. I just want to know how can i loop mins then i will add and if condition to check if its between 60 and 7200 and break out of the loop.

import java.util.Scanner;

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

   Scanner input = new Scanner(System.in);

    String contractName;
    int mins;

   //getting inputs

   //HERE IS THE PROBLEM, IF I TAKE THE FOR LOOP OUT IT WILL WORK FINE BUT ONLY ONCE.
   for (int x=0;x<10;x++)
   {
   System.out.println("Please enter contractName"); 
   contractName = input.nextLine(); 



   System.out.println("Please enter mins");
    mins = input.nextInt();
   }

    //object name
    Rental mike = new Rental(contractName, mins);

   //to clear buffer 
   input.nextLine(); 

   //getting inputs 
   System.out.println("Please enter contractName"); 
   contractName = input.nextLine(); 

   System.out.println("Please enter mins");
   mins = input.nextInt();

    //object name
    Rental luke = new Rental(contractName,mins);

    //to clear buffer 
    input.nextLine(); 

     //getting inputs 
     System.out.println("Please enter contractName"); 
     contractName = input.nextLine(); 

     System.out.println("Please enter mins");
     mins = input.nextInt();

     //object name
     Rental ihab = new Rental(contractName,mins);




    //outputs

    System.out.println("Contract Number: " + mike.getContractNumber()+ " \nHours: "+mike.getHours()+ " Minutes: " +mike.getMins()+
    "\nTotal Price: " +mike.getPrice());


    System.out.println("\nContract Number: " + luke.getContractNumber()+ " \nHours: "+luke.getHours()+ " Minutes: " +luke.getMins()+
    "\nTotal Price: " +luke.getPrice());

     System.out.println("\nContract Number: " + ihab.getContractNumber()+ " \nHours: "+ihab.getHours()+ " Minutes: " +ihab.getMins()+
    "\nTotal Price: " +ihab.getPrice());



   //methods

   int highest = compare(mike,luke);
   int highest2 = compare(ihab,mike);
   int highest3 = compare(luke,ihab);

   if (highest > highest2 && highest > highest3)
   {
      System.out.println("\nContract Number:"+ mike.getContractNumber()+ " is the greater");
   }
   else if (highest2 > highest && highest2 > highest3)
   {
      System.out.println("\nContract Number:" +ihab.getContractNumber()+ " is the greater");

   }
   else 
   {
     System.out.println("\nContract Number:" +luke.getContractNumber()+ " is the greater");

   }




 }



 public static int compare(Rental ob1, Rental ob2)
 {


     int mins;  
   if ((ob1.getHours() > ob2.getHours()) || ob1.getMins() > ob2.getMins())
   {
         mins = ob1.getHours()*60 + ob1.getMins(); 

         return mins;

   }

   else
   { 
          mins = ob1.getHours()*60 + ob1.getMins(); 
     return mins;
   }
 }   
 }
for (int x=0;x<10;x++)
{
   System.out.println("Please enter contractName"); 
   contractName = input.nextLine(); 



   System.out.println("Please enter mins");
   mins = input.nextInt();
   if(mins > 60 && mins < 7200)
       break;
}

Use 'break' to break out of a loop. Also, the for loop just replaces each variable each time, making it store only the values on x = 9. If you want every value for mins and contractName, use arrays. I don't understand your symbol out of scope error, can you paste your error output?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM