Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我对类中的getter和returns感到困惑?_Java_Return_Getter - Fatal编程技术网

Java I';我对类中的getter和returns感到困惑?

Java I';我对类中的getter和returns感到困惑?,java,return,getter,Java,Return,Getter,我一直在教自己编码、getter和返回值,以及如何在主程序中调用它们。为了试一试,我试着编写一个程序来计算长途电话的费用,但当我运行它时,它就崩溃了,我知道这与课程有关 公平的警告,我计算上午/下午的时间有点奇怪,但我已经尽力了 我想这一定是因为在我的主代码中调用了计算——特别是字符串weekday=call1.calculateweekday();和int time=call1.calculatetime(),但我对编程非常陌生,我只是刚刚开始学习这些术语和用法,所以我不知道是什么。我只知道,

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

公平的警告,我计算上午/下午的时间有点奇怪,但我已经尽力了

我想这一定是因为在我的主代码中调用了计算——特别是字符串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!");
        }

    }
}
包装实践;
导入java.util.Scanner;
集体电话{
字符串weekdayinput;
字符串工作日;
整小时;
字符串ampm;
整数时间;
int计算时间(){
if(ampm.equals(“pm”)| | ampm.equals(“pm”)| | ampm.equals(“pm”)){
时间=小时+1200;
}else如果(ampm.equals(“am”)| | ampm.equals(“am”)| | ampm.equals(“am”)){
时间=小时;
}
否则{
System.out.println(“您输入的时间或AM/PM不正确。”);
}
返回时间;
}
字符串calculateweekday(){
if(weekdayinput.equals(“mo”)| | weekdayinput.equals(“mo”)| | weekdayinput.equals(“mo”)){
工作日=“星期一”;
}
else if(weekdayinput.equals(“tu”)| | weekdayinput.equals(“tu”)| | weekdayinput.equals(“tu”)){
工作日=“星期二”;
}
else if(weekdayinput.equals(“we”)| | weekdayinput.equals(“we”)| | weekdayinput.equals(“we”)){
工作日=“星期三”;
}
else if(weekdayinput.equals(“th”)| | weekdayinput.equals(“th”)| | weekdayinput.equals(“th”)){
工作日=“星期四”;
}
else if(weekdayinput.equals(“fr”)| | weekdayinput.equals(“fr”)| | weekdayinput.equals(“fr”)){
工作日=“星期五”;
}
else if(weekdayinput.equals(“sa”)| | weekdayinput.equals(“sa”)| | weekdayinput.equals(“sa”)){
工作日=“星期六”;
}
else if(weekdayinput.equals(“su”)| | weekdayinput.equals(“su”)| | weekdayinput.equals(“su”)){
工作日=“星期日”;
}
否则{
System.out.println(“您输入的工作日不正确。”);
}
返回工作日;
}
}
公共类getters{
公共静态void main(字符串参数[]){
扫描仪输入=新扫描仪(System.in);
Call call1=新调用();
字符串weekday=call1.calculateweekday();
int time=call1.calculatetime();
System.out.println(“要计算长途电话每分钟的费用,我们需要一些信息。”);
System.out.println(
“您计划在什么时间打电话。不需要分钟。请只输入小时数。(例8)”;
call1.hour=input.nextInt();
input.hasNextLine();
System.out.println(“通话是在上午还是下午?”;
call1.ampm=input.nextLine();
input.hasNextLine();
System.out.println(“那是一周中的哪一天?请输入只有前两个字母的工作日(例如Fr”);
call1.weekdayinput=input.nextLine();
如果(时间>=8&&time=1212&&time=1&&time<8&&!weekday.equals(“星期六”)&&!weekday.equals(“星期日”)

||time>1206&&time在main函数中,您正在创建一个新调用并调用调用的函数

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,对于字符串,默认值为null(编程时有时会遇到NullPointerException)。只要您不尝试访问此变量,就不禁止使用null值,这就是您的函数所做的

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

将是

 if (ampm.equals("pm") || ampm.equals("PM") || ampm.equals("Pm"))
将抛出,因为此时尝试访问的ampm或任何其他字符串为空,并且您正在尝试将空字符串与字符串进行比较

确保在将字符串与任何内容(例如与)进行比较之前初始化字符串

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语句。

在main函数中,您正在创建一个新调用并调用该调用的函数

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,对于字符串,默认值为null(编程时有时会遇到NullPointerException)。只要您不尝试访问此变量,就不禁止使用null值,这就是您的函数所做的

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

将是

 if (ampm.equals("pm") || ampm.equals("PM") || ampm.equals("Pm"))
将抛出,因为此时尝试访问的ampm或任何其他字符串为空,并且您正在尝试将空字符串与字符串进行比较

确保在将字符串与任何内容(例如与)进行比较之前初始化字符串

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();
作为旁注:您应该阅读有关交换机的内容