Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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 方法和构造函数实现_Java_Methods_Constructor - Fatal编程技术网

Java 方法和构造函数实现

Java 方法和构造函数实现,java,methods,constructor,Java,Methods,Constructor,我的代码目前还没有编译,我知道这是因为我的构造函数没有返回值——但是如果我能得到一些帮助(因为我有一个我已经尝试过修复的版本),我将不胜感激 我有两个文件,一个有构造函数,另一个访问该构造函数。我还应该注意,我在网上找到的所有帮助都说我必须使用if-else语句,但这会导致更多错误 主文件的我的代码: import java.util.Scanner;//importing scanner public class QuestionTwo { public static void ma

我的代码目前还没有编译,我知道这是因为我的构造函数没有返回值——但是如果我能得到一些帮助(因为我有一个我已经尝试过修复的版本),我将不胜感激

我有两个文件,一个有构造函数,另一个访问该构造函数。我还应该注意,我在网上找到的所有帮助都说我必须使用if-else语句,但这会导致更多错误

主文件的我的代码:

import java.util.Scanner;//importing scanner 

public class QuestionTwo {

  public static void main(String[] args) {
    int numberofDays;//these two lines define variables
    int sharePoints;

Scanner keyboard = new Scanner(System.in);//activating scanner
System.out.print("Number of days in the period: ");//asking question

numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

System.out.print("Share points on the first day: ");//asking another question
sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input
while (numberofDays < 10 || numberofDays > 20)
{
    System.out.println("The number of days doesn’t meet the required criteria, enter it again");
    System.out.print("Number of days in the period: ");
    numberofDays = keyboard.nextInt();
}

SharePattern share = new SharePattern(numberofDays, sharePoints);
SharePattern.outPutTablePrinter(numberofDays, sharePoints);
//above two lines print day and share points, as well as the first line 
of text (as it does not change)


}

}
import java.util.Scanner//导入扫描仪
公开课问题二{
公共静态void main(字符串[]args){
int numberofDays;//这两行定义变量
int共享点;
扫描仪键盘=新扫描仪(System.in);//激活扫描仪
System.out.print(“期间天数:”;//提问
numberofDays=keyboard.nextInt();//通过将变量定义为键盘输入来获取输入
System.out.print(“第一天分享积分:”;//问另一个问题
sharePoints=keyboard.nextInt();//通过将变量定义为键盘输入来获取输入
而(numberofDays<10 | | numberofDays>20)
{
System.out.println(“天数不符合要求的条件,请重新输入”);
系统输出打印(“期间天数:”);
numberofDays=keyboard.nextInt();
}
SharePattern share=新的SharePattern(天数、sharePoints);
SharePattern.outPutTablePrinter(numberofDays,sharePoints);
//以上两行打印日期和共享点,以及第一行
文本(因为它不会改变)
}
}
以下是构造函数(返回错误的构造函数)的代码:

公共类共享模式{
私人静态整数日;
私有静态int共享;
公共共享模式(int numberofDays,int sharePoints)//构造函数
{
numberofDays=天;
共享点=共享;
}
公共静态int输出打印机(int numberofDays,int sharePoints){

对于(inti=2;i首先,您有一个无效的
else{}else

不太清楚你想完成什么

选择1 删除构造函数和静态字段。永远不要使用
share
变量

使您的
outPutTablePrinter
方法使用您传递给它的参数。该方法不返回任何内容。它正在打印一个值。使其
public static void

选择2 如果希望保留构造函数,则需要翻转这些相等语句的顺序并删除
static
关键字

仍然使方法无效

public class SharePattern {
    private int days;
    private int share;

    public SharePattern(int numberofDays, int sharePoints){
        days = numberofDays;
        share = sharePoints;
    }

    public void outPutTablePrinter() {

在main方法中,您必须使用
share.outPutTablePrinter()

它实际上非常简单(再次为我的无能感到抱歉):

我应该把

return (sharePoints);

如果我正确理解您的代码,我建议您这样做

SharePattern.class

private static int days;
private static int share;

public SharePattern(int numberofDays, int sharePoints)//constructor
{
    days=numberofDays;
    share=sharePoints;

}
public static void outPutTablePrinter(){

    for (int i = 2; i <= days; i++) {
        if (days % 2 == 0)
            if (i <= days / 2) {
                share = share + 50;
            } else {
                share = share - 25;
            }
        else
            if (i <= days / 2 + 1) {
                share = share + 50;
            } else {
                share = share - 25;
            }
    }
    System.out.println("The share points on the final day would be: "+share);
}
私有静态整数天;
私有静态int共享;
公共共享模式(int numberofDays,int sharePoints)//构造函数
{
天数=天数;
共享=共享点数;
}
公共静态无效输出打印机(){

对于(int i=2;i您将字段设置为反向…我非常怀疑您是否需要静态字段。并且您的方法必须返回某些内容删除结尾处多余的大括号,并为您的方法提供一个返回值。在构造函数中,您的倒数numberofDays=days;sharePoints=share;应该是days=numberofDays;share=sharePointsSharePattern@Aominèwha如果返回值是,我很抱歉我的构造函数不返回值。括号是不需要的,你是想返回共享吗?
private static int days;
private static int share;

public SharePattern(int numberofDays, int sharePoints)//constructor
{
    days=numberofDays;
    share=sharePoints;

}
public static void outPutTablePrinter(){

    for (int i = 2; i <= days; i++) {
        if (days % 2 == 0)
            if (i <= days / 2) {
                share = share + 50;
            } else {
                share = share - 25;
            }
        else
            if (i <= days / 2 + 1) {
                share = share + 50;
            } else {
                share = share - 25;
            }
    }
    System.out.println("The share points on the final day would be: "+share);
}