簡體   English   中英

Java符號超出范圍

[英]java symbol out of scope out of scope

我必須修改程序以連續提示每次租賃的分鍾數,直到該值介於60到7,200(含)之間。 因此,我要做的是將輸入提示放在for循環之間,但是每次我這樣做時。 我的程序告訴我即使它是全局符號也找不到符號,並且在for循環中使用它。 我只想知道如何循環分鍾,然后添加條件條件來檢查其是否在60到7200之間並退出循環。

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;
}

使用“ break”打破循環。 同樣,for循環每次都只替換每個變量,使其僅存儲x = 9上的值。如果要讓mins和contractName的每個值都使用數組。 我不了解您的符號超出范圍錯誤,可以粘貼錯誤輸出嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM