二进制运算符错误"-"Java 的错误操作数类型



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

"DayCare.java:29:错误:二进制运算符'-'的操作数类型不正确[numDaysString-1](第一种类型:字符串第二种类型: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); }  
}

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

最新更新