gpt4 book ai didi

java - 我对类里面的 setter/getter 和返回感到困惑?

转载 作者:行者123 更新时间:2023-12-02 01:23:47 25 4
gpt4 key购买 nike

我一直在自学编码、 getter 和返回值,以及如何在主程序中调用它们。为了尝试一下,我尝试编写一个程序来计算长途电话的费用,但是当我运行它时它就崩溃了,我知道这与类有关。

公平警告,我计算 AM/PM 的时间有点奇怪,但我尽力了。

我认为这必须是调用我的主代码中的计算——特别是 String weekday = call1.calculateweekday();和 int time = call1.calculatetime(); ,但我对编程非常陌生,我才刚刚开始学习这些术语和用途,所以我不知道是什么。我只知道,当我在主程序中移动这两行时,它会在此时中断。

package practice;

import java.util.Scanner;

class Call {
String weekdayinput;
String weekday;
int hour;
String ampm;
int time;

int calculatetime() {

if (ampm.equals("pm") || ampm.equals("PM") || ampm.equals("Pm")) {

time = hour + 1200;
} else if (ampm.equals("am") || ampm.equals("AM") || ampm.equals("Am")) {

time = hour;

}

else {
System.out.println("You entered something either time or AM/PM incorrectly.");
}

return time;
}

String calculateweekday() {

if (weekdayinput.equals("mo") || weekdayinput.equals("Mo") || weekdayinput.equals("MO")) {

weekday = "Monday";
}

else if (weekdayinput.equals("tu") || weekdayinput.equals("Tu") || weekdayinput.equals("TU")) {

weekday = "Tuesday";
}

else if (weekdayinput.equals("we") || weekdayinput.equals("We") || weekdayinput.equals("WE")) {

weekday = "Wednesday";
}

else if (weekdayinput.equals("th") || weekdayinput.equals("Th") || weekdayinput.equals("TH")) {

weekday = "Thursday";
}

else if (weekdayinput.equals("fr") || weekdayinput.equals("Fr") || weekdayinput.equals("FR")) {

weekday = "Friday";
}

else if (weekdayinput.equals("sa") || weekdayinput.equals("Sa") || weekdayinput.equals("SA")) {

weekday = "Saturday";
}

else if (weekdayinput.equals("su") || weekdayinput.equals("Su") || weekdayinput.equals("SU")) {

weekday = "Sunday";
}

else {
System.out.println("You entered your weekday incorrectly.");
}

return weekday;
}

}

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

Scanner input = new Scanner(System.in);
Call call1 = new Call();

String weekday = call1.calculateweekday();
int time = call1.calculatetime();

System.out.println("To calculate the cost per minute of your long-distance call, we'll need some information.");
System.out.println(
"What hour are you planning on making the call. Minutes aren't necessary. Please only enter the hour number. (ex. 8)");
call1.hour = input.nextInt();
input.hasNextLine();
System.out.println("Is the call taking place AM or PM?");
call1.ampm = input.nextLine();
input.hasNextLine();
System.out.println("And what day of the week is that? Please enter weekday with only first two letters. (ex. Fr");
call1.weekdayinput = input.nextLine();



if (time >= 8 && time <= 11 && !weekday.equals("Saturday") && !weekday.equals("Sunday")
|| time >= 1212 && time <= 1206 && !weekday.equals("Saturday") && !weekday.equals("Sunday"))

{
System.out.println("Your call will charge $4.50 a minute.");
}

else if (time == 12 && !weekday.equals("Saturday") && !weekday.equals("Sunday")
|| time >= 1 && time < 8 && !weekday.equals("Saturday") && !weekday.equals("Sunday")
|| time > 1206 && time <= 1211 && !weekday.equals("Saturday") && !weekday.equals("Sunday")) {

System.out.println("Your call will charge $4.00 a minute.");

}

else if (weekday.equals("Saturday") || weekday.equals("Sunday")){

System.out.println("Your call will charge $2.25 a minute.");
}

else {
System.out.println("You must have entered something wrong!");
}

}
}

因此,我们的想法是,周一至周五上午 8:00 至下午 6:00 之间发起的任何调用均按每分钟 4.50 美元计费。周一至周五上午 8:00 之前或下午 6:00 之后开始的任何通话均按费率计费每分钟 4.00 美元。最后,任何在周六或周日调用的电话均按每分钟 2.25 美元的费率收费。

但是当我运行这个程序时,我在线程“main”java.lang.NullPointerException中得到异常在 Practice.Call.calculateweekday(GettersandREturns.java:32)在practice.GettersandREturns.main(GettersandREturns.java:82)

任何帮助将不胜感激。学习很难。

最佳答案

在主函数中,您将创建一个新调用并调用该调用的函数。

Call call1 = new Call();

String weekday = call1.calculateweekday();
int time = call1.calculatetime();

此时,如果您查看具有以下变量的 Call 类,您会收到错误

String weekdayinput;
String weekday;
int hour;
String ampm;
int time;

你会发现这些变量一开始并没有初始化,对于int来说默认值是0,对于String来说默认值是null(在编程的时候有时会遇到空指针异常)。只要您不尝试访问此变量,就不会禁止使用 null 值,而这正是您的函数所做的。

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean java.lang.String.equals(java.lang.Object)”

将是错误

 if (ampm.equals("pm") || ampm.equals("PM") || ampm.equals("Pm"))

将会抛出异常,因为您的 ampm 或您尝试访问的任何其他字符串此时为 null,并且您正在尝试将 null 字符串与字符串进行比较。

确保在将字符串与任何内容进行比较之前先对其进行初始化,例如与

String weekdayinput = "mo";
String weekday "mo";
int hour;
String ampm "am";
int time;

在请求输入后,您应该调用工作日和时间的函数。

 Scanner input = new Scanner(System.in);
Call call1 = new Call();

System.out.println("To calculate the cost per minute of your long-distance call, we'll need some information.");
System.out.println(
"What hour are you planning on making the call. Minutes aren't necessary. Please only enter the hour number. (ex. 8)");
call1.hour = input.nextInt();
input.hasNextLine();
System.out.println("Is the call taking place AM or PM?");
call1.ampm = input.nextLine();
input.hasNextLine();
System.out.println("And what day of the week is that? Please enter weekday with only first two letters. (ex. Fr");
call1.weekdayinput = input.nextLine();
String weekday = call1.calculateweekday();
int time = call1.calculatetime();

作为旁注:您应该阅读关于可以替换这么多 if 语句的开关。

关于java - 我对类里面的 setter/getter 和返回感到困惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57263475/

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