我试图将第一个代码块中的calculate值返回到我的GUI中的JLabel(这将是我粘贴的第二个代码块。
第一段代码 -
<myNode axs="p1">
现在,下面是我的第二个GUI代码 -
import java.util.Scanner;
public class SpeedingTicketClass {
Scanner scan=new Scanner (System.in); //Creating a scanner to read in users input of the speed limit and their speed.
//Variables used to store users input of the speed limit and the speed they are going.
int speedLimit=0;
int usersSpeed=0;
String usersName;
//Method being used to obtain the speed limit of the area with a do/while loop to make sure a correct speed limit is entered.
public int getSpeedLimit() {
do {
System.out.println("Please enter the speed limit: ");
speedLimit=scan.nextInt();
}while (!(speedLimit==70 || speedLimit==20| speedLimit==30 || speedLimit==40 || speedLimit==50 || speedLimit==60));
return speedLimit;
}
//Method being used to obtain the speed in which a user is going at.
public int getUsersSpeed() {
System.out.println("Please enter your speed: ");
usersSpeed=scan.nextInt();
return usersSpeed;
}
public String getUsersInformation() {
usersName=scan.nextLine();
return usersName;
}
//Method being used to calculate the speed of the users inputs and what fine they will receive.
public String calculateSpeed() {
String calculate="";
if (speedLimit==20 && usersSpeed>20 && usersSpeed<=24) {
calculate="Just a warning. Please do not speed again!";
}
else if (speedLimit==20 && usersSpeed>24 && usersSpeed<30) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==20 && usersSpeed>=30 && usersSpeed<35) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==20 && usersSpeed>=35 && usersSpeed<40) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==20 && usersSpeed>=40) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==20 && usersSpeed<=20) {
calculate=("You are within the speed limit!");
} //20mph IF statement
if (speedLimit==30 && usersSpeed>30 && usersSpeed<=34) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==30 && usersSpeed>34 && usersSpeed<40) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==30 && usersSpeed>=40 && usersSpeed<45) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==30 && usersSpeed>=45 && usersSpeed<50) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==30 && usersSpeed>=50) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==30 && usersSpeed<=30) {
calculate=("You are within the speed limit!");
} //30mph IF statement
if (speedLimit==40 && usersSpeed>40 && usersSpeed<=44) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==40 && usersSpeed>44 && usersSpeed<50) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==40 && usersSpeed>=50 && usersSpeed<55) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==40 && usersSpeed>=55 && usersSpeed<60) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==40 && usersSpeed>=60) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==40 && usersSpeed<=40) {
calculate=("You are within the speed limit!");
} //40mph IF statement
if (speedLimit==50 && usersSpeed>50 && usersSpeed<=54) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==50 && usersSpeed>54 && usersSpeed<60) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==50 && usersSpeed>=60 && usersSpeed<65) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==50 && usersSpeed>=65 && usersSpeed<70) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==50 && usersSpeed>=70) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==50 && usersSpeed<=50) {
calculate=("You are within the speed limit!");
} //50mph IF statement
if (speedLimit==60 && usersSpeed>60 && usersSpeed<=64) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==60 && usersSpeed>64 && usersSpeed<70) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==60 && usersSpeed>=70 && usersSpeed<75) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==60 && usersSpeed>=75 && usersSpeed<80) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==60 && usersSpeed>=80) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==60 && usersSpeed<=60) {
calculate=("You are within the speed limit!");
} //60mph IF statement
if (speedLimit==70 && usersSpeed>70 && usersSpeed<=74) {
calculate=("Just a warning. Please do not speed again!");
}
else if (speedLimit==70 && usersSpeed>74 && usersSpeed<80) {
calculate=("Fine. £50 to pay!");
}
else if (speedLimit==70 && usersSpeed>=80 && usersSpeed<85) {
calculate=("Fine. £100 to pay!");
}
else if (speedLimit==70 && usersSpeed>=85 && usersSpeed<90) {
calculate=("3 points on your license and £150 fine to pay!");
}
else if (speedLimit==70 && usersSpeed>=90) {
calculate=("Disqualifed and a £1000 fine to pay!");
}
else if (speedLimit==70 && usersSpeed<=70) {
calculate=("You are within the speed limit!");
} //70mph IF statement
if (speedLimit>70) {
calculate=("Please enter a speed limit which is 70mph or under!");
}
System.out.println(calculate);
return calculate;
}
}
请有人帮助我,因为我已经一次又一次地尝试使其工作,但事实并非如此。
答案 0 :(得分:0)
您应该使用类似if (usersSpeed >= (!speedLimit + 20) && usersSpeed <= (!speedLimit + 40)
等的内容进行所有选择。