gpt4 book ai didi

java - 初学者 Java - 找不到符号错误?

转载 作者:行者123 更新时间:2023-12-01 08:11:44 26 4
gpt4 key购买 nike

我一直在试图找出为什么这个基本的 java 程序无法运行。我收到大约 7 个找不到符号错误。任何帮助将非常感激。我很想知道我做错了什么。

该程序只是一些基本计算,提示一些输入和输出数据,显示超速者的罚款金额。

import java.io.*;
import java.util.*;

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

// Create a scanner to read from keyboard
Scanner kbd = new Scanner(System.in);

System.out.print("\nEnter Driver's FIRST Name.");
String firstName = kbd.next();

System.out.print("\nEnter Driver's LAST Name.");
String lastName = kbd.next();

System.out.print("\nEnter Driver's Age.");
int age = Integer.parseInt(kbd.next());

System.out.print("\nEnter the Speed Limit.");
int speedLimit = Integer.parseInt(kbd.next());

System.out.print("\nEnter Driver's Actual Speed");
int actualSpeed = Integer.parseInt(kbd.next());

System.out.print("\nDid violation occur in construction zone? (yes/no)");
String constructionZone = kbd.next();

int speedDifference = (actualSpeed - speedLimit);



if (speedDifference <= 5) {
int baseFine = 0;
}
else if(speedDifference >= 20) {
int baseFine = (speedDifference / 5) * 50;
}
else {
int baseFine = (speedDifference / 5) * 30;
}



if(constructionZone.equals("yes")) {
int constructionFine = 10;
}
else {
int constructionFine = 0;
}



if(age <= 21 && speedDifference >= 20) {
int underageFine = 300;
}
else {
int underageFine = 0;
}



int totalFine = baseFine + constructionFine + underageFine;

System.out.println("Last Name: " + lastName);
System.out.println("First Name: " + firstName);
System.out.println("Driver Age: " + age);
System.out.println("Speed Limit: " + speedLimit);
System.out.println("Actual Speed: " + actualSpeed);
System.out.println("MPH Over Limit: " + speedDifference);
System.out.println("Base Fine: $" + baseFine);
System.out.println("Construction Zone Fine: $" + constructionFine);
System.out.println("Underage Fine: $" + underageFine);
System.out.println("Total Fine: $" + totalFine);

}
}

最佳答案

您的变量 baseFineconstructionFineunderageFine 是在 if 语句的范围内定义的。将它们声明在该范围之外,以便它们在 main 方法的范围内可见。例如

int baseFine = 0;
if (speedDifference <= 5) {
baseFine = 0;
}

关于java - 初学者 Java - 找不到符号错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16737144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com