当前位置: 首页 > 知识库问答 >
问题:

二进制运算符错误的操作数类型“-”java

董阳平
2023-03-14

我不知道如何修正我错误。错误状态

“DayCare.java:29:错误:二进制运算符”-“[numDaysString-1])的操作数类型不正确)第一类型:String第二类型:int”


public class DayCare
{
   public static void main(String args[]) 
   {
      // Declare two-dimensional array here.
      double weeklyRate[][] =
   {{30.00, 60.00, 88.00, 115.00, 140.00},
      {26.00, 52.00, 70.00, 96.00, 120.00},
    {24.00, 46.00, 67.00, 89.00, 110.00 }, 
    {22.00, 40.00, 60.00, 75.00, 88.00},
     {20.00, 35.00, 50.00, 66.00, 84.00}};
      // Declare other variables.
      int numDays;   
      int age;
      String numDaysString;
      String ageString;
      int QUIT = 99;
      Scanner input = new Scanner(System.in);

      while(age != QUIT)
      {  System.out.println("Enter number of days: ");
       numDaysString = input.nextLine(); numDays = Integer.parseInt(numDaysString);
        if(age >= 4) age = 4; System.out.println("Weekly charge is $" + weeklyRate[age]
         [numDaysString - 1]); 
        System.out.println("Enter the age of the child or 99 to quit: "); } 
         System.out.println("End of program"); System.exit(0); }  
      }

共有1个答案

邓开济
2023-03-14

code>NumDaysString的类型是String并且您还需要将Age初始化为任意值

import java.util.Scanner;

public class Split {

    public static void main(String args[]) {
        // Declare two-dimensional array here.
        double weeklyRate[][] = { { 30.00, 60.00, 88.00, 115.00, 140.00 }, { 26.00, 52.00, 70.00, 96.00, 120.00 },
                { 24.00, 46.00, 67.00, 89.00, 110.00 }, { 22.00, 40.00, 60.00, 75.00, 88.00 },
                { 20.00, 35.00, 50.00, 66.00, 84.00 } };
        // Declare other variables.
        int numDays;
        int age = 0;
        String numDaysString;
        String ageString;
        int QUIT = 99;
        Scanner input = new Scanner(System.in);

        while (age != QUIT) {
            System.out.println("Enter number of days: ");
            numDaysString = input.nextLine();
            numDays = Integer.parseInt(numDaysString);
            if (age >= 4)
                age = 4;
            System.out.println("Weekly charge is $" + weeklyRate[age][numDays - 1]);
            System.out.println("Enter the age of the child or 99 to quit: ");
        }
        System.out.println("End of program");
        System.exit(0);
    }

}
 类似资料: