【问题标题】:java symbol out of scope out of scopejava符号超出范围超出范围
【发布时间】:2023-03-27 16:55:01
【问题描述】:

我必须修改我的程序以不断提示每次租赁的分钟数,直到该值介于 60 和 7,200 之间(含)。所以我想做的是将输入提示放在for循环之间,但是每次我都这样做。我的程序告诉我找不到符号,即使它是全局的并且在 for 循环中使用它。我只想知道如何循环分钟然后我将添加 if 条件来检查它是否在 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;
   }
 }   
 }

【问题讨论】:

    标签: java loops object for-loop scope


    【解决方案1】:
    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 的每个值,请使用数组。我不明白你的符号超出范围错误,你能粘贴你的错误输出吗?

    【讨论】:

    • RentalDemo.java:60: 错误:找不到符号 System.out.println("合同编号:" + mike.getContractNumber()+ " \nHours: "+mike.getHours()+ "分钟:" +mike.getMins()+ ^ 符号:可变麦克位置:类 RentalDemo
    • 它把 ^ 放在 mike.getContractNumber 下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 2015-04-12
    • 1970-01-01
    • 2012-05-04
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多